java swing標準對話框具體實現(xiàn)
更新時間:2013年06月30日 15:32:52 作者:
這篇文章介紹了swing標準對話框的具體實現(xiàn)方法,有需要的朋友可以參考一下
復制代碼 代碼如下:
package test001;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
public class TestJOptionPane implements ActionListener{
private JFrame jf = new JFrame("標準對話框測試");
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new TestJOptionPane().createUI();
}
public void createUI(){
JToolBar jtb = new JToolBar();
String[] s = {"錯誤", "退出確認1", "退出確認2", "警告", "輸入", "選擇"};
int size = s.length;
JButton[] button = new JButton[size];
for(int i = 0; i < size; i++){
button[i] = new JButton(s[i]);
button[i].addActionListener(this);
jtb.add(button[i]);
}
jf.add(jtb, "North");
jf.setSize(350, 150);
jf.setLocation(400, 200);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String s = e.getActionCommand();
if(s.equals("錯誤")){
JOptionPane.showMessageDialog(null, "要顯示的錯誤信息---",
"錯誤提示",JOptionPane.ERROR_MESSAGE);
}
else if(s.equals("退出確認1")){
int result = JOptionPane.showConfirmDialog(null,
"推出前是否保存程序?");
if(result == JOptionPane.YES_OPTION){
System.out.println("保存程序---");
System.exit(0);
}
else if(result == JOptionPane.NO_OPTION){
System.exit(0);
}
}
else if(s.equals("退出確認2")){
int result = JOptionPane.showConfirmDialog(null, "退出前是否保存程序?");
if(result == JOptionPane.YES_OPTION){
System.out.println("保存程序---");
System.exit(0);
}
else if(result == JOptionPane.NO_OPTION){
System.exit(0);
}
}
else if(s.equals("警告")){
Object[] options = {"繼續(xù)", "撤銷"};
int result = JOptionPane.showOptionDialog(null,
"本操作可能導致數(shù)據(jù)丟失","Warning", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null, options, options[0]);
if(result == 0){
System.out.println("繼續(xù)操作---");
}
}
else if(s.equals("輸入")){
String name = JOptionPane.showInputDialog("請輸入您的姓名:");
if(name != null){
System.out.println("姓名:" + name);
}
}
else if(s.equals("選擇")){
Object[] possibleValues = {"體育", "政治", "經(jīng)濟", "文化"};
Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one","Input", JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);
String choose = (String)selectedValue;
if(choose != null){
System.out.println("你選擇的是:"+ choose);
}
}
}
}
相關(guān)文章
Java Swing JComboBox下拉列表框的示例代碼
這篇文章主要介紹了Java Swing JComboBox下拉列表框的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12基于Spring上下文工具類?ApplicationContextUtil
這篇文章主要介紹了基于Spring上下文工具類?ApplicationContextUtil,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Java數(shù)據(jù)結(jié)構(gòu)之實現(xiàn)哈希表的分離鏈接法
今天給大家?guī)淼氖顷P(guān)于Java數(shù)據(jù)結(jié)構(gòu)的相關(guān)知識,文章圍繞著Java哈希表的分離鏈接法展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06SpringBoot Test 多線程報錯的根本原因(dataSource already
在使用Springboot test進行相關(guān)測試的時候,發(fā)現(xiàn)開啟線程操作數(shù)據(jù)庫的時候異常,這篇文章主要介紹了SpringBoot Test 多線程報錯:dataSource already closed的根本原因及解決方法,需要的朋友可以參考下2022-06-06