?? simple3.java
字號:
/* * Simple3.java - an example of handling events. * For this example, we will use inner member classes to * implement an ActionListener for each button. This approach * can avoid some of the code clutter that anonymous classes * can sometimes cause. It also concentrates the action code * all in one place, and allows synonyms. */import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Simple3 extends JPanel{ private static JFrame frame; // static so main can use it private static JPanel myPanel; // a panel for contentPane private JButton button1; // Define out here to make private JButton button2; // visible to ActionListener // Define handlers for each event needed (button1, button2) private class Button1Handler implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(frame, "Button 1 pressed"); } } private class Button2Handler implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(frame, "Button 2 pressed"); } } public Simple3() // 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 add it ActionListener class button1.addActionListener(new Button1Handler()); button2.addActionListener(new Button2Handler()); myPanel.add(button1); // Adds to current JFrame myPanel.add(button2); } public static void main(String s[]) { Simple3 gui = new Simple3(); // Simple3 component frame = new JFrame("Simple3"); // 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 + -