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

📄 driver.java

📁 Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套 代码
💻 JAVA
字号:
import java.awt.*;import java.awt.event.*;import javax.swing.*;/** Traversal of Oval List Program  (Figure 11.11) *  Author: David D. Riley *  Date: JANUARY, 2005 */public class Driver implements ActionListener {	private JFrame window;	private JButton greenBtn, blueBtn, coordinateBtn;	private	SimpleList<Oval> ovalList;		/** post:	window is created 	 *			and  a row of black ovals spans window horizontally	 *			and  the row of ovals is stored in ovalList 	 */	public Driver()  {   		Oval tempOval;		int horizontalPos;			window = new JFrame("Manipulate the Ovals");		window.setBounds(10, 10, 600, 500);		window.setVisible(true);		window.setLayout(null);		greenBtn = newButton(20, 430, "Color Green");		blueBtn = newButton(210, 430, "Color Blue");		coordinateBtn = newButton(400, 430, "Print Coordinates");		ovalList = new SimpleList<Oval>();				horizontalPos = 5;		ovalList.reset();		while  ( horizontalPos < window.getWidth() )   {			tempOval = new Oval(horizontalPos, 100, 10, 10);			window.add(tempOval, 0);			ovalList.add( tempOval );			horizontalPos = horizontalPos + 15;		}			window.repaint();	}	/**	pre:	window != null  <br>	 *	post:	result is a newly created button	 *			and  result.getX()==x  and  result.getY()==y	 *			and  result.getWidth()==180  and  result.getHeight()==30	 *			and  result.getParent() == window's pane	 *			and  this is the action listener for result 	 */	public JButton newButton(int x, int y, String s)   {		JButton button;		button = new JButton(s);		button.setBounds(x, y, 180, 30);		button.addActionListener(this);		window.add(button, 0);		return button;	}	/**	post:	greenBtn was clicked	 *				implies all ovals are recolored green	 *			and  blueBtn was clicked	 *				implies  all ovals are recolored blue	 *			and  coordinateBtn was clicked	 *				implies the X coordinate of all ovals are output 	 */	public void actionPerformed(ActionEvent e)   {		if (e.getSource() == coordinateBtn) {            for (Oval tempOval : ovalList)  {				System.out.println( "X coordinate of oval: " + tempOval.getX() );            }		} else {  // either greenBtn or blueBtn was clicked            for (Oval tempOval : ovalList)  {				if (e.getSource()==greenBtn)					tempOval.setBackground( Color.green );				else					tempOval.setBackground( Color.blue );                tempOval.repaint();            }		}	}} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -