java中的gui实现事件监听

1、当需要按钮被按下时执行一定的任务,就要向按钮注册动作事件监听器,并为按钮编写动作事件处理代码,即编写实现ActionLisener监听接口的监听器类,并实现actionPerforned方法。设计一个Java GUI应用程序,当点击按钮时,记录点击按钮和点击次数,并显示在窗体中。

/*1、当需要按钮被按下时执行一定的任务,就要向按钮注册动作事件监听器,

并为按钮编写动作事件处理代码,即编写实现ActionLisener监听接口的监听器类,

并实现actionPerforned方法。设计一个Java GUI应用程序,当点击按钮时,

记录点击按钮和点击次数,并显示在窗体中。*/

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

public classGetClickCountTest {

    public static void main(String arg[]){

        new GetClickCountFrame();

       

    }

}

class GetClickCountFrame extends JFrame implements ActionListener{

    int count = 0;

    int count1 = 0;

    private JButton b1 = new JButton("按钮1");

   

    private JButton b2 = new JButton("按钮2");

    private JButton b3 = new JButton("按钮3");

    private JLabel l1 = new JLabel("显示点击次数");

    private JTextField t = new JTextField(20);

    public GetClickCountFrame(){

        JFramef = newJFrame();

        JPanelp1 = newJPanel();

        JPanelp2 = newJPanel();

        t.setEditable(false);

        //JLabel l2 = new JLabel();    

        b1.addActionListener(this);    

        b2.addActionListener(this);

        b3.addActionListener(this);

        p1.add(l1);

        p1.add(t);

        p2.add(b1);

        p2.add(b2);

        p2.add(b3);

        f.getContentPane().add(p1,BorderLayout.CENTER);

        f.getContentPane().add(p2,BorderLayout.SOUTH);

        f.setSize(500,300);

        f.setTitle("测试点击次数");

        f.setDefaultCloseOperation(EXIT_ON_CLOSE);

        f.setVisible(true);

    }

 

    @Override

    public voidactionPerformed(ActionEvent e) {

        // TODO Auto-generated method stub

        Objectsource = e.getSource();

   

        if(source==b1){

           

            count  = count+1;

            t.setText("按钮1点击了"+count+"");

        }

        if(source==b2){

            count1 = count1+1;

            t.setText("按钮2点击了"+count1+"");

        }

        if(source==b3){

            t.setText("");

            count=0;

            count1=0;

        }

   

    }

}


  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java GUI 事件监听可以通过添加事件监听器来实现。使用事件监听器,您可以检测用户在 GUI 组件上执行的操作,例如单击按钮或更改文本字段的值。 以下是 Java GUI 事件监听的基本步骤: 1. 创建需要添加事件监听器的 GUI 组件。 2. 创建一个事件监听器类,该类应实现所需的事件监听器接口,例如 ActionListener、MouseListener 或 KeyListener。 3. 在 GUI 组件上添加事件监听器,使用 addEventListener()或 addActionListener()等事件处理方法。 4. 在事件监听器类编写事件处理代码,以响应用户在 GUI 组件上执行的操作。 以下是一个简单的示例,展示如何在 Java GUI 实现事件监听: ```java import javax.swing.*; import java.awt.event.*; public class EventListenerExample implements ActionListener { JFrame frame; JButton button; public EventListenerExample() { frame = new JFrame("Event Listener Example"); button = new JButton("Click me"); button.addActionListener(this); frame.add(button); frame.pack(); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { JOptionPane.showMessageDialog(frame, "Button clicked!"); } } public static void main(String[] args) { new EventListenerExample(); } } ``` 在此示例,我们创建了一个 JFrame 和一个 JButton。我们将 ActionListener 接口实现添加到 JButton 上,并使用 addActionListener() 方法将其添加到 JButton 上。在 actionPerformed() 方法,我们编写了响应用户单击按钮时要执行的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值