GUI对话框

消息对话框

public static void showMessageDialog(Component parentComponent,String message,String title,int messageType)

parentComponent为null,就会在正前面显示,为this就会在该组件中间显示。

message为对话信息

title为对话框题目

messageType为一个顺带的图标,为JOptionPane的常量

INFORMATON_MESSAGE小感叹号

WARNING_MESSAGE警告

ERROR_MESSAGE交叉

QUESTION_MESSAGE问号

PLAIN_MESSAGE  没有

测试代码(输入不是字母会弹出警告信息

        String regex="\p{Alpha}+";
        if(text1.getText().matches(regex)){
            textArea1.append(text1.getText()+"
");
            text1.setText(null);
        }else{
            JOptionPane.showMessageDialog(this, "error","huang",JOptionPane.PLAIN_MESSAGE);
        }

输入对话框

public static String showInputDialog(Component parentComponent,objext message,String title,int messageType)

除了返回类型

和上面一样,不重复了

一小段测试代码(输入框里面输入一串数字算总数

        double sum=0;
        String a=JOptionPane.showInputDialog(this,"input number split by space","",JOptionPane.PLAIN_MESSAGE);
        if(a!=null){
            Scanner scanner1=new Scanner(a);
            for(;scanner1.hasNext();){
                try{
                    double number=scanner1.nextDouble();
                    textArea1.append(number+"+");
                    sum+=number;
                }
                catch(Exception e2){
                    String t=scanner1.next();//不理他跳下一个
                }
            }
        }
        textArea1.append("="+ sum+"
");

选择对话框

public static int showConfirmDialog(Component parentComponent,Objext message,String title,int optionType)

这次返回的是int ,后面的optionType也有点不一样

记住个JOptionPane.YES_OPTION


自定义对话框

其实对话框一直有分有模式和没有模式的,之前一直忘记了

模式对话框就是不处理它就没法处理父窗口,而非模式对话框就是不用先处理此对话框也可以处理父窗口.

自定义的对话框要继承JDialog

还要像窗口那样

setBounds,setVisible,setDefaultCloseOperation

测试代码(用对话框改窗口的名字

class MyWin extends JFrame implements ActionListener{
    JTextField text1;
    JButton button1,button2;
    JTextArea textArea1;
    JLabel label1;
    MyDialog dialog1;
    MyWin(){
        init();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    void init(){
        setLayout(new FlowLayout());
        button1=new JButton("go");
        add(button1);
        button1.addActionListener(this);
        dialog1=new MyDialog(this,"is me");
        dialog1.setModal(true);//改为由模式的对话框 
        
    }
    public void actionPerformed(ActionEvent e){
        dialog1.setVisible(true);
        String s=dialog1.s;
        setTitle(s);
    }
}

class MyDialog extends JDialog implements ActionListener{
    JTextField text1;
    String s;
    MyDialog(JFrame f,String s){
        super(f,s);
        setLayout(new FlowLayout());
        text1=new JTextField(8);
        text1.addActionListener(this);
        add(new JLabel("input window title"));
        add(text1);
        setBounds(60,60,200,180);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
    }
    public void actionPerformed(ActionEvent e){
        s=text1.getText();
        setVisible(false);
    }
}
原文地址:https://www.cnblogs.com/vhyc/p/5997245.html