java swing標(biāo)準(zhǔn)對(duì)話框具體實(shí)現(xiàn)
更新時(shí)間:2013年06月30日 15:32:52 作者:
這篇文章介紹了swing標(biāo)準(zhǔn)對(duì)話框的具體實(shí)現(xiàn)方法,有需要的朋友可以參考一下
復(fù)制代碼 代碼如下:
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("標(biāo)準(zhǔn)對(duì)話框測(cè)試");
/**
* @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 = {"錯(cuò)誤", "退出確認(rèn)1", "退出確認(rèn)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("錯(cuò)誤")){
JOptionPane.showMessageDialog(null, "要顯示的錯(cuò)誤信息---",
"錯(cuò)誤提示",JOptionPane.ERROR_MESSAGE);
}
else if(s.equals("退出確認(rèn)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("退出確認(rèn)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,
"本操作可能導(dǎo)致數(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("請(qǐng)輸入您的姓名:");
if(name != null){
System.out.println("姓名:" + name);
}
}
else if(s.equals("選擇")){
Object[] possibleValues = {"體育", "政治", "經(jīng)濟(jì)", "文化"};
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實(shí)現(xiàn)5種負(fù)載均衡算法(小結(jié))
負(fù)載均衡是將客戶端請(qǐng)求訪問,通過提前約定好的規(guī)則轉(zhuǎn)發(fā)給各個(gè)server,本文主要介紹了Java實(shí)現(xiàn)5種負(fù)載均衡算法,具有一定的參考價(jià)值,感興趣的可以了解一下2022-06-06Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(26)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07Java Swing JComboBox下拉列表框的示例代碼
這篇文章主要介紹了Java Swing JComboBox下拉列表框的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12基于Spring上下文工具類?ApplicationContextUtil
這篇文章主要介紹了基于Spring上下文工具類?ApplicationContextUtil,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java數(shù)據(jù)結(jié)構(gòu)之實(shí)現(xiàn)哈希表的分離鏈接法
今天給大家?guī)淼氖顷P(guān)于Java數(shù)據(jù)結(jié)構(gòu)的相關(guān)知識(shí),文章圍繞著Java哈希表的分離鏈接法展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06SpringBoot Test 多線程報(bào)錯(cuò)的根本原因(dataSource already
在使用Springboot test進(jìn)行相關(guān)測(cè)試的時(shí)候,發(fā)現(xiàn)開啟線程操作數(shù)據(jù)庫(kù)的時(shí)候異常,這篇文章主要介紹了SpringBoot Test 多線程報(bào)錯(cuò):dataSource already closed的根本原因及解決方法,需要的朋友可以參考下2022-06-06springboot2.0整合logback日志的詳細(xì)代碼
這篇文章主要介紹了springboot2.0整合logback日志的應(yīng)用場(chǎng)景分析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02