`
C_SHaDow
  • 浏览: 51601 次
  • 性别: Icon_minigender_1
  • 来自: 大同
社区版块
存档分类
最新评论

继承的例子

阅读更多

最近学校留作业,因为用不惯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;
    }
    
}
   
0
0
分享到:
评论

相关推荐

    经典java继承例子

    在这个经典的Java继承例子中,我们将深入理解这一机制。 首先,我们要明确的是,继承的主要目的是减少代码冗余,提高代码的可维护性和可扩展性。在Java中,我们使用`extends`关键字来表示一个类继承自另一个类。...

    VC类继承例子

    "VC类继承例子"这一主题将深入探讨如何在Visual C++中利用继承来设计更加灵活和可扩展的软件结构。 首先,我们需要理解继承的基本概念。继承允许我们定义一个基类,其中包含了一些通用的属性和方法,然后创建一个或...

    Java继承例子.doc

    Java继承例子.doc

    hibenate继承例子

    在这个"hibernate继承例子"中,我们将探讨如何利用Hibernate处理对象的继承关系,并将这种继承映射到数据库中的表结构。 在Java中,我们可以有单继承或多层继承的类结构。同样,Hibernate提供了一种方式,通过继承...

    继承窗体的设计C#源码

    在C#编程中,"继承窗体的设计"是面向对象编程的一个重要概念,它允许我们创建一个新的窗体类,该类基于已有的窗体类,从而可以复用和扩展功能。这种方式大大提高了代码的可重用性和可维护性。本文将深入探讨C#中继承...

    C++虚拟继承举例及其代码

    通过一个具体的例子,我们将帮助初学者更好地理解和体会虚拟继承的工作原理。 首先,我们需要理解什么是继承。继承是面向对象编程的一个核心特性,它允许一个类(子类或派生类)从另一个类(父类或基类)中获取属性...

    java中继承的使用案例

    继承的使用 (1)声明一个人类Person,放入以你的姓名为包名的包中,姓名为private类型属性,性别为protected类型变量,年龄为默认访问类型变量;该类有两个构造方法Person()和Person(姓名,性别,年龄),构造...

    C++全套学习课件继承和派生(例子).pdf

    1. 单一继承例子 在单一继承的例子中,我们定义了一个基类Point,它具有x和y两个私有成员变量,用于表示二维空间中的一个点,以及它们的获取和设置函数。还有一个派生类Circle,它继承自Point类,并增加了一个半径...

    继承树.java

    对java中继承的练习,一个简单的例子,通过描述大树的性质来进行理解。

    继承的小例子

    标题"继承的小例子"暗示我们将讨论关于Java继承的一些基础实例。 首先,让我们深入理解Java继承的概念。当一个子类继承一个父类时,子类就自动获得了父类的所有非私有(public和protected)成员,包括字段和方法。...

    C++继承与多态例子

    在这个例子中,`Dog`类继承了`Animal`类,并添加了一个新的成员函数`bark()`。由于继承,`Dog`类的对象可以调用`eat()`方法,这是从`Animal`类继承而来的。 接下来,我们来谈谈多态。多态有两种类型:静态多态...

    java_中关于_继承_的一个小例子.

    这个小例子可能是为了演示如何在Java中实现继承以及它所带来的优势。 首先,我们来理解继承的基本概念。当我们说一个类B继承自类A,我们可以表示为`class B extends A`。这意味着B类自动拥有了A类的所有非私有...

    11.5_ 接口与继承的实例,VS2008源代码

    在提供的"接口继承例子"中,可能包含了以下几种示例: 1. **单一继承**:一个子类只继承一个父类,展示如何复用和扩展父类的功能。 2. **多重继承**:虽然C#不支持类的多重继承,但它允许一个类实现多个接口,这里...

    浅析Javascript原型继承 推荐第1/2页

    现在让我们来分析一个具体的原型继承例子: ```javascript function Gizmo(id) { this.id = id; } Gizmo.prototype.toString = function() { return "gizmo" + this.id; }; function Hoozit(id) { this.id = id...

    Java编程实例集:类和继承(中文注释).rar

    收集的一些Java编程实例集:类和继承,含有丰富的中文注释,内容大致是:定义 Point类、定义形状接口、循环输出下转形状并打英创建自己异常、使用继承 (实例一个父类定义到子类)子类赋值把一个点定义创建圆对象定义...

    js模拟类继承小例子

    总结,这个例子展示了如何利用JavaScript的原型链来实现类继承,包括构造函数的调用、方法的重写和扩展,以及实例化和类型检查等概念。通过这种方式,我们可以创建一个类层次结构,使得子类可以继承并扩展父类的功能...

    多态继承接口结构例子

    在C#编程语言中,多态(Polymorphism)、继承(Inheritance)和接口(Interface)是面向对象编程的三大核心特性。结构(Struct)则是C#中的一个基本类型,与类(Class)有所不同。本篇文章将深入探讨这些概念,并...

    范磊 c++从零开始第十二章 继承 doc课件

    "单一继承例子.cpp"可能包含这样的示例代码,演示如何创建一个从单一基类继承的子类,并展示继承的特性。在这个过程中,你可能会学到如何使用构造函数初始化列表来正确地初始化基类的成员。 文档"第十二章 继承.doc...

    InheritanceTest:关于公司的继承例子

    这个"InheritanceTest"例子可能通过具体的类和方法展示了上述一些概念,帮助开发者更好地理解和应用Java中的继承特性。通过分析源代码,我们可以深入理解如何在实际项目中利用继承来构建灵活和可扩展的软件架构。

    C++中公有继承、私有继承、保护继承.的含义及例子

    C++中public继承、private继承、protected继承的例子

Global site tag (gtag.js) - Google Analytics