?? s08.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>易都網(wǎng)--Java 2 圖形設(shè)計卷Ⅱ: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,網(wǎng)絡(luò)安全,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="s07.htm">上一頁</a>][<a href="s09.htm">下一頁</a>]第二部分 Swing組件</p> <p align="center"><b>第8章 標(biāo)簽與按鈕</b></p> <p> Swing的標(biāo)簽和按鈕分別用JLabel和JButton類表示,它們是能夠顯示文本或圖標(biāo)的簡單組件。缺省時,標(biāo)簽沒有邊框,可以顯示一個字符串,一個圖標(biāo)或同時顯示字符串和圖標(biāo)。除了用于修飾文本域等不重要的小事情外,Swing的標(biāo)簽還能起到圖像畫布(顯示一個圖像的組件)的作用。由于AWT的圖像不是組件,不能把它們添加到一個容器中。因此,使用AWT的開發(fā)人員實現(xiàn)了各種不同的圖像畫布類;然而,在Swing中,可以把JLabel類當(dāng)作圖像畫面使用(注:有關(guān)圖像畫面的更多信息參見4.3.1“Swing組件中的定制繪制”一節(jié))。<br> 按鈕大概是使用最為普遍的用戶界面組件。按鈕通常帶有某種邊框,且可以被鼠標(biāo)或快捷鍵激活。Swing按鈕比Swing標(biāo)簽要復(fù)雜得多,不僅因為能夠激活它們來完成某個功能,而且很多其他Swing組件都是AbstractButton類的擴展,而AbstractButton類是Swing按鈕的基類。 </p> <p> <b>8.1 JLabel與JBution</b></p> <p> </p> <p> <b>8.2 JLabel</b></p> <p align="center"><b>例8-1 運行中的JLabel</b></p> <hr noshade size="1"><pre>import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Test extends JApplet { public Test() { Container contentPane = getContentPane(); JLabel imageOnly = new JLabel(new ImageIcon(this.getClass().getResource("dogs.gif"))); JLabel textAndImage = new JLabel("Vote!", new ImageIcon(this.getClass().getResource("ballot_box.gif")), JLabel.RIGHT); JScrollPane scrollPane = new JScrollPane(imageOnly); scrollPane.setPreferredSize(new Dimension(270,200)); contentPane.setLayout( new FlowLayout(FlowLayout.CENTER, 25, 25)); contentPane.add(textAndImage); contentPane.add(scrollPane); }}</pre> <hr noshade size="1"> <p><br> </p> <p> <b>8.2.1 內(nèi)容排列</b></p> <p> </p> <p align="center"><b>例8-2 設(shè)置Swing標(biāo)簽的排列屬性</b></p> <hr noshade size="1"><pre>import java.net.URL;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class Test extends JApplet implements SwingConstants { JLabel label = new JLabel("Action!"); JPanel controlPanel = new JPanel(); JComboBox alignmentHorizontal = new JComboBox(); JComboBox alignmentVertical = new JComboBox(); public void init() { Container contentPane = getContentPane(); ImageIcon icon = new ImageIcon(this.getClass().getResource("slate.gif")); label.setIcon(icon); label.setHorizontalAlignment(CENTER); label.setFont(new Font("Times-Roman", Font.ITALIC, 20)); label.setMaximumSize(new Dimension(0, 150)); setupComboBoxes(); setupControlPanel(); contentPane.setLayout(new BorderLayout()); contentPane.add(controlPanel, "North"); contentPane.add(label, "Center"); alignmentVertical.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent event) { JComboBox b = (JComboBox)event.getSource(); String s = (String)b.getSelectedItem(); int c = getSwingConstantByName(s); label.setVerticalAlignment(c); } }); alignmentHorizontal.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent event) { JComboBox b = (JComboBox)event.getSource(); String s = (String)b.getSelectedItem(); int c = getSwingConstantByName(s); label.setHorizontalAlignment(c); } }); } void setupComboBoxes() { alignmentVertical.addItem("Top"); alignmentVertical.addItem("Center"); alignmentVertical.addItem("Bottom"); alignmentHorizontal.addItem("Left"); alignmentHorizontal.addItem("Center"); alignmentHorizontal.addItem("Right"); alignmentVertical.setSelectedItem( getSwingConstantName( label.getVerticalAlignment())); alignmentHorizontal.setSelectedItem( getSwingConstantName( label.getHorizontalAlignment())); } void setupControlPanel() { controlPanel.setBorder( BorderFactory.createTitledBorder("Alignment")); controlPanel.add(new JLabel( "Vertical:")); controlPanel.add(alignmentVertical); controlPanel.add(Box.createHorizontalStrut(5)); controlPanel.add(Box.createHorizontalStrut(25)); controlPanel.add(new JLabel( "Horizontal:")); controlPanel.add(Box.createHorizontalStrut(5)); controlPanel.add(alignmentHorizontal); } int getSwingConstantByName(String s) { if(s.equalsIgnoreCase("left")) return LEFT; else if(s.equalsIgnoreCase("center")) return CENTER; else if(s.equalsIgnoreCase("right")) return RIGHT; else if(s.equalsIgnoreCase("top")) return TOP; else if(s.equalsIgnoreCase("bottom")) return BOTTOM; return -1; } String getSwingConstantName(int c) { if(c == LEFT) return "Left"; else if(c == CENTER) return "Center"; else if(c == RIGHT) return "Right"; else if(c == TOP) return "Top"; else if(c == BOTTOM) return "Bottom"; return "undefined"; }}</pre> <hr noshade size="1"> <p> <b>8.2.2 文本的位置</b></p> <p> </p> <p align="center"><b>例8-3 設(shè)置標(biāo)簽的文本位置</b></p> <hr noshade size="1"><pre>import java.net.URL;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class Test extends JApplet implements SwingConstants { JLabel label = new JLabel("Action!"); JPanel controlPanel = new JPanel(); JComboBox alignmentHorizontal = new JComboBox(); JComboBox alignmentVertical = new JComboBox(); public void init() { Container contentPane = getContentPane(); ImageIcon icon = new ImageIcon(this.getClass().getResource("penguin.gif")); label.setIcon(icon); label.setHorizontalTextPosition(CENTER); label.setFont(new Font("Times-Roman", Font.ITALIC, 20)); setupComboBoxes(); setupControlPanel(); label.setHorizontalAlignment(JLabel.CENTER); label.setVerticalAlignment(JLabel.CENTER); contentPane.setLayout(new BorderLayout()); contentPane.add(controlPanel, "North"); contentPane.add(label, "Center"); alignmentVertical.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent event) { JComboBox b = (JComboBox)event.getSource(); String s = (String)b.getSelectedItem(); int c = getSwingConstantByName(s); label.setVerticalTextPosition(c); } }); alignmentHorizontal.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent event) { JComboBox b = (JComboBox)event.getSource(); String s = (String)b.getSelectedItem(); int c = getSwingConstantByName(s); label.setHorizontalTextPosition(c); } }); } void setupComboBoxes() { alignmentVertical.addItem("Top"); alignmentVertical.addItem("Center"); alignmentVertical.addItem("Bottom"); alignmentHorizontal.addItem("Left"); alignmentHorizontal.addItem("Center"); alignmentHorizontal.addItem("Right"); alignmentVertical.setSelectedItem( getSwingConstantName( label.getVerticalTextPosition())); alignmentHorizontal.setSelectedItem( getSwingConstantName( label.getHorizontalTextPosition())); } void setupControlPanel() { controlPanel.setBorder( BorderFactory.createTitledBorder("Text Position")); controlPanel.add(new JLabel( "Vertical:")); controlPanel.add(alignmentVertical); controlPanel.add(Box.createHorizontalStrut(5)); controlPanel.add(Box.createHorizontalStrut(25)); controlPanel.add(new JLabel("Horizontal:")); controlPanel.add(Box.createHorizontalStrut(5)); controlPanel.add(alignmentHorizontal); } int getSwingConstantByName(String s) { if(s.equalsIgnoreCase("left")) return LEFT; else if(s.equalsIgnoreCase("center")) return CENTER; else if(s.equalsIgnoreCase("right")) return RIGHT; else if(s.equalsIgnoreCase("top")) return TOP; else if(s.equalsIgnoreCase("bottom")) return BOTTOM; return -1; } String getSwingConstantName(int c) { if(c == LEFT) return "Left"; else if(c == CENTER) return "Center"; else if(c == RIGHT) return "Right"; else if(c == TOP) return "Top"; else if(c == BOTTOM) return "Bottom"; return "undefined"; }}</pre> <hr noshade size="1"> <p> <b>8.2.3 圖標(biāo)/文本間隙</b></p> <p> </p> <p align="center"><b>例8-4 設(shè)置一個標(biāo)簽的圖標(biāo)/文本間隙</b></p> <hr noshade size="1"><pre>import java.net.URL;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class Test extends JApplet implements SwingConstants { public void init() { Container contentPane = getContentPane(); JComboBox iconTextGap = new JComboBox(); JPanel controlPanel = new JPanel(); ImageIcon icon = new ImageIcon(this.getClass().getResource("ladybug.gif")); final JLabel label = new JLabel("Lady Bug", icon, CENTER); label.setFont(new Font("Times-Roman", Font.ITALIC, 20)); iconTextGap.addItem("4"); iconTextGap.addItem("10"); iconTextGap.addItem("15"); iconTextGap.addItem("20"); iconTextGap.addItem("25"); controlPanel.add(new JLabel("Icon/Text Gap:")); controlPanel.add(iconTextGap); contentPane.setLayout(new BorderLayout()); contentPane.add(controlPanel, "North"); contentPane.add(label, "Center"); iconTextGap.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent event) { JComboBox b = (JComboBox)event.getSource(); String s = (String)b.getSelectedItem(); int gap = Integer.parseInt(s); label.setIconTextGap(gap); } }); }}</pre> <hr noshade size="1"> <p> 8.2.4 許可狀態(tài)</p> <p> </p> <p align="center"><b>例8-5 設(shè)置一個標(biāo)簽的許可狀態(tài)</b></p> <hr noshade size="1"><pre>import java.net.URL;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class Test extends JApplet implements SwingConstants { public void init() { Container contentPane = getContentPane(); JComboBox iconTextGap = new JComboBox(); JPanel controlPanel = new JPanel(); ImageIcon icon = new ImageIcon(this.getClass().getResource("ladybug.gif")); final JLabel label = new JLabel("Lady Bug", icon, CENTER); label.setFont(new Font("Times-Roman", Font.ITALIC, 20)); iconTextGap.addItem("4"); iconTextGap.addItem("10"); iconTextGap.addItem("15"); iconTextGap.addItem("20"); iconTextGap.addItem("25"); controlPanel.add(new JLabel("Icon/Text Gap:")); controlPanel.add(iconTextGap); contentPane.setLayout(new BorderLayout()); contentPane.add(controlPanel, "North"); contentPane.add(label, "Center"); iconTextGap.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent event) { JComboBox b = (JComboBox)event.getSource(); String s = (String)b.getSelectedItem(); int gap = Integer.parseInt(s); label.setIconTextGap(gap); } }); }}</pre> <hr noshade size="1">
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -