使用JDialog组件:
JDialog的类层次结构图:
java.lang.Object
--java.awt.Component
--java.awt.Container
--java.awt.Window
--java.awt.JDialog
--javax.swing.JDialog
如果你为公司设计一套“物品工具借用系统”,在这个系统中,借用物品的员工必须详细填员工编号、借用物品、借用器具、借
用日期、预计归还日期时间、借用原因等等,若没有详细填这此数据,就无法取得准许借用物品的证明文件。因此在设计这套系统
的过程中,你必须查看用户是否已经填妥相关数据,若员工忘记了填写某些重要字段,系统应该给予警示,提醒用户哪些字段必须
填写。这个情况也常发生在网络问卷或网络会员注册系统,用户必须填写相关数据,例如用户若没有填写E-Mail邮件地址,则系统
会提示你应当填写邮件地址,否则系统将不处理用户填写的信息。
为应付这种情况,java提供了JDialog与JOptionPane供我们使用,事实上,JOptionPane是阳春版的JDialog,当你在使用
JOptionPane时,系统会自动产生JDialog组件,并将JOptionPane的内容放入JDialog的ContentPane中,而这些均由系统在背后自动
运行,并不需要由我们介入。使用JOptionPane的好处是此组件已经默认了许多交互方式,你只用设置想要的显示模式,JOptionPane
就能轻易的显示出来,可说相当方便,若这些模式还是无法满足你的需求,你就可以使用JDialog来自行设计你的对话框。
我们先来看如何构造JDialog,JOptionPane将在后半段介绍:
JDialog构造函数:
JDialog():建立一个non-modal的对话框,没有title也不属于任何事件窗口组件。
JDialog(Dialog owner):建立一个属于Dialog组件的对话框,为non-modal形式,也没有title.
JDialog(Dialog owner,Boolean modal):建立一个属于Dialog组件的对话框,可决定modal形式,但没有title.
JDialog(Dialog owner,String title):建立一个属于Dialog组件的对话框,为non-modal形式,对话框上有title.
JDialog(Dialog owner,String title,Boolean modal):建立一个属于Dialog组件的对话框,可决定modal形式,且对话框上有
title.
JDialog(Frame owner):建立一个属于Frame组件的对话框,为non-modal形式,也没有title.
JDialog(Frame owner,Boolean modal):建立一个属于Frame组件的对话框,可决定modal形式,但没有title.
JDialog(Frame owner,String title):建立一个属于Frame组件的对话框,为non-modal形式,对话框上有title.
JDialog(Frame owner,String title,Boolean modal):建立一个属于Frame组件的对话框,可决定modal形式,且对话框上有title.
上面所说的modal是一种对话框操作模式,当modal为true时,代表用户必须结束对话框才能回到原来所属的窗口。当modal为
false时,代表对话框与所属窗口可以互相切换,彼此之间在操作上没有顺序性。
一般而言对话框都会依附在某个窗口上,例如JFrame或JDialog,原因在于对话框通常是一个程序运行的过程中与用户互动的中
间过程,在使用JDialog上跟JFrame非常相似,由上面的JDialog层次结构图中你可以发现,JDialog是继承AWT的Dialog类而来,因
此JDialog为一个Heavyweight组件。要加入组件到JDialog上与JFrame是一样的,你必须先取得JDialog的ContentPane,然后再把组
件加到此ContentPane中,JDialog默认的版面管理器是BorderLayout.
11-1-1:在JFrame上建立JDialog:
我们来看一个JDialog的例子,在这个例子中,用户在主窗口按下"借用物品"的按钮时,会跳出一个让用户填写窗体的JDialog
窗口,用户必须结束此JDialog窗口后才能返回主窗口.
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import com.incors.plaf.alloy.*;
import com.incors.plaf.alloy.themes.glass.*;
public class DialogDemo implements ActionListener{
JFrame f=null;
public void actionPerformed(ActionEvent e){
String cmd=e.getActionCommand();
if (cmd.equals("借用物品")){
new LendingSystem(f);
}else if (cmd.equals("离开系统")){
System.exit(0);
}
}
public DialogDemo(){
f=new JFrame("JDialog Example");
Container contentPane=f.getContentPane();
JPanel buttonPanel=new JPanel();
JButton b=new JButton("借用物品");
b.addActionListener(this);
buttonPanel.add(b);
b=new JButton("离开系统");
b.addActionListener(this);
buttonPanel.add(b);
buttonPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.blue,2),
"借用物品系统",TitledBorder.CENTER,TitledBorder.TOP));
contentPane.add(buttonPanel,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args){
SwingUtil.setLookAndFeel();
new DialogDemo();
}
}
class LendingSystem implements ActionListener{
JTextField staffField,objectField,borrowDateField,returnDateField,reasonField;
JDialog dialog;
public void actionPerformed(ActionEvent e){
String cmd=e.getActionCommand();
if (cmd.equals("确定")){
}else if (cmd.equals("取消")){
dialog.dispose();
}
}
LendingSystem(JFrame f){
dialog = new JDialog(f,"借用物品",true);
GridBagConstraints c;
int gridx,gridy,gridwidth,
gridheight,anchor,fill,ipadx,ipady;
double weightx,weighty;
Insets inset;
GridBagLayout gridbag = new GridBagLayout();
Container dialogPane = dialog.getContentPane();
dialogPane.setLayout(gridbag);
JLabel label = new JLabel("员工编号 : ");
gridx=0; //第0列
gridy=0; //第0行
gridwidth = 1; //占一单位宽度
gridheight = 1; //占一单位高度
weightx = 0; //窗口增大时组件宽度增大比率0
weighty = 0; //窗口增大时组件高度增大比率0
anchor = GridBagConstraints.CENTER; //容器大于组件size时将组件置于容器中央
fill = GridBagConstraints.BOTH; //窗口拉大时会填满水平与垂直空间
inset = new Insets(0,0,0,0); //组件间间距
ipadx = 0; //组件内水平宽度
ipady = 0; //组件内垂直高度
c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
weightx,weighty,anchor,fill,inset,ipadx,ipady);
gridbag.setConstraints(label,c);
dialogPane.add(label);
label = new JLabel("借用器具 : ");
gridx=3;
gridy=0;
c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
weightx,weighty,anchor,fill,inset,ipadx,ipady);
gridbag.setConstraints(label,c);
dialogPane.add(label);
label = new JLabel("借用日期: ");
gridx=0;
gridy=1;
c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
weightx,weighty,anchor,fill,inset,ipadx,ipady);
gridbag.setConstraints(label,c);
dialogPane.add(label);
label = new JLabel("归还日期: ");
gridx=3;
gridy=1;
c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
weightx,weighty,anchor,fill,inset,ipadx,ipady);
gridbag.setConstraints(label,c);
dialogPane.add(label);
label = new JLabel("借用原因 : ");
gridx=0;
gridy=2;
c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
weightx,weighty,anchor,fill,inset,ipadx,ipady);
gridbag.setConstraints(label,c);
dialogPane.add(label);
staffField = new JTextField();
gridx=1;
gridy=0;
gridwidth = 2;
gridheight = 1;
weightx = 1;
weighty = 0;
c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
weightx,weighty,anchor,fill,inset,ipadx,ipady);
gridbag.setConstraints(staffField,c);
dialogPane.add(staffField);
objectField = new JTextField();
gridx=4;
gridy=0;
c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
weightx,weighty,anchor,fill,inset,ipadx,ipady);
gridbag.setConstraints(objectField,c);
dialogPane.add(objectField);
borrowDateField = new JTextField();
gridx=1;
gridy=1;
c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
weightx,weighty,anchor,fill,inset,ipadx,ipady);
gridbag.setConstraints(borrowDateField,c);
dialogPane.add(borrowDateField);
returnDateField = new JTextField();
gridx=4;
gridy=1;
c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
weightx,weighty,anchor,fill,inset,ipadx,ipady);
gridbag.setConstraints(returnDateField,c);
dialogPane.add(returnDateField);
reasonField = new JTextField();
gridx=1;
gridy=2;
gridwidth = 5;
c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
weightx,weighty,anchor,fill,inset,ipadx,ipady);
gridbag.setConstraints(reasonField,c);
dialogPane.add(reasonField);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,2));
JButton b = new JButton("确定");
panel.add(b);
b = new JButton("取消");
b.addActionListener(this);
panel.add(b);
gridx=0;
gridy=3;
gridwidth = 6;
weightx = 1;
weighty = 1;
c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
weightx,weighty,anchor,fill,inset,ipadx,ipady);
gridbag.setConstraints(panel,c);
dialogPane.add(panel);
dialog.setBounds(200,150,400,130);
dialog.show();
}
}
class SwingUtil{
public static final void setLookAndFeel() {
try{
Font font = new Font("JFrame", Font.PLAIN, 12);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font) {
UIManager.put(key, font);
}
}
AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
AlloyTheme theme = new GlassTheme();
LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);
UIManager.setLookAndFeel(alloyLnF);
}catch(UnsupportedLookAndFeelException ex){
ex.printStackTrace();
}
/*
try{
Font font = new Font("JFrame", Font.PLAIN, 12);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font) {
UIManager.put(key, font);
}
}
UIManager.setLookAndFeel("com.stefankrause.xplookandfeel.XPLookAndFeel");
}catch(Exception e){
e.printStackTrace();
}*/
}
}
11-2:使用JOptionPane类的静态方法:
JOptionPane的类层次结构图:
java.lang.Object
--java.awt.Component
--java.awt.Container
--javax.swing.JComponent
--javax.swing.JOptionPane
以JDialog来制作对话框,你必须实作对话框中的每一个组件,但有时候我们的对话框只是要显示一段文字,或是一些简单的
选择(是或否),这时候可以利用JOptionPane类,它可以让你很简单的做出这样的效果,不仅大大的减少了程序代码的编写,也
让整个程序看起来清爽许多!
JOptionPane的构造函数:
JOptionPane():建立一个显示测试信息的JOptionPane组件。
JOptionPane(Object message):建立一个显示特定信息的JOptionPane组件。
JOptionPane(Object message,int messageType):建立一个显示特定信息的JOptionPane组件,并设置信息类型。
JOptionPane(Object message,int messageType,int optionType):建立一个显示特定信息的JOptionPane组件,并设置信息与选项.
JOptionPane(Object message,int messageType,int optionType,Icon icon):建立一个显示特定信息的JOptionPane组件,并设置
信息与选项,且可显示出图案.
JOptionPane(Object message,int messageType,int optionType,Icon icon,Object[] options):建立一个显示特定信息的
JOptionPane组件,并设置信息与选项,且可显示出图案.选项值是一个Object Array,可
用作更改按钮上的文字.
JOptionPane(Object message,int messageType,int optionType,Icon icon,Object[] options,Object initialValue):建立一个
显示特定信息的JOptionPane组件,并设置信息与选项类型,且可以显示出图案.选项值是
一个Object Array,可用作更改按钮上的文字,并设置默认按钮.
使用JOptionPane对象所得到的对话框是modal为true形式,也就是说我们必须先关闭对话框窗口才能回到产生对话框的母窗口上
.
要利用JOptionPane类来输出对话框,通常我们不会new一个JOptionPane对象出来,而是使用JOptionPane所提供的一些静态方法,
不用产生JOptionPane对象就可以直接使用,这些方法方法都是以showXxxxxDialog的形式出现,若你的对话框是出现在
InternaleFrame上,你可以用showInternalXxxxxDialog的各种方法产生对话框.以下我们整理出JOptionPane提供输出对话框的所有
静态方法:
Message Dialog
----------------------------------------------------------------------------------------------------------
方法: |void showMessageDialog(Component parentComponent,Object message)
|void showMessageDialog(Component parentComponent,Object message,String title,int messageType)
|void showMessageDialog(Component parentComponent,Object message,String title,int messageType,Icon
| icon)
|void showInternalMessageDialog(Component parentComponent,Object message)
|void showInternalMessageDialog(Component parentComponent,Object message,String title,int messageType)
|void showInternalMessageDialog(Component parentComponent,Object message,String title,int messageType
| ,Icon icon)
------------------------------------------------------------------------------------------------------------
说明: |显示信息对话框,对话框中只含有一个按钮,通常是"确定"按钮,例如安装完某个软件时通常会跳出一个对话框告知你
|安装已经成功.这类的方法有5种参数:
|parentComponent:是指产生对话框的组件为何,通常是指Frame或Dialog组件.
| message:是指要显示的组件,通常是String或Label类型
| title:对话框标题列上显示的文字.
| messageType:指定信息类型,共有5种类型,分别是ERROR_MESSAGE,INFORMATION_MESSAGE,WARING_MESSAGE,
| QUESTION_MESSAGE,PLAIN_MESSAGE(不显示图标).指定类型后对话框就会出现相对应的图标.
| icon:若你不喜欢java给的图标,你可以自己自定图标.
------------------------------------------------------------------------------------------------------------
Confirm Dialog
------------------------------------------------------------------------------------------------------------
方法: |int showConfirmDialog(Component parentComponent,Object message)
|int showConfirmDialog(Component parentComponent,Object message,String title,int optionType)
|int showConfirmDialog(Component parentComponent,Object message,String title,int optionType,
| int messageType)
|int showConfirmDialog(Component parentComponent,Object message,String title,int optionType,
| int messageType,Icon icon)
|int showInternalConfirmDialog(Component parentComponent,Object message)
|int showInternalConfirmDialog(Component parentComponent,Object message,String title,int optionType)
|int showInternalConfirmDialog(Component parentComponent,Object message,String title,int optionType
| ,int messageType)
|int showInternalConfirmDialog(Component parentComponent,Object message,String title,int optionType
| ,int messageType,Icon icon)
-----------------------------------------------------------------------------------------------------------
说明: |显示确认对话框,这类的对话框通常会问用户一个问题,然后用户回答是或不是,例如当我们修改了某个文件的内容
|却没存盘就要离开时,系统大部份都会跳出确认对话框.询问我们是否要存储修改过的内容.确认对话框方法有6种
|参数:
|parentComponent:是指产生对话框的组件为何,通常是指Frame或Dialog组件.
| message:是指要显示的组件,通常是String或Label类型
| title:对话框标题列上显示的文字.
| optionType:确定按钮的类型,有5种类型,分别是DEFAULT_OPTION,YES_NO_OPTION,YES_NO_CANCEL,与
| OK_CANCEL_OPTION.
| messageType:指定信息类型,共有5种类型,分别是ERROR_MESSAGE,INFORMATION_MESSAGE,WARING_MESSAGE,
| QUESTION_MESSAGE,PLAIN_MESSAGE(不显示图标).指定类型后对话框就会出现相对应的图标.
| icon:若你不喜欢java给的图标,你可以自己自定图标.
| 返回值为一整数值,依用户按下什么钮而定,YES_OPTION=0,NO_OPTION=1,CANCEL_OPTION=2,OK_OPTION=0
|,CLOSED_OPTION=-1(当用户都不选直接关掉对话框时)
----------------------------------------------------------------------------------------------------------
Input Dialog
-----------------------------------------------------------------------------------------------------------
方法: |String showInputDialog(Object message)
|String showInputDialog(Component parentComponent,Object message)
|String showInputDialog(Component parentComponent,Object message,String title,int messageType)
|Object showInputDailog(Component parentComponent,Object message,String title,int messageType,Icon icon
| ,Object[] selectionValues,Object initialSelectionValue)
|String showInternalInputDialog(Object message)
|String showInternalInputDialog(Component parentComponent,Object message)
|String showInternalInputDialog(Component parentComponent,Object message,String title,int messageType)
|Object showInternalInputDialog(Component parentComponent,Object message,String title,int messageType,
| Icon icon,Object[] selectionValues,Object initialSelectionValue)
-----------------------------------------------------------------------------------------------------------
说明: |显示输入对话框,这类的对话框可以让用户输入相关的信息,当用户按下确定钮后,系统会得到用户所输入的信息.输入
|对话框不仅可以让用户输入文字,也可以提供Combo Box组件让用户选择相关信息,避免用户输入错误.输入对话框方法
|有6种参数:
|parentComponent:是指产生对话框的组件为何,通常是指Frame或Dialog组件.
| message:是指要显示的组件,通常是String或Label类型
| title:对话框标题列上显示的文字.
| messageType:指定信息类型,共有5种类型,分别是ERROR_MESSAGE,INFORMATION_MESSAGE,WARING_MESSAGE,
| QUESTION_MESSAGE,PLAIN_MESSAGE(不显示图标).指定类型后对话框就会出现相对应的图标.
| icon:若你不喜欢java给的图标,你可以自己自定图标.
|selectionValues:给用户选择的可能值.Object Array中的数据会以ComboBox方式显示出来.
|initialSelectionValue:对话框初始化时所显示的值.
| 当用户按下确定按钮时会返回用户输入的信息,若按下取消按钮则返回null.
-------------------------------------------------------------------------------------------------------------
Option Dialog
-------------------------------------------------------------------------------------------------------------
方法: |int showOptionDialog(Component parentComponent,Object message,String title,int optionType,int
| messageType,Icon icon,Object[] options,Object initalValue)
|int showInternalOptionDialog(Component parentComponent,Object message,String title,int optionType,
| int messageType,Icon icon,Object[] options,Object initialValue)
-------------------------------------------------------------------------------------------------------------
说明: |显示选择对话框,这类的对话框可以让用户自定义对话类型,最大的好处是可以改变按钮上的文字.选择对话框方法有
|6种参数:
|parentComponent:是指产生对话框的组件为何,通常是指Frame或Dialog组件.
| message:是指要显示的组件,通常是String或Label类型
| title:对话框标题列上显示的文字.
| optionType:确定按钮的类型,有5种类型,分别是DEFAULT_OPTION,YES_NO_OPTION,YES_NO_CANCEL,与
| OK_CANCEL_OPTION.
| messageType:指定信息类型,共有5种类型,分别是ERROR_MESSAGE,INFORMATION_MESSAGE,WARING_MESSAGE,
| QUESTION_MESSAGE,PLAIN_MESSAGE(不显示图标).指定类型后对话框就会出现相对应的图标.
| icon:若你不喜欢java给的图标,你可以自己自定图标.
| options:给用户选择的按钮显示文字.
| initalValue:对话框初始化时按钮默认值.
| 返回值为一整数值,依用户按下什么钮而定,YES_OPTION=0,NO_OPTION=1,CANCEL_OPTION=2,OK_OPTION=0
|,CLOSED_OPTION=-1(当用户都不选直接关掉对话框时)
------------------------------------------------------------------------------------------------------------
上面表格看起来好像很多方法似的,但实际上只区分成四大类,你只需要选择要用哪类的对话框,再决定使用那类中的哪个方法即
可.我们慢慢来为你介绍这四个种类的对话框.
11-2-1:输出Message Dialog
Message Dialog是在对话框上显示出一段信息,目的在告知用户一些相关信息,因此Message Dialog只会有一个确定按钮,让用户
看完信息后就可以关闭这个对话框.下面这个例子我们使用Message对话框,我们来看看不同的MessageType会有什么样的图案产生.
MessageDialog.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import com.incors.plaf.alloy.*;
import com.incors.plaf.alloy.themes.glass.*;
public class MessageDialog implements ActionListener{
JFrame f=null;
public MessageDialog(){
f=new JFrame("optionPane");
Container contentPane=f.getContentPane();
contentPane.setLayout(new GridLayout(2,3));
JButton b=new JButton("Show Error Icon");
b.addActionListener(this);
contentPane.add(b);
b=new JButton("Show Information Icon");
b.addActionListener(this);
contentPane.add(b);
b=new JButton("Show Waring Icon");
b.addActionListener(this);
contentPane.add(b);
b=new JButton("Show Question Icon");
b.addActionListener(this);
contentPane.add(b);
b=new JButton("Show Plain Icon");
b.addActionListener(this);
contentPane.add(b);
b=new JButton("Show User Define Icon");
b.addActionListener(this);
contentPane.add(b);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args){
SwingUtil.setLookAndFeel();
new MessageDialog();
}
public void actionPerformed(ActionEvent e){
String cmd=e.getActionCommand();
String title="Message Dialog";
String message="";
int type=JOptionPane.PLAIN_MESSAGE;
if (cmd.equals("Show Error Icon")){
type=JOptionPane.ERROR_MESSAGE;
message="Error Message";
}else if (cmd.equals("Show Information Icon")){
type=JOptionPane.INFORMATION_MESSAGE;
message="information Message";
}else if (cmd.equals("Show Waring Icon")){
type=JOptionPane.WARNING_MESSAGE;
message="Waring Message";
}else if (cmd.equals("Show Question Icon")){
type=JOptionPane.QUESTION_MESSAGE;
message="Question Message";
}else if (cmd.equals("Show Plain Icon")){
type=JOptionPane.PLAIN_MESSAGE;
message="Plain Message";
}else if (cmd.equals("Show User Define Icon")){
type=JOptionPane.PLAIN_MESSAGE;
JOptionPane.showMessageDialog(f,message,title,type,new ImageIcon("..\\icons\\glass.jpg"));
return ;
}
JOptionPane.showMessageDialog(f,message,title,type);
}
}
class SwingUtil{
public static final void setLookAndFeel() {
try{
Font font = new Font("JFrame", Font.PLAIN, 12);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font) {
UIManager.put(key, font);
}
}
AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
AlloyTheme theme = new GlassTheme();
LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);
JFrame.setDefaultLookAndFeelDecorated(true);
UIManager.setLookAndFeel(alloyLnF);
}catch(UnsupportedLookAndFeelException ex){
ex.printStackTrace();
}
}
}
11-2-2:输出Confirm Dialog:
看过了信息对话框,接着我们来看确认对话框到底怎样.Confirm Dialog目的在让用户对某个问题选择"Yes"或"No",可算是一种
相当简单的是非选择对话框.下面这个范例中,用户可选择不同按钮类型的确认对话框,为方便说明,在此我们将Message Type都默认
为JOptionPane.INFORMATION_MESSAGE.
ConfirmDialog.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import com.incors.plaf.alloy.*;
import com.incors.plaf.alloy.themes.glass.*;
public class ConfirmDialog implements ActionListener
{
JFrame f = null;
JLabel label = null;
public ConfirmDialog()
{
f = new JFrame("OptionPane Demo");
Container contentPane = f.getContentPane();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,2));
JButton b = new JButton("Show DEFAULT_OPTION");
b.addActionListener(this);
panel.add(b);
b = new JButton("Show YES_NO_OPTION");
b.addActionListener(this);
panel.add(b);
b = new JButton("Show YES_NO_CANCEL_OPTION");
b.addActionListener(this);
panel.add(b);
b = new JButton("Show OK_CANCEL_OPTION");
b.addActionListener(this);
panel.add(b);
label = new JLabel(" ",JLabel.CENTER);
contentPane.add(label,BorderLayout.NORTH);
contentPane.add(panel,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args)
{
SwingUtil.setLookAndFeel();
new ConfirmDialog();
}
public void actionPerformed(ActionEvent e)
{
//处理用户按钮事件,默认的messageType是JoptionPane.INFORMATION_MESSAGE.
String cmd = e.getActionCommand();
String title = "Confirm Dialog";
String message ="";
int messageType = JOptionPane.INFORMATION_MESSAGE;
int optionType = JOptionPane.YES_NO_OPTION;
if(cmd.equals("Show DEFAULT_OPTION")) {
optionType = JOptionPane.DEFAULT_OPTION;
message = "Show DEFAULT_OPTION Buttons";
} else if(cmd.equals("Show YES_NO_OPTION")) {
optionType = JOptionPane.YES_NO_OPTION;
message = "Show YES_NO_OPTION Buttons";
} else if(cmd.equals("Show YES_NO_CANCEL_OPTION")) {
optionType = JOptionPane.YES_NO_CANCEL_OPTION;
message = "Show YES_NO_CANCEL_OPTION Buttons";
} else if(cmd.equals("Show OK_CANCEL_OPTION")) {
optionType = JOptionPane.OK_CANCEL_OPTION;
message = "Show OK_CANCEL_OPTION Buttons";
}
int result = JOptionPane.showConfirmDialog(f, message,
title, optionType, messageType);
if (result == JOptionPane.YES_OPTION)
label.setText("您选择:Yes or OK");
if (result == JOptionPane.NO_OPTION)
label.setText("您选择:No");
if (result == JOptionPane.CANCEL_OPTION)
label.setText("您选择:Cancel");
if (result == JOptionPane.CLOSED_OPTION)
label.setText("您没做任何选择,并关闭了对话框");
}
}
class SwingUtil{
public static final void setLookAndFeel() {
try{
Font font = new Font("JFrame", Font.PLAIN, 12);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font) {
UIManager.put(key, font);
}
}
AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
AlloyTheme theme = new GlassTheme();
LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);
UIManager.setLookAndFeel(alloyLnF);
}catch(UnsupportedLookAndFeelException ex){
ex.printStackTrace();
}
}
}
11-2-3:输出Input Dialog:
Input Dialog可以让用户输入相关信息,当用户按下确定钮后,系统会得到用户所输入的信息.输入对话框不仅可以让用户自输入
文字,也可以显示出ComboBox组件让用户选择相关信息,避免用户输入错误,当用户输入完毕按下确定按钮时会返回用户输入的信息,
若按下取消则返回null值.下面为InputDialog的范例.
InputDialog.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import com.incors.plaf.alloy.*;
import com.incors.plaf.alloy.themes.glass.*;
public class InputDialog implements ActionListener
{
JFrame f = null;
JLabel label = null;
public InputDialog()
{
f = new JFrame("OptionPane Demo");
Container contentPane = f.getContentPane();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,2));
JButton b = new JButton("Show Text Input");
b.addActionListener(this);
panel.add(b);
b = new JButton("Show ComboBox Input");
b.addActionListener(this);
panel.add(b);
label = new JLabel(" ",JLabel.CENTER);
contentPane.add(label,BorderLayout.NORTH);
contentPane.add(panel,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args)
{
SwingUtil.setLookAndFeel();
new InputDialog();
}
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
String title = "Input Dialog";
String message ="您最熟悉哪一种程序语言?";
int messageType = JOptionPane.QUESTION_MESSAGE;
String[] values = {"JAVA","PHP","ASP","C++","VB"};
String result ="";
if(cmd.equals("Show Text Input")) {
result = JOptionPane.showInputDialog(f, message,
title, messageType);
} else if(cmd.equals("Show ComboBox Input")) {
result = (String)JOptionPane.showInputDialog(f, message,
title, messageType,null,values,values[0]);
}
if (result == null)
label.setText("您取消了对话框");
else{
label.setText("您输入:"+result);
}
}
}
class SwingUtil{
public static final void setLookAndFeel() {
try{
Font font = new Font("JFrame", Font.PLAIN, 12);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font) {
UIManager.put(key, font);
}
}
AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
AlloyTheme theme = new GlassTheme();
LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);
UIManager.setLookAndFeel(alloyLnF);
}catch(UnsupportedLookAndFeelException ex){
ex.printStackTrace();
}
}
}
11-2-3:输出Option Dialog:
Option Dialog可以让用户自定义对话框类型,比较具有弹性,最大的好处是可以改变按钮上的文字.我们来看下面的例子:
OptionDialog.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import com.incors.plaf.alloy.*;
import com.incors.plaf.alloy.themes.glass.*;
public class OptionDialog implements ActionListener
{
JFrame f = null;
JLabel label = null;
public OptionDialog()
{
f = new JFrame("OptionPane Demo");
Container contentPane = f.getContentPane();
JButton b = new JButton("Show Option Dialog");
b.addActionListener(this);
label = new JLabel(" ",JLabel.CENTER);
contentPane.add(label,BorderLayout.NORTH);
contentPane.add(b,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args)
{
SwingUtil.setLookAndFeel();
new OptionDialog();
}
public void actionPerformed(ActionEvent e)
{
String title = "Option Dialog";
String message ="您喜欢吃汉堡吗?";
int messageType = JOptionPane.QUESTION_MESSAGE;
//由于我们的optionType设置成JOptionPane.YES_NO_CANCEL_OPTION,因此对话框中会有三个按钮.我们在options
//的String Arrray中设置这三个按钮的名称,并以options[1]按钮为默认值,若将options参数设为null,系统会原来
//的按钮名称来显示.
int optionType = JOptionPane.YES_NO_CANCEL_OPTION;
String[] options = {"喜欢","不喜欢","取消"};
int result = JOptionPane.showOptionDialog(f, message, title,
optionType, messageType,null,options,options[1]);
if (result == JOptionPane.YES_OPTION)
label.setText("您选择:喜欢");
if (result == JOptionPane.NO_OPTION)
label.setText("您选择:不喜欢");
if (result == JOptionPane.CANCEL_OPTION)
label.setText("您选择:取消");
if (result == JOptionPane.CLOSED_OPTION)
label.setText("您没做任何选择,并关闭了对话框");
}
}
class SwingUtil{
public static final void setLookAndFeel() {
try{
Font font = new Font("JFrame", Font.PLAIN, 12);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font) {
UIManager.put(key, font);
}
}
AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
AlloyTheme theme = new GlassTheme();
LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);
UIManager.setLookAndFeel(alloyLnF);
}catch(UnsupportedLookAndFeelException ex){
ex.printStackTrace();
}
}
}
11-2-5:输出Internal Dialog:
我们之前曾经说过,JOptionPane也可以显示出Internal Dialog对话框,使用方法跟上面的范例一模一样,只是在方法名称上多
了Internal这个字眼,例如showInternalMessageDialog()等等,我们来看下面的范例:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import com.incors.plaf.alloy.*;
import com.incors.plaf.alloy.themes.glass.*;
public class InternalDialog implements ActionListener
{
JInternalFrame internalFrame = null;
JLabel label = null;
public InternalDialog()
{
JFrame f = new JFrame("OptionPane Demo");
Container contentPane = f.getContentPane();
JDesktopPane desktopPane = new JDesktopPane();
internalFrame = new JInternalFrame(
"Internal Frame", true,
true,
true,
true);
internalFrame.setLocation( 20,20);
internalFrame.setSize(200,200);
internalFrame.setVisible(true);
Container icontentPane = internalFrame.getContentPane();
JButton b = new JButton("Show Internal Dialog");
b.addActionListener(this);
icontentPane.add(b,BorderLayout.CENTER);
label = new JLabel(" ",JLabel.CENTER);
icontentPane.add(label,BorderLayout.NORTH);
desktopPane.add(internalFrame);
contentPane.add(desktopPane,BorderLayout.CENTER);
f.setSize(350, 350);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args)
{
SwingUtil.setLookAndFeel();
new InternalDialog();
}
public void actionPerformed(ActionEvent e)
{
String title = "Option Dialog";
String message ="您喜欢吃汉堡吗?";
int messageType = JOptionPane.QUESTION_MESSAGE;
int optionType = JOptionPane.YES_NO_CANCEL_OPTION;
String[] options = {"喜欢","不喜欢","取消"};
int result = JOptionPane.showInternalOptionDialog(internalFrame, message,
title, optionType, messageType,null,options,options[1]);
if (result == JOptionPane.YES_OPTION)
label.setText("您选择:喜欢");
if (result == JOptionPane.NO_OPTION)
label.setText("您选择:不喜欢");
if (result == JOptionPane.CANCEL_OPTION)
label.setText("您选择:取消");
if (result == JOptionPane.CLOSED_OPTION)
label.setText("您没做任何选择,并关闭了对话框");
}
}
class SwingUtil{
public static final void setLookAndFeel() {
try{
Font font = new Font("JFrame", Font.PLAIN, 12);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font) {
UIManager.put(key, font);
}
}
AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
AlloyTheme theme = new GlassTheme();
LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);
UIManager.setLookAndFeel(alloyLnF);
}catch(UnsupportedLookAndFeelException ex){
ex.printStackTrace();
}
}
}
读者在使用Internal Dialog时要特别注意,一般我们利用JOptionPane所产生的对话框均是modal为true状态,可是当你使用
Internal Dialog时对话框会变成modal为false状态.因此你可以不用关闭之前的对话框,就可以再按一次按钮,再产生一个Internal
Dialog.你再运行上一例操作后就知道了.
若你想将JInternalFrame内的Internal Dialog model设为true,你可以有下面两种解决方法:
1.你可以建立JOptionPane对象,而非直接去调用JOptionPane的static方法来输出对话框.然后利用JOptionPane的createDialog()方
法,取得JDialog对象,再利用Dialog(JDialog继承Dialog)所提供的setModal()方法将modal设为true.
2.直接使用JDialog.
11-3:使用JOptionPane组件建立对话框:
我们在11-2节中已经介绍了JOptionPane的构造函数,虽然大部份的情况下我们只需要使用JOptionPane的静态方法来产生对话框
,但如果你想直接使用JOptionPane对象来产生对话框,当然也可以.下面我们就举一个以JOptionPane对象产生对话框的范例:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import com.incors.plaf.alloy.*;
import com.incors.plaf.alloy.themes.glass.*;
public class OptionPaneDemo implements ActionListener
{
JFrame f = null;
JLabel label = null;
public OptionPaneDemo()
{
f = new JFrame("OptionPane Demo");
Container contentPane = f.getContentPane();
JButton b = new JButton("Show Text Input");
b.addActionListener(this);
label = new JLabel(" ",JLabel.CENTER);
contentPane.add(label,BorderLayout.NORTH);
contentPane.add(b,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args)
{
SwingUtil.setLookAndFeel();
new OptionPaneDemo();
}
public void actionPerformed(ActionEvent e)
{
String title = "Input Dialog";
JLabel message =new JLabel("您最喜欢吃什么食物?",JLabel.CENTER);
int messageType = JOptionPane.QUESTION_MESSAGE;
int optionType = JOptionPane.OK_CANCEL_OPTION;
String result ="";
//利用message,messageType,optionType来建立JOptionPane对象
JOptionPane optionPane = new JOptionPane(message,messageType,optionType);
//利用JOptionPane的setWandtsInput()方法,使对话框有一个输入字段让用户输入信息
optionPane.setWantsInput(true);
//利用JOptionPane的setInitialSelectionValue()方法,使得输入字段上的初始值为"请输入";
optionPane.setInitialSelectionValue("请输入!");
//利用JOptionPane的setInputValue()方法,使得当用户按下"Cancel"键或关闭对话框时,result的默认字符串为
//"你没有输入";
optionPane.setInputValue("您没有输入!");
JDialog dialog = optionPane.createDialog(f, title);
dialog.show();
//JOptionPane的getInputValue()方法可以取得用户输入的信息.
result = (String)optionPane.getInputValue();
label.setText("您输入:"+result);
}
}
class SwingUtil{
public static final void setLookAndFeel() {
try{
Font font = new Font("JFrame", Font.PLAIN, 12);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font) {
UIManager.put(key, font);
}
}
AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
AlloyTheme theme = new GlassTheme();
LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);
UIManager.setLookAndFeel(alloyLnF);
}catch(UnsupportedLookAndFeelException ex){
ex.printStackTrace();
}
}
}