?? simple1.java
字號:
/* * Simple1.java - an example of handling events. * For this example, we implement a single ActionListener * which is then used to catch all events. A series of ifs * are used to determine which object caused the event. */import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Simple1{ 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 ActionListener public Simple1() // Construct, build GUI { // Create a panel myPanel = new JPanel(); // Create the buttons. button1 = new JButton("Button 1"); // Create buttons button2 = new JButton("Button 2"); SimpleListener ourListener = new SimpleListener(); // Set action listener for both buttons to share button1.addActionListener(ourListener); button2.addActionListener(ourListener); myPanel.add(button1); // Adds to current JFrame myPanel.add(button2); } private class SimpleListener implements ActionListener { /* * We will use a simple inner class to implement an * ActionListener to use to get the button events. We * could have instead used the Simple1 class itself to * implement ActionListener instead, but this way is * more consistent with the other examples. */ public void actionPerformed(ActionEvent e) { // We will use getActionCommand to get the button // name, but we could use getSource() instead and // the if tests would then be like: // if (e.getSource() == button1) etc. String buttonName = e.getActionCommand(); if (buttonName.equals("Button 1")) JOptionPane.showMessageDialog(frame, "Button 1 pressed"); else if (buttonName.equals("Button 2")) JOptionPane.showMessageDialog(frame, "Button 2 pressed"); else JOptionPane.showMessageDialog(frame, "Unknown event"); } } public static void main(String s[]) { Simple1 gui = new Simple1(); // Create Simple1 component frame = new JFrame("Simple1"); // 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); }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -