?? s14.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="s13.htm">上一頁</a>][<a href="s15.htm">下一頁</a>]</p> <p align="center"><b>第14章 窗口和對話框</b></p> <p> Swing的窗口(window)、窗體(frame)和對話框(dialog)是分別擴展AWT的window類Frame類和Dialog類的重量組件。當這三個組件都是窗口時,這三個組件之間的差別是不明顯的,因此,有時在給定情況下要確定使用哪個組件是很困難的。為了澄清這些差別,表14-1列出了與這三個組件有關的一些屬性。</p> <p> 表14-1 窗口、窗體和對話框屬性<sup>①,②</sup><br> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━<br> 屬性 窗口 窗體 對話框<br> ─────────────────────────────────<br> 模態 否 否 否/CSG<br> 可調整大小 否 否/SG 是/SG<br> 標題欄 否 是 是<br> 邊框 否 是 是<br> 標題 否 是/CSG 是/CSG<br> 菜單欄 否 是/SG 否<br> 焦點管理器 是 是 是<br> 警告字符串 是/G 是/G 是/G<br> 圖標圖像<sup>③</sup> 否 是/SG 否<br> 鏈接到一個窗體 是 否 是<br> ─────────────────────────────────<br> ①是/否指缺省的屬性狀態<br> ②C=在構造時可設置,S=可使用的設置方法,G=可使用的獲取方法(即get...()或is...())<br> <sup>③不是所有的平臺都支持窗口的圖標化。</sup></p> <p> 窗口是這三個組件中最基本的組件,事實上,java.awt.Window是Frame和Dialog的超類。窗口沒有邊框、標題欄或菜單欄,而且不能調整其大小。如果需要在其他組件之上的無邊框矩形區域中顯示某些內容,則窗口是最合適的。<br> </p> <p> 14.1 JWindow</p> <p align="center"><b>例14-1 使用JWindow來實現一個splash窗口</b></p> <hr noshade size="1"> import java.awt.*;<br> import java.awt.event.*;<br> import javax.swing.*; <p>// This has been modified from the code in the book<br> // to display the animated swing.gif in a window in the corner<br> // of your desktop. A double click closes the window.</p> <p>public class Test extends JFrame {<br> Toolkit toolkit = Toolkit.getDefaultToolkit();<br> JWindow window = new JWindow();<br> JLabel label = new JLabel(new ImageIcon("swing.gif"));</p> <p> static public void main(String[] args) {<br> JFrame frame = new Test();<br> }<br> public Test() {<br> //label.setBorder(BorderFactory.createRaisedBevelBorder());<br> window.getContentPane().add(label, "Center");<br> //centerWindow();<br> // change location to suite taste ...<br> window.setLocation(75,10);<br> window.pack();<br> window.show();</p> <p> window.addMouseListener(new MouseAdapter() {<br> public void mousePressed(MouseEvent e) {<br> if(e.getClickCount() == 2) {<br> window.dispose();<br> System.exit(0);<br> }<br> }<br> });<br> }<br> private void centerWindow() {<br> Dimension scrnSize = toolkit.getScreenSize();<br> Dimension labelSize = label.getPreferredSize();<br> int labelWidth = labelSize.width,<br> labelHeight = labelSize.height;</p> <p> window.setLocation(scrnSize.width/2 - (labelWidth/2),<br> scrnSize.height/2 - (labelHeight/2));<br> window.pack();<br> }<br> }</p> <hr size="1" noshade> <p align="center"><b>例14-2 一個作為應用程序窗口使用的JWindow實例</b></p> <hr noshade size="1"> import java.awt.*;<br> import java.awt.event.*;<br> import javax.swing.*; <p>public class Test extends JApplet {<br> JWindow window = new JWindow();<br> JMenuBar menuBar = new JMenuBar();<br> JMenu fileMenu = new JMenu("File");<br> JMenuItem quitItem;</p> <p> public Test() {<br> final Container contentPane = getContentPane();<br> JButton button = new JButton("show window ...");</p> <p> contentPane.setLayout(new FlowLayout());<br> contentPane.add(button);</p> <p> fileMenu.add("New");<br> fileMenu.add("Open ...");<br> fileMenu.add("Save");<br> fileMenu.add("Save As ...");<br> fileMenu.addSeparator();<br> fileMenu.add(quitItem = new JMenuItem("Quit"));</p> <p> menuBar.add(fileMenu);</p> <p> window.getRootPane().setJMenuBar(menuBar);<br> window.getRootPane().setBorder(<br> BorderFactory.createRaisedBevelBorder());</p> <p> button.addActionListener(new ActionListener() {<br> public void actionPerformed(ActionEvent e) {<br> Point pt = contentPane.getLocation();</p> <p> SwingUtilities.convertPointToScreen(<br> pt, contentPane);</p> <p> window.setBounds(pt.x + 10, pt.y + 10, 200, 200);<br> window.show();</p> <p> quitItem.addActionListener(new ActionListener() {<br> public void actionPerformed(ActionEvent e) {<br> window.dispose();<br> }<br> });<br> }<br> });<br> }<br> }</p> <hr size="1" noshade> <p> </p> <p> 14.1.1 JWindow屬性</p> <p> </p> <p> 14.1.2 JWindow類總結</p> <p> </p> <p> 14.1.3 AWT兼容</p> <p> </p> <p> 14.2 JDialog</p> <p> </p> <p align="center"><b>例14-3 運行中的JDialog</b></p> <hr noshade size="1"> import java.awt.*;<br> import java.awt.event.*;<br> import javax.swing.*; <p>public class Test extends JApplet {<br> private ConstraintsPanel cp = new ConstraintsPanel();<br> private JPanel buttonsPanel = new JPanel(); </p> <p> private JButton showButton = new JButton("show dialog ..."),<br> okButton = new JButton("OK"),<br> applyButton = new JButton("Apply"),<br> cancelButton = new JButton("Cancel");</p> <p> private JButton[] buttons = new JButton[] {<br> okButton, applyButton, cancelButton,<br> };</p> <p> private JDialog dialog = new JDialog(null, // owner<br> "Constraints Dialog", // title<br> true); // modal</p> <p> public Test() {<br> Container contentPane = getContentPane();<br> Container dialogContentPane = dialog.getContentPane();</p> <p> contentPane.setLayout(new FlowLayout());<br> contentPane.add(showButton);</p> <p> dialogContentPane.add(cp, BorderLayout.CENTER);<br> dialogContentPane.add(buttonsPanel, BorderLayout.SOUTH);<br> dialog.pack();</p> <p> // setLocationRelativeTo must be called after pack(),<br> // because dialog placement is based on dialog size.<br> // Because the applet is not yet showing, calling<br> // setLocationRelativeTo() here causes the dialog to be<br> // shown centered on the screen.<br> //<br> // If setLocationRelativeTo() is not invoked, the dialog<br> // will be located at (0,0) in screen coordinates.<br> //dialog.setLocationRelativeTo(this);</p> <p> for(int i=0; i < buttons.length; ++i) {<br> buttonsPanel.add(buttons[i]);<br> }<br> addButtonListeners();<br> }<br> private void addButtonListeners() {<br> showButton.addActionListener(new ActionListener() {<br> public void actionPerformed(ActionEvent e) {<br> // calling setLocationRelativeTo() here causes<br> // the dialog ito be centered over the applet.<br> dialog.setLocationRelativeTo(Test.this);<br> dialog.show();<br> }<br> });<br> okButton.addActionListener(new ActionListener() {<br> public void actionPerformed(ActionEvent e) {<br> showStatus("OK button Activated");<br> dialog.dispose();<br> }<br> });<br> applyButton.addActionListener(new ActionListener() {<br> public void actionPerformed(ActionEvent e) {<br> showStatus("Apply button Activated");<br> }<br> });<br> cancelButton.addActionListener(new ActionListener() {<br> public void actionPerformed(ActionEvent e) {<br> showStatus("Cancel button Activated");<br> dialog.dispose();<br> }<br> });<br> }<br> }</p> <hr size="1" noshade> <p> 14.2.1 JDialog屬性</p> <p> </p> <p> 14.2.2 JDialog類總結</p> <p> </p> <p> 14.2.3 AWT兼容</p> <p> </p> <p> 14.3 JOptionPane</p> <p> </p> <p align="center"><b>例14-4 用JOptionPane創建對話框</b></p> <hr noshade size="1"> import java.awt.*;<br> import java.awt.event.*;<br> import javax.swing.*; <p>public class Test extends JApplet {<br> private JButton topButton = new JButton(<br> "show dialog created from option pane");<br> private JButton bottomButton = new JButton(<br> "show dialog created with static method");</p> <p> private String title = "dialog title";<br> private String message = "message";</p> <p> public Test() {<br> Container contentPane = getContentPane();</p> <p> contentPane.setLayout(new FlowLayout());<br> contentPane.add(topButton);<br> contentPane.add(bottomButton);</p> <p> topButton.addActionListener(new ActionListener() {<br> public void actionPerformed(ActionEvent e) {<br> JOptionPane optionPane = new JOptionPane(<br> message, // message<br> JOptionPane.INFORMATION_MESSAGE); // messageType</p> <p> JDialog dialog = optionPane.createDialog(<br> topButton, // parentComponent<br> title); // title</p> <p> dialog.show();<br> }<br> });<br> bottomButton.addActionListener(new ActionListener() {<br> public void actionPerformed(ActionEvent e) {<br> JOptionPane.showMessageDialog(<br> bottomButton, // parentComponent<br> message, // message<br> title, // title<br> JOptionPane.INFORMATION_MESSAGE); // messageType<br> }<br> });<br> }<br> }</p> <hr size="1" noshade> <p> 14.3.1 內部窗體</p> <p> </p> <p align="center"><b>例14-5 用JOptionPane創建內部窗體</b></p> <hr noshade size="1"> import java.awt.*;<br> import java.awt.event.*;<br> import javax.swing.*; <p>public class Test extends JApplet {<br> private JButton button = new JButton("show internal frame");</p> <p> public Test() {<br> Container contentPane = getContentPane();</p> <p> contentPane.setLayout(new FlowLayout());<br>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -