?? testjoptionpane.java
字號:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* Description:
* <br/>Copyright (C), 2005-2008, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class TestJOptionPane
{
JFrame jf = new JFrame("測試JOptionPane");
//分別定義6個面板用于定義對話框的幾種選項
private ButtonPanel messagePanel;
private ButtonPanel messageTypePanel;
private ButtonPanel msgPanel;
private ButtonPanel confirmPanel;
private ButtonPanel optionsPanel;
private ButtonPanel inputPanel;
private String messageString = "消息區內容";
private Icon messageIcon = new ImageIcon("ico/heart.png");
private Object messageObject = new Date();
private Component messageComponent = new JButton("組件消息");
private JButton msgBn = new JButton("消息對話框");
private JButton confrimBn = new JButton("確認對話框");
private JButton inputBn = new JButton("輸入對話框");
private JButton optionBn = new JButton("選項對話框");
public void init()
{
JPanel top = new JPanel();
top.setBorder(new TitledBorder(new EtchedBorder(), "對話框的通用選項" ,
TitledBorder.CENTER ,TitledBorder.TOP ));
top.setLayout(new GridLayout(1 , 2));
//消息類型Panel,該Panel中的選項決定對話框的圖標
messageTypePanel = new ButtonPanel("選擇消息的類型",
new String[]{"ERROR_MESSAGE", "INFORMATION_MESSAGE", "WARNING_MESSAGE",
"QUESTION_MESSAGE", "PLAIN_MESSAGE" });
//消息內容類型的Panel,該Panel中的選項決定對話框的消息區的內容
messagePanel = new ButtonPanel("選擇消息內容的類型",
new String[]{"字符串消息", "圖標消息", "組件消息", "普通對象消息" , "Object[]消息"});
top.add(messageTypePanel);
top.add(messagePanel);
JPanel bottom = new JPanel();
bottom.setBorder(new TitledBorder(new EtchedBorder(), "彈出不同的對話框" ,
TitledBorder.CENTER ,TitledBorder.TOP));
bottom.setLayout(new GridLayout(1 , 4));
//創建用于彈出消息對話框的Panel
msgPanel = new ButtonPanel("消息對話框", null);
msgBn.addActionListener(new ShowAction());
msgPanel.add(msgBn);
//創建用于彈出確認對話框的Panel
confirmPanel = new ButtonPanel("確認對話框",
new String[]{"DEFAULT_OPTION", "YES_NO_OPTION", "YES_NO_CANCEL_OPTION",
"OK_CANCEL_OPTION"});
confrimBn.addActionListener(new ShowAction());
confirmPanel.add(confrimBn);
//創建用于彈出輸入對話框的Panel
inputPanel = new ButtonPanel("輸入對話框",
new String[]{"單行文本框","下拉列表選擇框"});
inputBn.addActionListener(new ShowAction());
inputPanel.add(inputBn);
//創建用于彈出選項對話框的Panel
optionsPanel = new ButtonPanel("選項對話框",
new String[]{"字符串選項", "圖標選項", "對象選項"});
optionBn.addActionListener(new ShowAction());
optionsPanel.add(optionBn);
bottom.add(msgPanel);
bottom.add(confirmPanel);
bottom.add(inputPanel);
bottom.add(optionsPanel);
Box box = new Box(BoxLayout.Y_AXIS);
box.add(top);
box.add(bottom);
jf.add(box);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
//根據用戶選擇返回選項類型
private int getOptionType()
{
if (confirmPanel.getSelection().equals("DEFAULT_OPTION"))
return JOptionPane.DEFAULT_OPTION;
else if (confirmPanel.getSelection().equals("YES_NO_OPTION"))
return JOptionPane.YES_NO_OPTION;
else if (confirmPanel.getSelection().equals("YES_NO_CANCEL_OPTION"))
return JOptionPane.YES_NO_CANCEL_OPTION;
else
return JOptionPane.OK_CANCEL_OPTION;
}
//根據用戶選擇返回消息
private Object getMessage()
{
if (messagePanel.getSelection().equals("字符串消息"))
return messageString;
else if (messagePanel.getSelection().equals("圖標消息"))
return messageIcon;
else if (messagePanel.getSelection().equals("組件消息"))
return messageComponent;
else if(messagePanel.getSelection().equals("普通對象消息"))
return messageObject;
else
return new Object[]{messageString , messageIcon ,
messageObject , messageComponent};
}
//根據用戶選擇返回消息類型(決定圖標區的圖標)
private int getDialogType()
{
if (messageTypePanel.getSelection().equals("ERROR_MESSAGE"))
return JOptionPane.ERROR_MESSAGE;
else if (messageTypePanel.getSelection().equals("INFORMATION_MESSAGE"))
return JOptionPane.INFORMATION_MESSAGE;
else if (messageTypePanel.getSelection().equals("WARNING_MESSAGE"))
return JOptionPane.WARNING_MESSAGE;
else if(messageTypePanel.getSelection().equals("QUESTION_MESSAGE"))
return JOptionPane.QUESTION_MESSAGE;
else
return JOptionPane.PLAIN_MESSAGE;
}
private Object[] getOptions()
{
if (optionsPanel.getSelection().equals("字符串選項"))
return new String[]{"a" , "b" , "c" , "d"};
else if (optionsPanel.getSelection().equals("圖標選項"))
return new Icon[]{new ImageIcon("ico/1.gif") , new ImageIcon("ico/2.gif"),
new ImageIcon("ico/3.gif"),new ImageIcon("ico/4.gif")};
else
return new Object[]{new Date() ,new Date() , new Date()};
}
//為各按鈕定義事件監聽器
private class ShowAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (event.getActionCommand().equals("確認對話框"))
{
JOptionPane.showConfirmDialog(jf , getMessage(),"確認對話框",
getOptionType(), getDialogType());
}
else if (event.getActionCommand().equals("輸入對話框"))
{
if (inputPanel.getSelection().equals("單行文本框"))
{
JOptionPane.showInputDialog(jf, getMessage(), "輸入對話框", getDialogType());
}
else
{
JOptionPane.showInputDialog(jf, getMessage(), "輸入對話框", getDialogType(),
null, new String[] {"輕量級J2EE企業應用實戰", "Struts2權威指南"},
"Struts2權威指南");
}
}
else if (event.getActionCommand().equals("消息對話框"))
{
JOptionPane.showMessageDialog(jf,getMessage(),"消息對話框",getDialogType());
}
else if (event.getActionCommand().equals("選項對話框"))
{
JOptionPane.showOptionDialog(jf , getMessage() , "選項對話框", getOptionType(),
getDialogType(), null, getOptions(), "a");
}
}
}
public static void main(String[] args)
{
new TestJOptionPane().init();
}
}
//定義一個JPanel類擴展類,該類的對象包含多個縱向排列的JRadioButton控件
//且Panel擴展類可以指定一個字符串作為TitledBorder
class ButtonPanel extends JPanel
{
private ButtonGroup group;
public ButtonPanel(String title, String[] options)
{
setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), title));
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
group = new ButtonGroup();
for (int i = 0; options!= null && i < options.length; i++)
{
JRadioButton b = new JRadioButton(options[i]);
b.setActionCommand(options[i]);
add(b);
group.add(b);
b.setSelected(i == 0);
}
}
//定義一個方法,用于返回用戶選擇的選項
public String getSelection()
{
return group.getSelection().getActionCommand();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -