您的位置:首页 > 其它

button 事件的三种处理方式

2014-07-31 15:35 302 查看
事件监听器放在外面整合成一个类

/**

*

*/

package ch13;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

/**

* @author jcuckoo

*

* 2014-7-31 下午2:57:12

*/

public class ListenerTest extends JFrame {

JButton button;

JButton btnAddStudent;

JButton btnDelStudent;

public ListenerTest(){

//1.事件源

button=new JButton("事件测试");

//4.事件注册

//button.addActionListener(new ButtonActionListener());

ButtonActionListener baListener=new ButtonActionListener();

button.addActionListener(baListener);

//5.事件的发生,鼠标点击按钮时

btnAddStudent=new JButton("保存学生的信息");

btnAddStudent.addActionListener(baListener);

add(button);

add(btnAddStudent);

setLayout(new FlowLayout());

this.setSize(300,300);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

//内部类

//2.事件监听器

class ButtonActionListener implements ActionListener{

@Override

public void actionPerformed(ActionEvent e) {

//3.事件处理程序

//JOptionPane.showMessageDialog(ListenerTest.this, "事件测试","操作提醒",JOptionPane.ERROR_MESSAGE);

String btnName=e.getActionCommand();

if(btnName.equals("事件测试")){

JOptionPane.showMessageDialog(ListenerTest.this, "测试成功",btnName,JOptionPane.ERROR_MESSAGE);

}

if(btnName=="保存学生的信息"){

JOptionPane.showMessageDialog(ListenerTest.this, "保存成功",btnName,JOptionPane.ERROR_MESSAGE);

}

}

}

public static void main(String[] args) {

ListenerTest test=new ListenerTest();

}

}

--------------------------------------------------------

2.定义一个类extends JFrame implements ActionListener

/**

*

*/

package ch13;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

/**

* @author jcuckoo

*

* 2014-7-31 下午3:15:28

*/

public class TestListener extends JFrame implements ActionListener {

JButton button;

public TestListener(){

//1.事件源

button=new JButton("事件测试");

button.addActionListener(this);

add(button);

setLayout(new FlowLayout());

this.setSize(300,300);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

@Override

public void actionPerformed(ActionEvent e) {

JOptionPane.showMessageDialog(TestListener.this, "事件测试","操作提醒",JOptionPane.ERROR_MESSAGE);

}

/**

* @param args

*/

public static void main(String[] args) {

TestListener test=new TestListener();

}

}

-------------------------------------------------------------------

3.将运行事件写在内部,最实用。

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JTextField;

/**

* @author jcuckoo

*

* 2014-7-31 上午11:24:03

*/

public class FileSender extends JFrame {

public FileSender(){

this.setLayout(null);

JButton button=new JButton("选择文件");

button.setBounds(30, 30, 100, 50);

final JTextField fileName=new JTextField();

fileName.setBounds(200,30,300,50);

this.add(fileName);

this.add(button);

button.addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

JFileChooser jfc=new JFileChooser();

jfc.showOpenDialog(FileSender.this);

File file=jfc.getSelectedFile();

jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );

String fileNameStr=file.getAbsolutePath();

JOptionPane.showMessageDialog(FileSender.this, fileNameStr);

fileName.setText(fileNameStr);

}

});

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

/*JFileChooser jfc=new JFileChooser();

jfc.setBounds(30, 100, 400, 400);

this.add(jfc);*/

this.setSize(600, 600);

this.setVisible(true);

}

public static void main(String[] args) {

FileSender sender=new FileSender();

}

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