Java 基础【04】Swing 组件事件注册

    聪明出于勤奋,天才在于积累。——华罗庚

   对上次的三个问题的个人理解: 

   1) 程序首先是从main函数开始执行的,假设main 函数不是 static ,就要先实例化这个类,然后调用 main 方法,这似乎是不现实的. 其次 用 static 修饰的 main 方法是存储在静态的存贮区当中的,也就是说在创建一个类之后,main 函数就已经存在了,去掉 static 修饰之后,编译可以通过,但是不能执行。
   2)查 API之后才发现BufferedRead 对象的 readLine()方读到的数据是,读到有换行的地方为一行,直接用 readLine 判断的时候已经读入一行了,在输出数据时就会隔行输出。

 FileReader file=new FileReader("C:\\123.txt");
            BufferedReader br1=new BufferedReader(file);
                       //判断的时候已经读入一行
            while((br1.readLine())!=null)
            {   //输出的是第二行的内容
                System.out.println(br1.readLine());
            }

所以用一个临时的字符串变量来存储读到的数据,程序改改这样就可以了:

    FileReader file=new FileReader("C:\\123.txt");
            BufferedReader br1=new BufferedReader(file);
            
            String cd;
            while((cd=br1.readLine())!=null)
            {
                System.out.println(cd);
            }

3)如果将客户端、输入流、输出流的初始化全部放进 Send 按钮的事件当中时,程序会达到我想要的效果,点击连接之后就会有客户端连接上去,但总觉得这样会有其他的安全隐患,总有一天它会暴漏的。

     今天要记录在这里的是老师随堂布置的一个小程序,实现一个计算器的雏形,里面只有加减运算,对其中的按钮注册有了一点新的认识,还是将代码贴出来先。

  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*; 


public class ComboBoxTest extends JFrame{     
      private JButton done =new JButton(" Done ");
      private JButton clear=new JButton(" Clear ");      
      private JLabel  label = new JLabel("Please choose serverID:0(+)and1(-)");      
      
 public  ComboBoxTest(){     
     //添加一个组合框并设置两个选项
      final JComboBox c = new JComboBox();
      int [] array = {0,1};
      c.addItem(array[0]);
      c.addItem(array[1]);

      final JTextField operand1=new JTextField(10);      //添加第一个操作数为输入文本框,占8列
      final JLabel t=new JLabel("+");                     //初始化中间的操作符为“+”号
      final JTextField operand2=new JTextField(10);      //第二个操作符
      final JTextField result=new JTextField(4);         //结果的文本域 ,初始化占4列
      
      //给组合框c注册一个事件,当组合框选项发生变化的时候,触发的相应事件
      c.addActionListener(new ActionListener() {      
          public void actionPerformed(ActionEvent e) {
              if(c.getSelectedIndex()==0)   //选项为“0”的时候 令中间的操作符显示“+”号
                t.setText(" + ");           
               else  t.setText(" - ");        
         }
        });
      //给按钮Done注册一个事件,当中间的操作符不同时进行不同的操作
       done.addActionListener(new ActionListener(){   
            public void actionPerformed(ActionEvent e) {
                if(c.getSelectedIndex()==0)   
                {
                    //当中间的操作符为“+”号时,进行两个操作数的加法 ,文本域的get()方法返回的是字符串,进行强制转换
                     int a=Integer.parseInt(operand1.getText())+Integer.parseInt(operand2.getText());                     
                     result.setText("="+" "+a+" ");  //设置结果显示相应的结果
                   }          
              else {
                  //当中间的操作符为“-”号的时候,进行两个操作数的减法
                int a=Integer.parseInt(operand1.getText())-Integer.parseInt(operand2.getText());                     
                result.setText("="+" "+a+" ");
              }    
            }              
          });
    // 给按钮clear注册一个事件,清空两个操作数和结果的内容
     clear.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {            
            operand1.setText("");    //清空操作数1
            operand2.setText("");    //清空操作数2
            result.setText("");      //清空结果框
            }               
          });    
      setLayout(new FlowLayout());
      add(label);                   
      add(c);
      add(operand1);
      add(t);
      add(operand2);
      add(result);
      add(done);
      add(clear); 
      setSize(350,140);  
      setVisible(true);   
     }
 
   public static void main(String[] args) {
       new ComboBoxTest(); 
         }
}

上面的代码中给选项框、“done”、"clear"按钮注册事件的时候所用的都是匿名类,这个类的创建就是为了给相应的组件添加事件,还可以这样写,用里面的“clear”这个按钮来做个例子。

实现 ActionListener 抽象类当中的唯一的一个接口函数,为此定义一个 ButtonListener 监听器的对象

class ButtonListener implements ActionListener{
       public void actionPerformed(ActionEvent e){
            operand1.setText("");    //清空操作数1
            operand2.setText("");    //清空操作数2
            result.setText("");      //清空结果框             
       }
   }

类属性当中需要定义一个 ButtonListener 的对象属性:

private ButtonListener clearaction = new ButtonListener();

最后一个步骤就是将这个按钮监听器的事件对象注册给按钮:

clear.addActionListener(clearaction);

  个人总结:

    这一种注册事件的方式大致过程是这样的 ButtonListener =》 ActionListener => 注册给按钮,和匿名类相比,缺点是代码量有点多,但假设你有N个打算具备这种

功能的按钮并且事件实现的方法比较复杂时,就可以实现一个 ActionListener 的对象,同时定义N个 ButtonListener 监听器对象,将相同的事件实现注册给按钮就可以了,相比之下匿名类在这种情形下面会有很大的工作量,代码量会激增。

     还可以通过事件 e.getSource()方法将所有的事件处理放进一个函数当中,这样似乎维护起来要更方便一点,在类的声明当中要强调实现接口中的抽象函数。

public class ComboBoxTest extends JFrame implements ActionListener

 具体的实现过程如下:

  public void actionPerformed(ActionEvent e){
       if(e.getSource()==c){
              if(c.getSelectedIndex()==0)   //选项为“0”的时候 令中间的操作符显示“+”号
                    t.setText(" + ");           
                    else  t.setText(" - ");     
       }
       
       if(e.getSource()==done){
            if(c.getSelectedIndex()==0)   
            {
                //当中间的操作符为“+”号时,进行两个操作数的加法 ,文本域的get()方法返回的是字符串,进行强制转换
                 int a=Integer.parseInt(operand1.getText())+Integer.parseInt(operand2.getText());                     
                 result.setText("="+" "+a+" ");  //设置结果显示相应的结果
               }          
          else {
              //当中间的操作符为“-”号的时候,进行两个操作数的减法
            int a=Integer.parseInt(operand1.getText())-Integer.parseInt(operand2.getText());                     
         result.setText("="+" "+a+" ");
          }      
       }       
       if(e.getSource()==clear){
           operand1.setText("");    //清空操作数1
            operand2.setText("");    //清空操作数2
            result.setText("");      //清空结果框    
       }

 

posted @ 2012-12-11 20:49  Orson  阅读(4681)  评论(1编辑  收藏  举报