zoukankan      html  css  js  c++  java
  • JAVA的Swing实现点击事件

    MyFrame.java

    package swing;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class MyFrame extends JFrame {
        JLabel timeLabel= new JLabel("00:00:00");
        JButton button=new JButton("显示时间");
    
        public MyFrame(String title){
            super(title);
            //内容面板
            Container contentPane=getContentPane();
            contentPane.setLayout(new FlowLayout());
    
            //向内容面板添加控件
            contentPane.add(button);
            contentPane.add(timeLabel);
            //创造监听器对象
            //把监听器注册给按钮
            button.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    //当按钮被点击时,Swing框架会调用监听器的actionPerformed()方法
                    System.out.println("按钮被点击....");
                    showTime();
                }
            });
    
        }
        public void showTime(){
            SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
            String timestr=sdf.format(new Date());
            timeLabel.setText(timestr);
            System.out.println("时间已更新");
        }
    }
    package swing;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class MyFrame extends JFrame {
        JLabel timeLabel= new JLabel("00:00:00");
        JButton button=new JButton("显示时间");
    
        public MyFrame(String title){
            super(title);
            //内容面板
            Container contentPane=getContentPane();
            contentPane.setLayout(new FlowLayout());
    
            //向内容面板添加控件
            contentPane.add(button);
            contentPane.add(timeLabel);
            //创造监听器对象
            //把监听器注册给按钮
            button.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    //当按钮被点击时,Swing框架会调用监听器的actionPerformed()方法
                    System.out.println("按钮被点击....");
                    showTime();
                }
            });
    
        }
        public void showTime(){
            SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
            String timestr=sdf.format(new Date());
            timeLabel.setText(timestr);
            System.out.println("时间已更新");
        }
    }
    SwingDemo.java
    package swing;
    
    import javax.swing.*;
    
    public class SwingDemo {
        private static void createGUI(){
    
            //JFrame指一个窗口,构造方法的参数为窗口标题
            MyFrame frame=new MyFrame("swing demo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //设置窗口的其它参数,如窗口大小
            frame.setSize(400,300);
    
            //显示窗口
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    createGUI();
                }
            });
        }
    }
  • 相关阅读:
    求列表中指定元素的位置
    Hash_P1026毒药?解药?
    Hash_集合
    bzoj1483: [HNOI2009]梦幻布丁
    bzoj1724: [Usaco2006 Nov]Fence Repair 切割木板
    容斥原理
    bzoj1042: [HAOI2008]硬币购物
    [Noi2016十连测第五场]二进制的世界
    NOI2016模拟赛Zbox loves stack
    bzoj2038: [2009国家集训队]小Z的袜子(hose)
  • 原文地址:https://www.cnblogs.com/yeyueweiliang/p/13644033.html
Copyright © 2011-2022 走看看