您的位置:首页 > 编程语言 > Java开发

java窗口监听器的简单例子

2014-04-03 21:46 417 查看
  package javaFrame;

  import java.awt.*;

  import javax.swing.*;

  import java.awt.event.*;

  import java.awt.FlowLayout;

  public class FrameEx2 implements ActionListener{

  JFrame jf;

  JPanel jp;

  JButton button1,button2;

  TextArea tf1;

  public FrameEx2(){

  jf = new JFrame("小小菜");

  button1 = new JButton("我得");

  button2 = new JButton("你得");

  Label label1 = new Label("监听器听到");

  tf1 = new TextArea();

  jp = new JPanel();

  jf.setVisible(true);

  button1.addActionListener(new secondbutton());

  button2.addActionListener(new firstbutton());

  jp.add(button1);

  jp.add(button2);

  jf. setLayout(new GridLayout(5,1));

  jf.add(label1);

  jf.add(tf1);

  jf.add(jp);

  jf.setSize(500,300);

  }

  public static void main(String[] args) {

  FrameEx2 action = new FrameEx2();

  }

  public void actionPerformed(ActionEvent e) {

  }

  class secondbutton implements ActionListener{

  public void actionPerformed(ActionEvent e) {

  tf1.append("这是左边,"+e.getActionCommand()+"\n\n");

  }

  }

  class firstbutton implements ActionListener{

  public void actionPerformed(ActionEvent e) {

  tf1.append("这是右边,"+e.getActionCommand()+"\n\n");

  }

  }

  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: