driver.java
来自「Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套」· Java 代码 · 共 54 行
JAVA
54 行
import javax.swing.*;import java.awt.event.*;import java.awt.Color;/** Random Line Program (Figure 11.4) * Author: David D. Riley * Date: January, 2005 */public class Driver implements ActionListener { private JFrame window; private JButton addBtn, redBtn; private Bag<RandomLine> bagOfLines; /** post: window is created at (10, 10) with width and height of 400 * and addBtn & removeBtn are added to window */ public Driver() { window = new JFrame("Random Lines"); window.setBounds(10, 10, 400, 400); window.setVisible(true); window.setLayout(null); addBtn = new JButton("Add a Line"); addBtn.setBounds(10, 330, 180, 30); addBtn.addActionListener(this); window.add( addBtn, 0 ); redBtn = new JButton("Color Line Red"); redBtn.setBounds(210, 330, 180, 30); redBtn.addActionListener(this); window.add(redBtn, 0 ); window.repaint(); bagOfLines = new Bag<RandomLine>(); } /** post: e.getSource() == addBtn * implies a new randomly drawn line segment appears. * and e.getSource() == redBtn * implies one line from pane@pre has been removed */ public void actionPerformed(ActionEvent e) { RandomLine someLine; if (e.getSource() == addBtn) { someLine = new RandomLine(); window.getContentPane().add(someLine, 0); bagOfLines.add(someLine); } else { if (bagOfLines.size() != 0) { someLine = bagOfLines.item(); someLine.setBackground(Color.red); } } window.repaint(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?