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

📄 simple2.java

📁 绝对经典!好动西和大家一起分享 呵呵 你们不应该如此限制的,不好
💻 JAVA
字号:
/* * Simple2.java - an example of handling events. *    For this example, we will use anonymous inner classes to *    implement an ActionListener for each button. This approach *    can avoid long switch-type if statements from the approach *    used in the Simple1 example. */import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Simple2{    private static JFrame frame;   // static so main can use it    private static JPanel myPanel; // panel for the contentPane    private JButton button1;       // Define out here to make    private JButton button2;       // visible to ActionListeners    public Simple2()               // Construct, build GUI    {        // Create a panel        myPanel = new JPanel();        // Create the buttons        button1 = new JButton("Button 1");        button2 = new JButton("Button 2");        // For each component that needs a listener, define an        // anonymous inner class to implement ActionListener        button1.addActionListener(            new ActionListener()            {                public void actionPerformed(ActionEvent e)                {                    JOptionPane.showMessageDialog(frame,                                           "Button 1 pressed");                }            }        );        button2.addActionListener(            new ActionListener()            {                public void actionPerformed(ActionEvent e)                {                    JOptionPane.showMessageDialog(frame,                                            "Button 2 pressed");                }            }        );        myPanel.add(button1);        // Adds to current JFrame        myPanel.add(button2);    }    public static void main(String s[])    {        Simple2 gui = new Simple2(); // Create Simple2 component                frame = new JFrame("Simple2");  // JFrame for the panel        // Standard idiom to catch close event        frame.addWindowListener(new WindowAdapter() {             public void windowClosing(WindowEvent e)              {System.exit(0);} });        frame.getContentPane().add(myPanel);        frame.pack();                  // Ready to go        frame.setVisible(true);    }}

⌨️ 快捷键说明

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