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();
}
}
}