driver.java

来自「Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套」· Java 代码 · 共 45 行

JAVA
45
字号
import javax.swing.JFrame;/** Driver for cake cutting  (Figures 9.9, 9.10, 9.12, 9.13, and 9.18) *  Author: David D. Riley *  Date: January, 2005 */public class Driver  {    private JFrame  window;    private Cake theCake;    private CakeButton sliceButton;	/**	post:	window is created at (10, 10) with width and height of 400	 *				and  sliceButton is added to window	 *				and  theCake is constructed as a CircularCake object   	 */    public Driver()  {		window = new JFrame("Cake Cutter");		window.setBounds(10, 10, 400, 500);		window.setVisible(true);		window.setLayout(null);		theCake = new CircularCake(100, 100, 200);		window.add( theCake, 0 );		sliceButton = new CakeButton( 75, 5, this);		window.add( sliceButton, 0 );		window.repaint();    }    	/**	pre:	theCake != null  <br>     *		post:	theCake@pre is removed from window	 *  			and  a new theCake is constructed & placed on window	 *			 	(The new cake has a 50-50 chance of being circular 	 *				or rectangular.	 */	public  void  cutTheCake()   {		window.remove(theCake);    	if (Math.random() > 0.5)   {			theCake = new CircularCake(100, 100, (int)(Math.random()*200+1));    	} else {    		theCake = new RectangularCake(100, 150, 200, (int)(Math.random()*300+1));    	}    	window.add( theCake, 0 );		window.repaint();    }}

⌨️ 快捷键说明

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