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();
}
}
/**
*
*/
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();
}
}
相关文章推荐
- 以SayHello为例说明三种事件处理方式
- android 触发Button按钮事件的三种方式
- Button点击事件的三种实现方式
- JavaScript事件处理的方式(三种)
- 触发Button按钮事件的三种方式
- JavaScript事件处理的方式(三种)
- JavaScript学习笔记之事件处理程序的三种方式
- 从零开始学Android(二):利用“打招呼”来说明按钮点击事件的三种处理方式
- Flex三种事件处理方式
- Android之Button按钮点击事件的三种方式
- Android 触发Button按钮事件的三种方式
- 设置Button监听事件的三种方式
- Swing中添加事件响应的三种处理方式说明
- Adobe Flex 三种方式处理事件
- Swing中添加事件响应的三种处理方式
- JAVA事件的三种处理方式
- 安卓中关于事件处理方式以Button为例实现点击事件监听
- 以SayHello为例演示事件处理的三种方式
- Android之Button点击事件处理的几种方式
- android studio教程 button 用匿名内部类的方式实现事件处理