Java中为按钮添加监听器(事件处理)四种形式总结

您所在的位置:网站首页 java界面设置功能 Java中为按钮添加监听器(事件处理)四种形式总结

Java中为按钮添加监听器(事件处理)四种形式总结

2023-09-20 05:27| 来源: 网络整理| 查看: 265

      以下的示例程序是要在一个面板中添加三个按钮,并分别添加三个监听器对象用来作为按钮的动作监听器,当点击不同的按钮时使得面板的背景色变成相应的颜色:

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonFrame{ public static void main(String[] args){     EventQueue.invokeLater(() -> {         JFrame frame= new CreatButtonFrame();         frame.setTitle("ButtonTest");         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         frame.setVisible(true);}); } } class CreatButtonFrame extends JFrame{ private JPanel buttonPanel; private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; public CreatButtonFrame(){ setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); JButton yellowButton = new JButton("Yellow"); JButton blueButton = new JButton("Blue"); JButton redButton = new JButton("Red"); buttonPanel = new JPanel(); buttonPanel.add(yellowButton); buttonPanel.add(blueButton); buttonPanel.add(redButton); add(buttonPanel); ColorAction yellowAction = new ColorAction(Color.YELLOW); ColorAction blueAction = new ColorAction(Color.BLUE); ColorAction redAction = new ColorAction(Color.RED); yellowButton.addActionListener(yellowAction); blueButton.addActionListener(blueAction); redButton.addActionListener(redAction); } class ColorAction implements ActionListener{ private Color backgroundColor; public ColorAction(Color c){ backgroundColor = c; } public void actionPerformed(ActionEvent event){ buttonPanel.setBackground(backgroundColor); } } }

      以上程序没必要单独为事件监听器定义一个类并构造类的3个对象,因此,可以使用lambda表达式简化如下:

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonFrame{ public static void main(String[] args){ EventQueue.invokeLater(() -> { JFrame frame= new CreatButtonFrame(); frame.setTitle("ButtonTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);}); } } class CreatButtonFrame extends JFrame{ private JPanel buttonPanel; private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; public CreatButtonFrame(){ setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); buttonPanel = new JPanel(); makeButton("yellow",Color.yellow); makeButton("blue",Color.blue); makeButton("red",Color.red); add(buttonPanel); } public void makeButton(String name, Color backgroundColor) { JButton button = new JButton(name); buttonPanel.add(button); button.addActionListener(event -> buttonPanel.setBackground(backgroundColor)); } }

      此外,当我们不习惯于使用lambda表达式,而是更喜欢创建实现了ActionListener接口的事件源容器,然后这个容器再设置自身作为监听器,则可以修改为如下的形式:

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonFrame{ public static void main(String[] args){ EventQueue.invokeLater(() -> { JFrame frame= new CreatButtonFrame(); frame.setTitle("ButtonTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);}); } } class CreatButtonFrame extends JFrame implements ActionListener{ private JPanel buttonPanel; private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; private Button yellowButton,blueButton,redButton; public CreatButtonFrame(){ setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); buttonPanel = new JPanel(); yellowButton=new Button("yellow"); blueButton=new Button("blue"); redButton=new Button("red"); yellowButton.addActionListener(this); blueButton.addActionListener(this); redButton.addActionListener(this); buttonPanel.add(yellowButton); buttonPanel.add(blueButton); buttonPanel.add(redButton); add(buttonPanel); } public void actionPerformed(ActionEvent event) { Object source=event.getSource(); if(source==yellowButton) { buttonPanel.setBackground(Color.yellow); }else if(source==blueButton) { buttonPanel.setBackground(Color.blue); }else if(source==redButton){ buttonPanel.setBackground(Color.red); } } }

      此外,还可以采用一种机制指定事件监听器,其事件处理器包含一个方法调用。例如当一个按钮监听器需要执行调用:

frame.doMethod();

可以通过EventHandler类创建一个监听器:

EventHandler.create(ActionListener.class,frame,"doMethod")

但利用lambda表达式更容易实现:

event -> frame.doMethod();

因此,这种通过EventHandler类创建监听器的方法不常用。该方法的完整示例如下,该示例在点击按钮“Click Here”后会打印“Hello World!”的消息:

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.beans.*; public class Buttonhand extends JFrame{ public static void main(String args[]){ JFrame frame = new Buttonhand(); frame.setSize(300, 400); frame.setVisible(true); } public Buttonhand(){ super("事件处理"); setDefaultCloseOperation(EXIT_ON_CLOSE); JButton button = new JButton("Click Here"); Container contentPane = getContentPane(); contentPane.add(button, BorderLayout.CENTER); button.addActionListener( (ActionListener)EventHandler.create( ActionListener.class, this, "print") ); } public void print(){ System.out.println("Hello World!"); } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3