继承的例子

最近学校留作业,因为用不惯NetBean,还是用JCreator编写代码。写着写着无意中搞出一个继承,不知道这样子是好还是坏。自我感觉在添加组件时挺方便的,希望路过的高手指点:

 

/**
 * @(#)MainFrame.java
 *
 *
 * @author 
 * @version 1.00 2010/10/20
 */
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class MainFrame extends JFrame {

    private JScrollPane jspForTxtArea;
    private JLabel[] labels;
    private JTextArea txtArea;
    private JTextField txtField1,txtField2;
    private MyButton[] buttons;
    private String[] text;
	
    private void init() {
        text = new String[7];
        text[0] = "This should be a unique identifier for the purposes of filing. If more than one person is working on the project or more than one analysis technique is being used, this identifier could contain letters and numbers. For example, if Chris Smith and Jan Koo are both doing an analysis, the identifier might be CS1 or JK75. If both a heuristic evaluation and a think-aloud usability study were used, the identifiers might be HE6 or TA89. Follow the unique identifier with the word 'Problem,' if the report pertains to a usability problem of the interface, or the words 'Good Feature,' if it describes an aspect of the interface you feel should be preserved in any redesign.";
        text[1] = "This description will be used as the 'name' of this UAR when you talk about its relation to other UARs. Make the name as short as possible (about three to five words) but still descriptive and distinguishable from other aspects of the system. If this UAR is about a problem (as opposed to a good feature), make sure you have a name that describes the problem, rather than a solution.";
        text[2] = "This is the objective supporting material that justifies your identifying the aspect as worthy of report. This section needs to contain enough information for a reader of this UAR to understand what triggered the report. For an HE report, for instance, this could be an image of a cluttered screen and the heuristic about aesthetics and minimalist design. In a think-aloud study this is usually what was on the screen (a screen shot or description), what the user did (keystrokes, mouse movements), what the system did in response to any user actions, and what the user said. You need to include enough pertinent information about the identification of an aspect for the reader to understand what the analyst was thinking when the aspect was identified (for HE) or what the user was trying to do when the aspect either hindered or facilitated his or her progress.";
        text[3] = "This is your interpretation of the evidence. That is, for a think-aloud usability test, why you think what happened happened, or, for an HE, why you think the aspect was designed the way it was. You need to provide enough content in this explanation for the reader to understand the problem-even if they do not know the system or domain as well as you do.";
        text[4] = "This is your reasoning about how important it is to either fix this problem or preserve this good feature. This includes how frequently the users will experience this aspect, whether they are likely to learn how it works, whether it will affect new users, casual users, experienced users, etc.";
        text[5] = "If this aspect is a problem (as opposed to a good feature to be preserved in the next version of the software), this is the place to propose a solution. It is not necessary to have a solution as soon as you identify a problem-you might find after analyzing the whole interface that many problems are related and can all be fixed by making a single broad change instead of making several small changes. However, if you do propose a possible solution, report any potential design trade-offs that you see";
        text[6] = "It is often the case that UARs are related to each other. This is where you record which UARs this one is related to and a statement about how it is related. Make sure that all the related UARs point to each other. It is a common mistake to enter the pointer into a newly created UAR, but neglect to go back to the previous ones that it relates to and update their UARs.";
		
        labels = new JLabel[14];
		
        labels[0] = new JLabel("UAR componet names:");
        labels[1] = new JLabel("UAR componet description:");
        labels[2] = new JLabel("1.UAR Identifier");
        labels[3] = new JLabel("2.Succinct Description of the Usability Aspect");
        labels[4] = new JLabel("3.Evidence for the Aspect");
        labels[5] = new JLabel("4.Explanation of the Aspect");
        labels[6] = new JLabel("5.Severity of the Problem or Benefit of the Good Feature");
        labels[7] = new JLabel("6.Possible Solutions and Petential Trade-offs");
        labels[8] = new JLabel("7.Relationship to Other Usability Aspects");
        labels[9] = new JLabel("Enter a number:");
        labels[10] = new JLabel("Enter a search string:");
        labels[11] = new JLabel("Found at:");
        labels[12] = new JLabel();
        labels[13] = new JLabel();
		
        for(int i=0; i<2; i++) 
            labels[i].setFont(new Font("Arial", 1, 13));
        for(int i=2; i<labels.length; i++) 
            labels[i].setFont(new Font("宋体", 0, 12));
		
        txtArea = new JTextArea();
        txtArea.setLineWrap(true);
        txtArea.setWrapStyleWord(true);
        jspForTxtArea = new JScrollPane(txtArea);
        jspForTxtArea.setAutoscrolls(true);
		
        txtField1 = new JTextField(6);
        txtField2 = new JTextField(6);
		
        txtField1.getDocument().addDocumentListener(
            new DocumentListener(){
                public void changedUpdate(DocumentEvent e) {
                    insertUpdate(e);
                }
                public void insertUpdate(DocumentEvent e) {
                    txtArea.setText("");
                }
                public void removeUpdate(DocumentEvent e) {
         	    insertUpdate(e);
                }
            });
		
        buttons = new MyButton[3];
		
        buttons[0] = new DisplayButton();
        buttons[1] = new SearchButton();
        buttons[2] = new ExitButton();
        buttons[0].setText("Display");
        buttons[1].setText("Search");
        buttons[2].setText("Exit");
		
        for(int i=0; i<buttons.length; i++) 
            buttons[i].addActionListener(new ProcessActions());
    }
	
    /**
     * 界面上所有按钮的父类,继承了JButton
     */
    private class MyButton extends JButton {
        public void actionPerformed() {
        System.out.println("Haven't you overwrite \"actionPerformed\" method?");
        }
    }
	
    private class DisplayButton extends MyButton {
        public void actionPerformed() {
            String strchosen = txtField1.getText();
            int intchosen = 0;
            try{
                intchosen = Integer.parseInt(strchosen);
            } catch (Exception e) {
            }
            if (intchosen > 0 && intchosen < 8) {
                txtArea.setText(text[intchosen-1]);
            } else {
                javax.swing.JOptionPane.showMessageDialog(null, 
                    "Please enter value between 1 and 7");
                txtField1.setText("");
                return;
            }
        }	
     }
	
     private class SearchButton extends MyButton {
        public void actionPerformed() {
            String str = txtArea.getText();
            if ( str.equals("") ) {
	javax.swing.JOptionPane.showMessageDialog
                    (null, "Please select text");return;
            }
            String pattern = txtField2.getText();
            if ( pattern.equals("") ) {
                javax.swing.JOptionPane.showMessageDialog
                   (null, "Please enter a search string");return;
            }
            int first=0, end=0, index, count=0;
            boolean flag = true;
			
            while(true) {
                System.out.println("===="+count);
                index = str.indexOf(pattern);
                if (index==-1) break;
                end = index;
                count++;
                if ( flag ) {
                    first = end;
                    flag = false;
                }
                str = str.substring(index+pattern.length());
            }
			
            int intconfirm=0;
            if (count==0) {
                intconfirm=javax.swing.JOptionPane.showConfirmDialog
                (null, "String '"+pattern+"' not found\r\nSearch same text again?");
            } else if (count==1) {
                intconfirm=javax.swing.JOptionPane.showConfirmDialog
                (null, "The number of occurences of '"+pattern+"' is "+count+"\r\nSearch same text?");
                labels[12].setText("Occurence "+1+" : Position : " + first);
            } else {
                intconfirm=javax.swing.JOptionPane.showConfirmDialog
                (null, "The number of occurences of '"+pattern+"' is "+count+"\r\nSearch same text?");	
                labels[12].setText("Occurence "+1+" : Position : " + first);
                labels[13].setText("Occurence "+count+" : Position : " + end);
           }
			
           if (intconfirm==1) {
	txtArea.setText("");
	txtField1.setText("");
           }
           txtField2.setText("");
        }
    }
	
    private class ExitButton extends MyButton {
        public void actionPerformed() {
             System.exit(0);
        }
    }

    private class ProcessActions implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            MyButton button = (MyButton) e.getSource();
            /**
             * 当添加按钮组件时,不需要修改这部分代码
         */
            for(int i=0;i<buttons.length;i++) {
                if(button==buttons[i]) {
                    button.actionPerformed();
                }
            }
        }
    }

    private void setBoundsForAll() {
        int xl=this.getWidth()/2-20, yl=20, x=20, y=8;
        labels[0].setBounds(x, y, xl, yl);
        labels[1].setBounds(xl+36, y, xl, yl);
		
        y+=25;
        for(int i=2; i<9; i++) 
             labels[i].setBounds(x, y+28*(i-2), xl, yl);
		
        x=xl+33;
        jspForTxtArea.setBounds(x, y+3, xl-50, 216);
        labels[11].setBounds(x, y+230, xl, 20);
	
        x=20;xl=166;y=labels[8].getY()+103;
        labels[9].setBounds(x, y, xl, yl);
        labels[10].setBounds(x, y+40, xl, yl);
		
        x+=xl;y-=2;xl=136;yl+=4;
        txtField1.setBounds(x, y, xl, yl);
        txtField2.setBounds(x, y+40, xl, yl);
		
        x+=(xl+10);y-=5;xl=80;yl=30;
        buttons[0].setBounds(x, y, xl, yl);
        buttons[1].setBounds(x, y+40, xl, yl);
		
        xl=this.getWidth()/2-20;x=xl+33;y+=5;
        labels[12].setBounds(x, y, xl, 20);
        labels[13].setBounds(x, y+40, xl, 20);
		
        buttons[2].setBounds(this.getWidth()-120, this.getHeight()-78, 80, 30);
    }

    public MainFrame() {
        super("UAR Components");
        this.init();
        this.setSize(880,500);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setContentPane(getContentPane(new JPanel()));
        this.setVisible(true);
    }
    
    public Container getContentPane(Container pane) {
        pane.setLayout(null);
        for(int i=0;i<labels.length;i++) pane.add(labels[i]);
        for(int i=0;i<buttons.length;i++) pane.add(buttons[i]);
        pane.add(jspForTxtArea);
        pane.add(txtField1);
        pane.add(txtField2);
        setBoundsForAll();
        return pane;
    }
    
}
   

你可能感兴趣的:(swing,Go)