driver.java

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

JAVA
38
字号
import javax.swing.JFrame;import java.awt.*;/** Program to print widths of parent Containers (Figure 14.12) *  Author: David Riley *  Date: January, 2005 */public class Driver  {	public Driver()   {		JFrame win = new JFrame("Ancestor Containers");        win.setBounds(10, 10, 400, 70);		win.setVisible(true);        win.setLayout(null);		Rectangle outerRect = new Rectangle( 0, 0, 350, 40 );		outerRect.setBackground(Color.blue);		win.add(outerRect, 0);		Rectangle midRect = new Rectangle( 0, 0, 250, 30 );		midRect.setBackground(Color.green);		outerRect.add(midRect, 0);		Rectangle innerRect = new Rectangle( 0, 0, 100, 20 );		innerRect.setBackground(Color.red);		midRect.add(innerRect, 0);		win.repaint();		displayAncestorWidths( innerRect, win.getContentPane() );	}		/**	post:	all ancestor widths are displayed 	 *				- most distant ancestors first  	 */	private void displayAncestorWidths(Component c, Container pane)   {		if (c != pane  &&  c != null)  			displayAncestorWidths( c.getParent(), pane );		System.out.println( "Width: " + c.getWidth() );			}}

⌨️ 快捷键说明

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