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

📄 windoweventdemo.java

📁 这是一个英文版的《Java程序设计与问题解决》现在好多大学都当成教材
💻 JAVA
字号:
/* * WindowEventDemo.java is a 1.4 example that requires * no other files. */import javax.swing.*;import java.awt.Dimension;import java.awt.BorderLayout;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;public class WindowEventDemo extends JApplet                             implements WindowListener,                                        ActionListener {    JTextArea display;    JFrame window;    JButton b1, b2;    static final String SHOW = "show";    static final String CLEAR = "clear";    final static String newline = "\n";    public void init() {        b1 = new JButton("Click to bring up a window.");        b1.setActionCommand(SHOW);        b1.addActionListener(this);        b2 = new JButton("Click to clear the display.");        b2.setActionCommand(CLEAR);        b2.addActionListener(this);        display = new JTextArea();        display.setEditable(false);        JScrollPane scrollPane = new JScrollPane(display);        scrollPane.setPreferredSize(new Dimension(200, 75));        JFrame.setDefaultLookAndFeelDecorated(true);        JPanel contentPane = new JPanel();        contentPane.setLayout(new BorderLayout());        contentPane.add(b1, BorderLayout.PAGE_START);        contentPane.add(scrollPane, BorderLayout.CENTER);        contentPane.add(b2, BorderLayout.PAGE_END);        setContentPane(contentPane);        //Create but don't show window.        window = new JFrame("Window Event Window");        window.addWindowListener(this);        JLabel l = new JLabel("The applet listens to this window"                            + " for window events.");        window.getContentPane().add(l, BorderLayout.CENTER);        window.pack();    }    public void stop() {        window.setVisible(false);    }    public void windowClosing(WindowEvent e) {        window.setVisible(false);        displayMessage("Window closing", e);    }    public void windowClosed(WindowEvent e) {        displayMessage("Window closed", e);    }    public void windowOpened(WindowEvent e) {        displayMessage("Window opened", e);    }    public void windowIconified(WindowEvent e) {        displayMessage("Window iconified", e);    }    public void windowDeiconified(WindowEvent e) {        displayMessage("Window deiconified", e);    }    public void windowActivated(WindowEvent e) {        displayMessage("Window activated", e);    }    public void windowDeactivated(WindowEvent e) {        displayMessage("Window deactivated", e);    }    void displayMessage(String prefix, WindowEvent e) {        display.append(prefix                       + ": "                       + e.getWindow()                       + newline);     }    public void actionPerformed(ActionEvent e) {        if (e.getActionCommand() == SHOW) {            window.pack();            window.setVisible(true);        } else {            display.setText("");        }    }}

⌨️ 快捷键说明

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