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();
            }
        });
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM