📄 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 + -