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

📄 driver.java

📁 Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套 代码
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -