driver.java
来自「Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套」· Java 代码 · 共 42 行
JAVA
42 行
import java.awt.event.*;import javax.swing.*;/** Example of event delegation with 2 buttons (Figure 9.32) * Author: David Riley * Date: January, 2005 */public class Driver implements ActionListener { private JFrame window; private JButton simpleButton, anotherButton; /** post: window is created at (10, 10) with width and height of 200 * and simpleButton is added to window * and anotherButton is added to window */ public Driver() { window = new JFrame("Two Buttons"); window.setBounds(10, 10, 200, 200); window.setVisible(true); window.setLayout(null); simpleButton = new JButton("Click Me"); simpleButton.setBounds(50, 20, 100, 40); simpleButton.addActionListener( this ); window.add( simpleButton, 0 ); anotherButton = new JButton("Clack Me"); anotherButton.setBounds(50, 80, 100, 40); anotherButton.addActionListener( this ); window.add( anotherButton, 0 ); window.repaint(); } /** post: simpleButton clicked implies CLICKED was output * and anotherButton clicked implies CLACKED was output */ public void actionPerformed( ActionEvent e ) { if (e.getSource() == simpleButton) System.out.println( "CLICKED" ); else System.out.println( "CLACKED" ); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?