栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java swing标准对话框具体实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java swing标准对话框具体实现

复制代码 代码如下:
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("标准对话框测试");
   
    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);
            }
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/153095.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号