java swing标准对话框具体实现
程序员文章站
2023-12-15 17:08:58
复制代码 代码如下:package test001;import java.awt.event.actionevent;import java.awt.event.acti...
复制代码 代码如下:
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 = {"继续", "撤销"};
int result = joptionpane.showoptiondialog(null,
"本操作可能导致数据丢失","warning", joptionpane.default_option,
joptionpane.warning_message, null, options, options[0]);
if(result == 0){
system.out.println("继续操作---");
}
}
else if(s.equals("输入")){
string name = joptionpane.showinputdialog("请输入您的姓名:");
if(name != null){
system.out.println("姓名:" + name);
}
}
else if(s.equals("选择")){
object[] possiblevalues = {"体育", "政治", "经济", "文化"};
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);
}
}
}
}