第14章 窗口和对话框
来源:互联网 发布:软件限制策略 编辑:程序博客网 时间:2024/05/17 00:13
第14章 窗口和对话框 Swing的窗口(window)、窗体(frame)和对话框(dialog)是分别扩展AWT的window类Frame类和Dialog类的重量组件。当这三个组件都是窗口时,这三个组件之间的差别是不明显的,因此,有时在给定情况下要确定使用哪个组件是很困难的。为了澄清这些差别,表14-1列出了与这三个组件有关的一些属性。 表14-1 窗口、窗体和对话框属性①,② 窗口是这三个组件中最基本的组件,事实上,java.awt.Window是Frame和Dialog的超类。窗口没有边框、标题栏或菜单栏,而且不能调整其大小。如果需要在其他组件之上的无边框矩形区域中显示某些内容,则窗口是最合适的。 14.1 JWindow 例14-1 使用JWindow来实现一个splash窗口 import java.awt.*; // This has been modified from the code in the book public class Test extends JFrame { static public void main(String[] args) { window.addMouseListener(new MouseAdapter() { window.setLocation(scrnSize.width/2 - (labelWidth/2), 例14-2 一个作为应用程序窗口使用的JWindow实例 import java.awt.*; public class Test extends JApplet { public Test() { contentPane.setLayout(new FlowLayout()); fileMenu.add("New"); menuBar.add(fileMenu); window.getRootPane().setJMenuBar(menuBar); button.addActionListener(new ActionListener() { SwingUtilities.convertPointToScreen( window.setBounds(pt.x + 10, pt.y + 10, 200, 200); quitItem.addActionListener(new ActionListener() { 14.1.1 JWindow属性 14.1.2 JWindow类总结 14.1.3 AWT兼容 14.2 JDialog 例14-3 运行中的JDialog import java.awt.*; public class Test extends JApplet { private JButton showButton = new JButton("show dialog ..."), private JButton[] buttons = new JButton[] { private JDialog dialog = new JDialog(null, // owner public Test() { contentPane.setLayout(new FlowLayout()); dialogContentPane.add(cp, BorderLayout.CENTER); // setLocationRelativeTo must be called after pack(), for(int i=0; i < buttons.length; ++i) { 14.2.1 JDialog属性 14.2.2 JDialog类总结 14.2.3 AWT兼容 14.3 JOptionPane 例14-4 用JOptionPane创建对话框 import java.awt.*; public class Test extends JApplet { private String title = "dialog title"; public Test() { contentPane.setLayout(new FlowLayout()); topButton.addActionListener(new ActionListener() { JDialog dialog = optionPane.createDialog( dialog.show(); 14.3.1 内部窗体 例14-5 用JOptionPane创建内部窗体 import java.awt.*; public class Test extends JApplet { public Test() { contentPane.setLayout(new FlowLayout()); button.addActionListener(new ActionListener() { 14.3.2 用JOptionPane静态方法创建对话框 14.3.3 消息对话框 例14-6 显示具有不同消息类型的消息对话框 import java.awt.*; public class Test extends JApplet { private String title = "dialog title"; public Test() { contentPane.setLayout(new FlowLayout()); button.addActionListener(new ActionListener() { switch(messageType) { public ControlPanel(final Test applet) { for(int i=0; i < typeNames.length; ++i) { for(int i=0; i < typeNames.length; ++i) { 例14-7 替换消息对话框的缺省图标 import java.awt.*; public class Test extends JApplet { private String title = "Reminder!"; public Test() { contentPane.setLayout(new FlowLayout()); button.addActionListener(new ActionListener() { 14.3.4 确认对话框 例14-8 使用确认对话框 import java.awt.*; public class Test extends JApplet { private String title = "Unsaved Changes"; public Test() { contentPane.setLayout(new FlowLayout()); button.addActionListener(new ActionListener() { switch(result) { 14.3.5 输入对话框 例14-9 有文本域的输入对话框 import java.awt.*; public class Test extends JApplet { public Test() { contentPane.setLayout(new FlowLayout()); button.addActionListener(new ActionListener() { if(s == null) 例14-10 有组合框的输入对话框 import java.awt.*; public class Test extends JApplet { private String title = "Animal Selection Dialog"; public Test() { contentPane.setLayout(new FlowLayout()); button.addActionListener(new ActionListener() { if(s == null) 例14-11 带列表的输入对话框 import java.awt.*; public class Test extends JApplet { private String title = "Animal Selection Dialog"; public Test() { contentPane.setLayout(new FlowLayout()); button.addActionListener(new ActionListener() { if(s == null) 14.3.6 选项对话框 例14-12 使用一个选项对话框 import java.awt.*; public class Test extends JApplet { public Test() { contentPane.setLayout(new FlowLayout()); applyButton.addActionListener(new ActionListener() { switch(value) { private JRadioButton[] buttons = { for(int i=0; i < buttons.length; ++i) { add(buttons[i]); for(int i=0; i < buttons.length; ++i) { 14.3.7 JOptionPane属性 14.3.8 JOptionPane事件 例14-13 监听从选项窗格激发的 PropertyChangeEvents import java.awt.*; public class Test extends JApplet { private String title = "Update References"; public Test() { messagePanel.setBorder( messagePanel.setLayout(new BoxLayout(messagePanel, for(int i=0; i < checkBoxes.length; ++i) final JOptionPane pane = new JOptionPane( contentPane.setLayout(new FlowLayout()); button.addActionListener(new ActionListener() { dialog.show(); // blocks Integer value = (Integer)pane.getValue(); if(value.intValue() == JOptionPane.OK_OPTION) if(name.equals(JOptionPane.VALUE_PROPERTY)) 14.3.9 JOptionPane类总结 例14-14 构造各种配置的选项窗格 import java.awt.*; public class Test extends JApplet { JOptionPane defaultPane = new JOptionPane(); JOptionPane messagePane = new JOptionPane( JOptionPane objectPane = new JOptionPane(objects); JOptionPane messageTypePane = new JOptionPane( JOptionPane messageAndOptionTypePane = new JOptionPane( JOptionPane messageOptionAndIconPane = new JOptionPane( Object[] options = { contentPane.setLayout(new BoxLayout(contentPane, 14.3.10 AWT兼容 14.4 本章回顾 略
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
属性 窗口 窗体 对话框
─────────────────────────────────
模态 否 否 否/CSG
可调整大小 否 否/SG 是/SG
标题栏 否 是 是
边框 否 是 是
标题 否 是/CSG 是/CSG
菜单栏 否 是/SG 否
焦点管理器 是 是 是
警告字符串 是/G 是/G 是/G
图标图像③ 否 是/SG 否
链接到一个窗体 是 否 是
─────────────────────────────────
①是/否指缺省的属性状态
②C=在构造时可设置,S=可使用的设置方法,G=可使用的获取方法(即get...()或is...())
③不是所有的平台都支持窗口的图标化。
import java.awt.event.*;
import javax.swing.*;
// to display the animated swing.gif in a window in the corner
// of your desktop. A double click closes the window.
Toolkit toolkit = Toolkit.getDefaultToolkit();
JWindow window = new JWindow();
JLabel label = new JLabel(new ImageIcon("swing.gif"));
JFrame frame = new Test();
}
public Test() {
//label.setBorder(BorderFactory.createRaisedBevelBorder());
window.getContentPane().add(label, "Center");
//centerWindow();
// change location to suite taste ...
window.setLocation(75,10);
window.pack();
window.show();
public void mousePressed(MouseEvent e) {
if(e.getClickCount() == 2) {
window.dispose();
System.exit(0);
}
}
});
}
private void centerWindow() {
Dimension scrnSize = toolkit.getScreenSize();
Dimension labelSize = label.getPreferredSize();
int labelWidth = labelSize.width,
labelHeight = labelSize.height;
scrnSize.height/2 - (labelHeight/2));
window.pack();
}
}
import java.awt.event.*;
import javax.swing.*;
JWindow window = new JWindow();
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem quitItem;
final Container contentPane = getContentPane();
JButton button = new JButton("show window ...");
contentPane.add(button);
fileMenu.add("Open ...");
fileMenu.add("Save");
fileMenu.add("Save As ...");
fileMenu.addSeparator();
fileMenu.add(quitItem = new JMenuItem("Quit"));
window.getRootPane().setBorder(
BorderFactory.createRaisedBevelBorder());
public void actionPerformed(ActionEvent e) {
Point pt = contentPane.getLocation();
pt, contentPane);
window.show();
public void actionPerformed(ActionEvent e) {
window.dispose();
}
});
}
});
}
}
import java.awt.event.*;
import javax.swing.*;
private ConstraintsPanel cp = new ConstraintsPanel();
private JPanel buttonsPanel = new JPanel();
okButton = new JButton("OK"),
applyButton = new JButton("Apply"),
cancelButton = new JButton("Cancel");
okButton, applyButton, cancelButton,
};
"Constraints Dialog", // title
true); // modal
Container contentPane = getContentPane();
Container dialogContentPane = dialog.getContentPane();
contentPane.add(showButton);
dialogContentPane.add(buttonsPanel, BorderLayout.SOUTH);
dialog.pack();
// because dialog placement is based on dialog size.
// Because the applet is not yet showing, calling
// setLocationRelativeTo() here causes the dialog to be
// shown centered on the screen.
//
// If setLocationRelativeTo() is not invoked, the dialog
// will be located at (0,0) in screen coordinates.
//dialog.setLocationRelativeTo(this);
buttonsPanel.add(buttons[i]);
}
addButtonListeners();
}
private void addButtonListeners() {
showButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// calling setLocationRelativeTo() here causes
// the dialog ito be centered over the applet.
dialog.setLocationRelativeTo(Test.this);
dialog.show();
}
});
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showStatus("OK button Activated");
dialog.dispose();
}
});
applyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showStatus("Apply button Activated");
}
});
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showStatus("Cancel button Activated");
dialog.dispose();
}
});
}
}
import java.awt.event.*;
import javax.swing.*;
private JButton topButton = new JButton(
"show dialog created from option pane");
private JButton bottomButton = new JButton(
"show dialog created with static method");
private String message = "message";
Container contentPane = getContentPane();
contentPane.add(topButton);
contentPane.add(bottomButton);
public void actionPerformed(ActionEvent e) {
JOptionPane optionPane = new JOptionPane(
message, // message
JOptionPane.INFORMATION_MESSAGE); // messageType
topButton, // parentComponent
title); // title
}
});
bottomButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(
bottomButton, // parentComponent
message, // message
title, // title
JOptionPane.INFORMATION_MESSAGE); // messageType
}
});
}
}
import java.awt.event.*;
import javax.swing.*;
private JButton button = new JButton("show internal frame");
Container contentPane = getContentPane();
contentPane.add(button);
public void actionPerformed(ActionEvent e) {
JOptionPane.showInternalMessageDialog(
button, // parentComponent
"Break Time ...", // message
"Reminder!", // title
JOptionPane.INFORMATION_MESSAGE); // messageType
}
});
}
}
import java.awt.event.*;
import javax.swing.*;
private JButton button = new JButton("show dialog ...");
private String message = "information";
private int messageType = JOptionPane.INFORMATION_MESSAGE;
private String messages[] = {
"information", "error", "warning", "question", "plain"
};
Container contentPane = getContentPane();
JPanel controlPanel = new ControlPanel(this);
contentPane.add(controlPanel);
contentPane.add(button);
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(
button, // parentComponent
message, // message
title, // title
messageType);
}
});
}
public void setMessageType(int messageType) {
this.messageType = messageType;
case JOptionPane.INFORMATION_MESSAGE:
message = messages[0];
break;
case JOptionPane.ERROR_MESSAGE:
message = messages[1];
break;
case JOptionPane.WARNING_MESSAGE:
message = messages[2];
break;
case JOptionPane.QUESTION_MESSAGE:
message = messages[3];
break;
case JOptionPane.PLAIN_MESSAGE:
message = messages[4];
break;
}
}
}
class ControlPanel extends JPanel {
private JComboBox messageTypes = new JComboBox();
private int[] typeValues = {
JOptionPane.INFORMATION_MESSAGE,
JOptionPane.ERROR_MESSAGE,
JOptionPane.WARNING_MESSAGE,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.PLAIN_MESSAGE,
};
private String[] typeNames = {
"JOptionPane.INFORMATION_MESSAGE",
"JOptionPane.ERROR_MESSAGE",
"JOptionPane.WARNING_MESSAGE",
"JOptionPane.QUESTION_MESSAGE",
"JOptionPane.PLAIN_MESSAGE",
};
add(messageTypes);
messageTypes.addItem(typeNames[i]);
}
messageTypes.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String s = (String)messageTypes.getSelectedItem();
int type;
if(s.equals(typeNames[i]))
applet.setMessageType(typeValues[i]);
}
}
});
}
}
import java.awt.event.*;
import javax.swing.*;
private JButton button = new JButton("show dialog ...");
private String message = "Dinner time";
Container contentPane = getContentPane();
contentPane.add(button);
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(
button, // parentComponent
message, // message
title, // title
JOptionPane.INFORMATION_MESSAGE,// messageType
new ImageIcon("dining.gif")); // icon
}
});
}
}
import java.awt.event.*;
import javax.swing.*;
private JButton button = new JButton("show dialog ...");
private String message[] = {
"Unsaved Changes in File: dialog.fm",
" ",
"Save before closing?",
" ",
};
Container contentPane = getContentPane();
contentPane.add(button);
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(
button, // parentComponent
message, // message
title, // title
JOptionPane.YES_NO_CANCEL_OPTION, // optionType
JOptionPane.WARNING_MESSAGE, // messageType
new ImageIcon("punch.gif")); // icon
case JOptionPane.CLOSED_OPTION:
showStatus("Dialog Closed");
break;
case JOptionPane.YES_OPTION:
showStatus("Yes");
break;
case JOptionPane.NO_OPTION:
showStatus("No");
break;
case JOptionPane.CANCEL_OPTION:
showStatus("Cancel");
break;
}
}
});
}
}
import java.awt.event.*;
import javax.swing.*;
private JButton button = new JButton("show dialog ...");
private String message = "Please Enter Your Name";
Container contentPane = getContentPane();
contentPane.add(button);
public void actionPerformed(ActionEvent e) {
String s = JOptionPane.showInputDialog(message);
showStatus("cancel button activated");
else
showStatus("Name: " + s);
}
});
}
}
import java.awt.event.*;
import javax.swing.*;
private JButton button = new JButton("show dialog ...");
private String message = "Select your favorite animal:";
private String[] selectionValues = {
"dog", "cat", "mouse", "goat", "koala", "rabbit",
};
Container contentPane = getContentPane();
contentPane.add(button);
public void actionPerformed(ActionEvent e) {
String s = (String)JOptionPane.showInputDialog(
Test.this, // parentComponent
message, // message
title, // title
JOptionPane.QUESTION_MESSAGE, // messageType
null, // icon
selectionValues, // selectionValues
selectionValues[3]); // initialValue
showStatus("cancel button activated");
else
showStatus(s);
}
});
}
}
import java.awt.event.*;
import javax.swing.*;
private JButton button = new JButton("show dialog ...");
private String message = "Select your favorite animal:";
private Object[] selectionValues = {
"dog", "cat", "mouse", "goat", "koala", "rabbit",
"mouse", "horse", "kangaroo", "iguana", "tiger", "lion",
"eagle", "vulture", "wolf", "coyote", "owl", "snake",
"shrew", "zebra", "wildebeast"
};
Container contentPane = getContentPane();
contentPane.add(button);
public void actionPerformed(ActionEvent e) {
String s = (String)JOptionPane.showInputDialog(
Test.this, // parentComponent
message, // message
title, // title
JOptionPane.QUESTION_MESSAGE, // messageType
null, // icon
selectionValues, // selectionValues
selectionValues[3]); // initialValue
showStatus("cancel button activated");
else
showStatus(s);
}
});
}
}
import java.awt.event.*;
import javax.swing.*;
private JButton button = new JButton("show dialog ...");
private JButton applyButton = new JButton("Apply");
private RotatePanel rotatePanel = new RotatePanel();
private String title = "Rotate";
private Object[] buttonRowObjects = new Object[] {
"Ok",
applyButton,
"Cancel",
};
Container contentPane = getContentPane();
contentPane.add(button);
public void actionPerformed(ActionEvent e) {
showStatus(rotatePanel.getSelectedAngle() +
" degrees");
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int value = JOptionPane.showOptionDialog(
button, // parentComponent
rotatePanel, // message
title, // title
JOptionPane.DEFAULT_OPTION, // optionType
JOptionPane.PLAIN_MESSAGE, // messageType
null, // icon
buttonRowObjects, // options
applyButton); // initialValue
case JOptionPane.CLOSED_OPTION:
showStatus(
"Dialog closed with close box");
break;
case JOptionPane.OK_OPTION:
showStatus("Ok button activated: " +
rotatePanel.getSelectedAngle() +
" degrees");
break;
case JOptionPane.CANCEL_OPTION:
showStatus("Cancel button activated");
break;
}
}
});
}
}
class RotatePanel extends JPanel {
private ButtonGroup group = new ButtonGroup();
new JRadioButton("0"),
new JRadioButton("90"),
new JRadioButton("180"),
new JRadioButton("270"),
};
public RotatePanel() {
setBorder(BorderFactory.createTitledBorder("Angle:"));
if(i ==0)
buttons[i].setSelected(true);
group.add(buttons[i]);
}
}
public String getSelectedAngle() {
String rv = null; // rv = return value
if(buttons[i].isSelected())
rv = buttons[i].getText();
}
return rv;
}
}
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
private JButton button = new JButton("show dialog ...");
private JPanel messagePanel = new JPanel();
private JCheckBox[] checkBoxes = {
new JCheckBox("All Cross-References"),
new JCheckBox("Text Insets Marked for Manual Update"),
new JCheckBox("Text Insets Marked for Automatic Update"),
new JCheckBox("OLE Links Marked for Manual Update"),
new JCheckBox("OLE Links Marked for Automatic Update"),
};
Container contentPane = getContentPane();
BorderFactory.createTitledBorder("Update:"));
BoxLayout.Y_AXIS));
messagePanel.add(checkBoxes[i]);
messagePanel, // message
JOptionPane.QUESTION_MESSAGE, // messageType
JOptionPane.OK_CANCEL_OPTION); // optionType
contentPane.add(button);
public void actionPerformed(ActionEvent e) {
JDialog dialog = pane.createDialog(
Test.this, // parentComponent
title); // title
updateReferences();
else
showStatus("dialog canceled");
}
});
pane.addPropertyChangeListener(
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
String name = e.getPropertyName();
System.out.println(name + ":" + e.getNewValue());
}
});
}
private void updateReferences() {
showStatus("updating references");
}
}
import java.awt.event.*;
import javax.swing.*;
public Test() {
Container contentPane = getContentPane();
Object[] objects = new Object[] {
new JLabel("JOptionPane(Object message)",
new ImageIcon("beach_umbrella.gif"),
JLabel.LEFT),
new JCheckBox("check me out"),
new VerboseObject(),
};
Object[] objects2 = new Object[] {
"JOptionPane(Object message, int messageType)",
"messageType = JOptionPane.INFORMATION_MESSAGE",
};
Object[] objects3 = new Object[] {
"JOptionPane(Object message, " +
"int messageType, int optionsType)",
"messageType = JOptionPane.QUESTION_MESSAGE",
"optionType = JOptionPane.YES_NO_CANCEL_OPTION",
};
Object[] objects4 = new Object[] {
"JOptionPane(Object message, " +
"int messageType, int optionsType, Icon icon)",
"messageType = JOptionPane.WARNING_MESSAGE",
"optionType = JOptionPane.OK_CANCEL_OPTION",
"icon = new ImageIcon(/"ballot_box.gif/")",
};
Object[] objects5 = new Object[] {
"JOptionPane(Object message, " +
"int messageType, int optionsType, Icon icon)",
"messageType = JOptionPane.ERROR_MESSAGE",
"optionType = JOptionPane.OK_CANCEL_OPTION",
"icon = null",
"options = /"button 1/", /"button 2/", " +
"new JButton(/"button 3/")",
};
"JOptionPane(Object message)");
objects2, JOptionPane.INFORMATION_MESSAGE);
objects3, JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_CANCEL_OPTION);
objects4, JOptionPane.WARNING_MESSAGE,
JOptionPane.OK_CANCEL_OPTION,
new ImageIcon("ballot_box.gif"));
"button 1", "button 2", new JButton("button 3"),
};
JOptionPane messageOptionIconAndOptionsPane =
new JOptionPane(
objects5, JOptionPane.ERROR_MESSAGE,
JOptionPane.OK_CANCEL_OPTION,
null,
options,
options[2]);
BoxLayout.Y_AXIS));
contentPane.add(defaultPane);
contentPane.add(new JSeparator());
contentPane.add(messagePane);
contentPane.add(new JSeparator());
contentPane.add(objectPane);
contentPane.add(new JSeparator());
contentPane.add(messageTypePane);
contentPane.add(new JSeparator());
contentPane.add(messageAndOptionTypePane);
contentPane.add(new JSeparator());
contentPane.add(messageOptionAndIconPane);
contentPane.add(new JSeparator());
contentPane.add(messageOptionIconAndOptionsPane);
}
}
class VerboseObject extends Object {
public String toString() {
return "This is what you'll see in the option pane";
}
}
- 第14章 窗口和对话框
- 窗口和对话框
- 窗口和对话框
- js弹出窗口和对话框
- js弹出窗口和对话框
- 窗口和对话框居中对齐
- 窗口和对话框居中对齐
- 窗口和对话框居中对齐
- 第3章 窗口和消息
- 第11章 对话框
- 第 11 章 对话框
- JavaScript权威指南_107_第14章_Window对象_14.8-多窗口和窗体-打开和关闭窗口
- 用模态窗口 模拟 alert 和 confirm 对话框
- 一千个是什么 - Windows窗口、控件和对话框
- JSP弹出窗口和模式对话框
- [转]JSP弹出窗口和模式对话框
- extjs4.0的窗口和对话框
- js“弹出对话框”和“弹出窗口”详解
- 第12章 轻量容器(续)
- 第13章 滚动
- Java SE -- 多线程 线程终止
- 如果上帝为你开错了窗
- 详述USB OTG发展及协议标准
- 第14章 窗口和对话框
- 转载 Makefile详解(超级好)
- EVC基础
- 付出就有回报,坚持才会胜利
- JCOM.jar Java和COM桥梁的框架组件
- c++的转换
- UNIX网络编程
- 第15章 内部窗体和桌面窗格
- 第16章 选取器