⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swingapp2.java

📁 Java样例程序集合:2D
💻 JAVA
字号:
/*
 * This is a 1.3 version (thanks to JFrame.EXIT_ON_CLOSE)
 * but could easily be converted to work in earlier releases.
 * There are many valid ways to implement the exercise.
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SwingApp2 {
    JButton startButton, stopButton;
    JLabel label;

    public SwingApp2() {
        JFrame f = new JFrame("SwingApp2");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //1.3+
        createGUI(f);
        initListeners();
        f.pack();
        f.setVisible(true);
    }

    void initListeners() {
        ActionListener al = new MyActionListener();
        startButton.addActionListener(al);
        stopButton.addActionListener(al);
    }

    class MyActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == startButton) {
                startButton.setEnabled(false);
                stopButton.setEnabled(true);
                label.setText("Press Stop.");
            } else {
                // e.getSource() == stopButton
                stopButton.setEnabled(false);
                startButton.setEnabled(true);
                label.setText("Press Start.");
            }
        }
    }

    void createGUI(JFrame f) {
        startButton = new JButton("Start");
        stopButton = new JButton("Stop");
        stopButton.setEnabled(false);
        label = new JLabel("Press Start.", JLabel.CENTER);

        Container c = f.getContentPane();
        //Use the content pane's default BorderLayout layout manager.
        c.add(startButton, BorderLayout.WEST);
        c.add(stopButton, BorderLayout.EAST);
        c.add(label, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        new SwingApp2();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -