首页 > 代码库 > 初学java之触发响应事件举例子

初学java之触发响应事件举例子

设置一个触发响应事件?

  比如消息框.....

 1 package hello;
 2 import javax.swing.*;
 3 import project.readerListen;
 4 import java.awt.*;
 5 import java.awt.event.*;
 6 class WindGrid extends  JFrame{
 7     
 8     JTextField text;
 9     ActionListener listener;    
10     
11     public WindGrid(String title)
12     {
13       setTitle(title);
14       init();
15       setBounds(40,50,260,270);
16       setVisible(true);    
17       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
18     }    
19     
20     void init()
21     {
22       text = new JTextField(10);
23       listener = new ReaderListen();  //自定义的 那个类
24       setLayout(new FlowLayout());
25       text.addActionListener(listener);
26       add(new Label("输入蚊子 "));
27       add(text);
28     }
29 
30 }
31 public class test {
32     public static void main(String args[])
33     {
34       new WindGrid("第一个窗口");    
35     }
36 }
View Code
 1 package hello;
 2 
 3 import java.awt.event.ActionEvent;
 4 import java.awt.event.ActionListener;
 5 import javax.swing.*;
 6 public class ReaderListen extends JFrame implements ActionListener 
 7 {
 8 
 9     public void actionPerformed(ActionEvent e)
10     {
11     // TODO Auto-generated method stub    
12         JFrame myjframe;
13         myjframe = new JFrame();
14         JTextArea mytest;
15         mytest =new JTextArea(e.getActionCommand(),8,9);
16         myjframe.add(mytest);
17         myjframe.setBounds(20, 30, 200, 300);
18         myjframe.setVisible(true);
19         
20     }
21 }
View Code