Newer
Older
ASD2024 / FirstGUI.java
@Your Name Your Name on 8 Oct 2 KB lambda formula
import javax.swing.JPanel;
import javax.swing.JFrame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;

public class FirstGUI extends JPanel implements ActionListener, WindowListener, Runnable {
    JFrame frame;
    JButton button1, button2;
    int seconds = 0; //秒数
    public static void main(String[] args) {
        new FirstGUI();
    }
    public FirstGUI(){
        frame = new JFrame("FirstGUI");
        frame.setSize(300,200);
        frame.getContentPane().add(this);
        this.add(button1 = new JButton("Button"));
        this.add(button2 = new JButton("Button2"));
        button1.addActionListener(this);
        button1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.out.println(e.getActionCommand()
                );
            }
        });
        button2.addActionListener(e -> { System.out.println(e.getActionCommand()); } ); 
        frame.addWindowListener(this);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
        Thread thread = new Thread(this);
        thread.start();
    }
    @Override
    public void windowOpened(WindowEvent e) {
    }
    @Override
    public void windowClosing(WindowEvent e) {
        java.awt.Toolkit.getDefaultToolkit().beep();
    }
    @Override
    public void windowClosed(WindowEvent e) {
    }
    @Override
    public void windowIconified(WindowEvent e) {
    }
    @Override
    public void windowDeiconified(WindowEvent e) {
    }
    @Override
    public void windowActivated(WindowEvent e) {
    }
    @Override
    public void windowDeactivated(WindowEvent e) {
    }
    @Override
    public void run() {
        for(int i=0; i< 10;i++){
            System.out.println(i+" sec");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}