?? s05.htm
字號:
<SCRIPT LANGUAGE="JavaScript" SRC="/-fs0/sys/pop-up.js"></SCRIPT><SCRIPT LANGUAGE="JavaScript" SRC="/-fs0/sys/pop-up-all.js"></SCRIPT><html><head><title>易都網--Java 2 圖形設計卷Ⅱ:SWING</title><LINK rel="stylesheet" href="../../../_public/javaa.css"><meta http-equiv="Content-Type" content="text/html; charset=GBK"><script language="JavaScript" src="../../../_public/javaa.js"></script><meta name="keywords" content="Java,JSP,ASP,PHP,J2EE,EJB,JavaScript,C/C++,ASM,CSS,HTML,XML,網絡安全,MySQL,ACCESS"></head><body bgcolor="#FFFFFF"><table border=0 cellpadding=0 cellspacing=0 width="100%"> <tbody> <script language="javascript">print2()</script> <tr> <td width="100%"> <table bgcolor=#EEEEEE border=0 cellpadding=3 cellspacing=0 width="100%"> <tbody> <tr> <td class=f1 id=thetd width="100%"> <p>[<a href="index.html" target="_self">目錄</a>][<a href="s04.htm">上一頁</a>][<a href="s06.htm">下一頁</a>]</p> <p align="center"><b>第5章 邊框、圖標和動作</b></p> <p> 本章介紹Swing的三種實用工具:邊框、圖標和動作。<br> 邊框繪制在組件的邊界周圍,它有許多不同的各類:線邊框、雕刻邊框、不光滑的邊框等等。邊框本身不是組件,所以,它們繪制在指定組件的邊襯中。<br> 圖標是圖形對象,通常是一個小圖像。與邊框一樣,圖標在指定組件的指定位置上繪制。<br> 動作封裝圖形用戶界面的一個邏輯操作,并且還簡化用戶界面元素的構造工作。動作通常由一個或多個圖標或文本字符串組成。可以把動作添加到某些容器中,這些容器創建一個組件與這個動作相關聯。例如,利用JMenu.add(Action)方法,可把動作添加到一個菜單中。當一個動作添加到一個菜單中時,這個菜單用與這個動作相關聯的文本和圖標來創建一個菜單項并把這個菜單項添加到菜單中。<br> 邊框、圖標和動作都是很有意義的,因為它們都可以與多個組件相關聯。由于邊框和圖標都不是組件,但卻都能繪制到組件中,所以,可以在支持使用邊框和圖標的多個組件中共享邊框和圖標。動作也必須被多個組件所共享,并且用來作為控制的中心點以維護與這個動作相關聯的組件的啟用狀態。</p> <p> <b>5.1 邊框</b></p> <p> 通過構造所需類型的邊框,然后把這個邊框傳送給JComponent.setBorder(Border),所有JComponent擴展(JViewport除外)都可以有邊框。雖然每個組件可以只有一個邊框,但Swing支持組合邊框。因此,在實際應用中,單個組件可以使數個邊框嵌套在一起,使邊框有一定的深度。<br> 邊框的使用很簡單。例如,圖5-1示出了一個帶標題邊框的JPanel實例。</p> <p> <applet code="Test.class" archive="s05_tu01.jar" width=350 height=200> </applet><br> 圖5-1 一個帶標題邊框的JPanel實例</p> <p> 例5-1例出了圖5-1所示的小應用程序的代碼。</p> <p align="center"><b> 例5-1 一個帶邊框的JPanel的小應用程序 </b></p> <hr size="1" noshade> <p> import java.awt.BorderLayout;<br> import javax.swing.*;<br> import javax.swing.border.*;</p> <p> public class Test extends JApplet {<br> public void init() {<br> JPanel panel = new JPanel();</p> <p> panel.setBorder(new TitledBorder("JPanel Border"));<br> getContentPane().add(panel, BorderLayout.CENTER);<br> }<br> } <hr size="1" noshade> 這個小應用程序創建一個帶標題的邊框,這個邊框傳遞給面板的setBorder方法。 <p><b>5.1.1 邊框和邊襯</b></p> <p> AWT容器有一個insets屬性,它定義容器的邊襯。布局管理器仔細地布局一個容器中的各個組件,以便這些組件不會侵占這個容器的邊襯區。容器的insets屬性是一個只讀屬性,修改AWT容器insets屬性唯一的方法是子類化一個容器并重載它的getInsets方法。</p> <p> 5.1.2 Swing的邊框類型</p> <p align="center"><b>例5-2 顯示所有Swing邊框類型的小應用程序</b></p> <hr noshade size="1"><pre>import java.awt.*;import javax.swing.*;import javax.swing.border.*;public class Test extends JApplet { public void init() { JPanel jpanel = new AllBordersPanel(); getContentPane().add(jpanel, BorderLayout.CENTER); }}class AllBordersPanel extends JPanel { public AllBordersPanel() { JPanel bl = new PanelWithTitle("Bevel Lowered"), br = new PanelWithTitle("Bevel Raised"), c = new PanelWithTitle("Compound"), l = new PanelWithTitle("Line"), m = new PanelWithTitle("Matte"), e = new PanelWithEmptyBorder("Empty"), t = new PanelWithTitle("Titled"), sbr = new PanelWithTitle("Soft Bevel Raised"), sbl = new PanelWithTitle("Soft Bevel Lowered"), el = new PanelWithTitle("Etched Lowered"), er = new PanelWithTitle("Etched Raised"); setLayout(new GridLayout(4,3,2,2)); ImageIcon icon = new ImageIcon(this.getClass().getResource("smiley.gif")); Dimension iconsz = new Dimension(icon.getIconWidth(), icon.getIconHeight()); bl.setBorder(BorderFactory.createLoweredBevelBorder()); br.setBorder(BorderFactory.createRaisedBevelBorder()); sbr.setBorder(new SoftBevelBorder(BevelBorder.RAISED)); sbl.setBorder(new SoftBevelBorder(BevelBorder.LOWERED)); t.setBorder(BorderFactory.createTitledBorder("Titled")); l.setBorder( BorderFactory.createLineBorder(Color.black,2)); c.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(Color.gray,10), BorderFactory.createRaisedBevelBorder()), BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(Color.blue,5), BorderFactory.createLoweredBevelBorder()))); el.setBorder(BorderFactory.createEtchedBorder( getBackground().brighter(), getBackground().darker())); er.setBorder(BorderFactory.createEtchedBorder( getBackground().darker(), getBackground().brighter())); m.setBorder(BorderFactory.createMatteBorder( iconsz.height, iconsz.width, iconsz.height, iconsz.width, icon)); add(br); add(bl); add(sbr); add(sbl); add(c); add(el); add(er); add(e); add(l); add(m); add(t); }}class PanelWithTitle extends JPanel { private String title; public PanelWithTitle(String title) { this.title = title; } public void paintComponent(Graphics g) { FontMetrics fm = g.getFontMetrics(); Dimension size = getSize(); int titleW = fm.stringWidth(title); g.setColor(Color.black); g.drawString(title, size.width/2 - titleW/2, size.height/2); }}class PanelWithEmptyBorder extends PanelWithTitle { public PanelWithEmptyBorder(String title) { super(title); setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); } public void paintComponent(Graphics g) { Dimension size = getSize(); Insets insets = getInsets(); g.setColor(Color.red); g.fillRect(insets.left,insets.top, size.width-2*insets.left, size.height-2*insets.top); super.paintComponent(g); }}</pre> <hr size="1" noshade> <p> 5.1.3 不透明與透明之間的比較</p> <p> </p> <p align="center"><b>例5-3 部分透明的邊框的樣例</b></p> <hr noshade size="1"><pre>import javax.swing.*;import javax.swing.border.*;import java.awt.*;public class Test extends JApplet { JPanel panel = new RainPanel(); TitledBorder border = new TitledBorder("JPanel Border"); public void init() { panel.setBorder(border); getContentPane().add(panel, BorderLayout.CENTER); System.out.println("opaque = " + border.isBorderOpaque()); System.out.println( "insets = " + border.getBorderInsets(panel)); }}class RainPanel extends JPanel { public void paintComponent(Graphics g) { Icon icon = new ImageIcon(getClass().getResource("rain.gif")); Dimension size = getSize(); int patchW = icon.getIconWidth(), patchH = icon.getIconHeight(); for(int r=0; r < size.width; r += patchW) { for(int c=0; c < size.height; c += patchH) icon.paintIcon(this, g, r, c); } }}</pre> <hr size="1" noshade> <p> 5.1.4 邊框包</p> <p> </p> <p> 5.1.5 邊框接口</p> <p> </p> <p> 5.1.6 AbstractBoorder類</p> <p> </p> <p> 5.1.7 邊框庫——共享邊框</p> <p> </p> <p align="center"><b>例5-4 從邊框庫中獲得邊框</b></p> <hr noshade size="1"><pre>import java.awt.*;import javax.swing.*;import javax.swing.border.*;public class Test extends JApplet { public void init() { Container contentPane = getContentPane(); JPanel panel = new JPanel(); JPanel panel2 = new JPanel(); Border border = BorderFactory.createRaisedBevelBorder(); Border border2 = BorderFactory.createRaisedBevelBorder(); panel.setBorder(border); panel2.setBorder(border2); contentPane.add(panel, BorderLayout.NORTH); contentPane.add(panel2, BorderLayout.SOUTH); if(border == border2) System.out.println("bevel borders are shared"); else System.out.println("bevel borders are NOT shared"); }}</pre> <hr size="1" noshade> <p> 5.1.8 替換內置邊框</p> <p> </p> <p> 5.1.9 實現定制的邊框</p> <p> </p> <p align="center"><b>例5-5 HandleBorder類清單</b></p> <hr noshade size="1"><pre>import java.awt.*;import javax.swing.*;import javax.swing.border.*;public class HandleBorder extends AbstractBorder { protected Color lineColor; protected int thick; public HandleBorder() { this(Color.black, 6); } public HandleBorder(Color lineColor, int thick) { this.lineColor = lineColor; this.thick = thick; } public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { Graphics copy = g.create(); if(copy != null) { try { copy.translate(x,y); paintRectangle(c,copy,w,h); paintHandles(c,copy,w,h); } finally { copy.dispose(); } } } public Insets getBorderInsets() { return new Insets(thick,thick,thick,thick); } protected void paintRectangle(Component c, Graphics g, int w, int h) { g.setColor(lineColor); g.drawRect(thick/2,thick/2,w-thick-1,h-thick-1); } protected void paintHandles(Component c, Graphics g, int w, int h) { g.setColor(lineColor); g.fillRect(0,0,thick,thick); // upper left g.fillRect(w-thick,0,thick,thick); // upper right g.fillRect(0,h-thick,thick,thick); // lower left g.fillRect(w-thick,h-thick,thick,thick); // lower right g.fillRect(w/2-thick/2,0,thick,thick); // mid top g.fillRect(0,h/2-thick/2,thick,thick); // mid left g.fillRect(w/2-thick/2,h-thick,thick,thick); // mid bottom g.fillRect(w-thick,h/2-thick/2,thick,thick); // mid right } }</pre> <hr size="1" noshade> <p align="center"><b>例5-6 使用定制邊框</b></p> <hr noshade size="1"><pre>import javax.swing.*;import javax.swing.border.*;import java.awt.*;import java.awt.event.*;public class Test extends JApplet { public void init() { Container contentPane = getContentPane(); JPanel[] panels = { new JPanel(), new JPanel(), new JPanel() }; Border[] borders = { new HandleBorder(), new HandleBorder(Color.red, 8), new HandleBorder(Color.blue, 10) }; contentPane.setLayout( new FlowLayout(FlowLayout.CENTER,20,20)); for(int i=0; i < panels.length; ++i) { panels[i].setPreferredSize(new Dimension(100,100)); panels[i].setBorder(borders[i]); contentPane.add(panels[i]); } }}</pre> <hr size="1" noshade> <p> </p> <p> 5.2 圖標</p> <p align="center"><b>例5-7 一個繪制圖標的小應用程序</b></p> <hr noshade size="1"><pre>import com.sun.java.swing.JApplet;import java.awt.Color;import java.awt.Graphics;public class IconTest extends JApplet { ColorIcon redIcon; ColorIcon blueIcon; ColorIcon yellowIcon; public void paint(Graphics g) { redIcon.paintIcon(this, g, 0, 0); blueIcon.paintIcon(this, g, redIcon.getIconWidth() + 10, 0); yellowIcon.paintIcon(this, g, redIcon.getIconWidth() + 10 + blueIcon.getIconWidth() + 10, 0); } public IconTest() { redIcon = new ColorIcon(Color.red, 50, 50); blueIcon = new ColorIcon(Color.blue, 60, 60); yellowIcon = new ColorIcon(Color.yellow, 70, 70); }}</pre> <hr size="1" noshade> <p align="center"><b>例5-8 ColorIcon類清單</b></p> <hr noshade size="1"><pre>import java.awt.*;import javax.swing.*;class ColorIcon implements Icon { private Color fillColor; private int w, h; public ColorIcon(Color fillColor, int w, int h) { this.fillColor = fillColor; this.w = w; this.h = h; } public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(Color.black); g.drawRect(x, y, w-1, h-1); g.setColor(fillColor); g.fillRect(x+1, y+1, w-2, h-2); } public int getIconWidth() { return w; } public int getIconHeight() { return h; }}</pre> <hr size="1" noshade> <p> </p> <p> </p> <p> 5.2.1 把圖標與組件相關聯</p> <p> </p> <p align="center"><b>例5-9 菜單項中的圖標</b></p> <hr noshade size="1"><pre>import java.awt.*;import javax.swing.*;public class Test extends JApplet { ColorIcon redIcon = new ColorIcon(Color.red, 40, 15), blueIcon = new ColorIcon(Color.blue, 40, 15), yellowIcon = new ColorIcon(Color.yellow, 40, 15); public void init() { JMenuBar mb = new JMenuBar(); JMenu colors = new JMenu("Colors"); colors.add(new JMenuItem(redIcon)); colors.add(new JMenuItem(blueIcon));
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -