Java graphical interface --- JOptionPane

Table of contents

1. Introduction to JOptionPane

Second, the use of JOptionalPane

(1) Message dialog box

(2) Confirm dialog box

(3) Input dialog box

(4) Options dialog box


1. Introduction to JOptionPane

It is very convenient to create some simple dialog boxes through JOptionPane. Swing has added corresponding components for these dialog boxes, and programmers do not need to manually add components. JOptionPane provides the following four methods to create dialog boxes.

方法:

showMessageDialog            消息对话框,告知用户某事已发生,用户只能单击确定按钮
showConfirmDialog            确认对话框,向用户确认某个问题,用户可以选择yes,no,cancel等                              选项
showInputDialog              输入对话框,提示要求输入某些信息。
showOptionDialog             自定义选项对话框,允许使用自定义选项,可取代showConfirmDialog                              所产生的对话框

The above methods have many overloaded forms, choose one of the most complete forms as follows:
showxxxDialog(Component p
              Object message
              String title
              int optionType
              int messageType
              icon icon
              Object[] options
              Object ini)

 

Parameter explanation:
p: the parent component of the current dialog

message: The information displayed in the dialog box, which can be strings, components, pictures, etc.

title: the title of the current dialog

optionType: The button type displayed in the current dialog box DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION, OK_CANCEL_OPTION

messageType: The type of the current dialog box: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, PLAIN_MESSAGE

icon: the icon in the upper left corner of the current dialog

options: Options for customizing the drop-down list

ini: default options in custom options

Second, the use of JOptionalPane

(1) Message dialog box

Case: Create an interface as shown in the figure, and display the content in the text field in the message dialog box. 

public class exer1 {
    JFrame s=new JFrame("程序练习");
    JTextArea text=new JTextArea(10,40);
    JButton b=new JButton(new AbstractAction("消息对话框") {
        @Override
        public void actionPerformed(ActionEvent e) {
           //点击按钮后弹出一个消息对话框,并且显示文本域中的内容
            String ss=text.getText();//获取文本域内容
            //参数最后一个表示指定消息对话框的类型:错误消息,警告消息,问题消息......
            //错误消息
            //JOptionPane.showMessageDialog(s,text,"消息对话框",JOptionPane.ERROR_MESSAGE);
            //警告消息
            JOptionPane.showMessageDialog(s,text,"消息对话框",JOptionPane.WARNING_MESSAGE);
            //问题消息
            //JOptionPane.showMessageDialog(s,text,"消息对话框",JOptionPane.QUESTION_MESSAGE);
           // 消息对话框
            // JOptionPane.showMessageDialog(s,text,"消息对话框",JOptionPane.INFORMATION_MESSAGE);

            //指定插入的图片
           // JOptionPane.showMessageDialog(s,ss,"消息对话框",JOptionPane.ERROR_MESSAGE,new ImageIcon());
        }
    });
    public void init(){
        //组装组件
        s.add(text);
        s.add(b,BorderLayout.SOUTH);

        s.pack();
        s.setVisible(true);

        s.setDefaultCloseOperation(3);
    }
}

(2) Confirm dialog box

Case: Create an interface as shown in the figure, and the clicked option will be displayed in the text box.

public class exer1 {
    JFrame s=new JFrame("程序练习");
    JTextArea text=new JTextArea(10,40);
    JButton b=new JButton(new AbstractAction("确认对话框") {
        @Override
        public void actionPerformed(ActionEvent e) {
            String ss=text.getText();
            text.append("\n");
            int res=JOptionPane.showConfirmDialog(s,ss,"确认对话框",JOptionPane.YES_NO_OPTION);
            if(res==JOptionPane.YES_OPTION){
                text.append("点击的是 是\n");
            }
            if(res==JOptionPane.NO_OPTION){
                text.append("点击的是 否\n");
            }
        }
    });
    public void init(){
        s.add(text);
        s.add(b,BorderLayout.SOUTH);

        s.setDefaultCloseOperation(3);
        s.pack();
        s.setVisible(true);
    }
}

(3) Input dialog box

Case: Make the interface as shown in the figure

 

public class exer1 {
    JFrame s=new JFrame("程序练习");
    JTextArea text=new JTextArea(10,40);
    JButton b=new JButton(new AbstractAction("输入对话框") {
        @Override
        public void actionPerformed(ActionEvent e) {
            //弹出输入对话框
            //返回值是对话框中输入的内容
            String res=JOptionPane.showInputDialog(s,"请输入信息:","输入对话框",JOptionPane.INFORMATION_MESSAGE);
            text.append(res+"\n");
        }
    });
    public void init(){
        s.add(text);
        s.add(b,BorderLayout.SOUTH);

        s.setDefaultCloseOperation(3);
        s.pack();
        s.setVisible(true);
    }
}

(4) Options dialog box

Case: Create an interface as shown in the figure, select the corresponding option, and it will be displayed in the text field.

 

public class exer1 {
    JFrame s=new JFrame("程序练习");
    JTextArea text=new JTextArea(10,40);
    JButton b=new JButton(new AbstractAction("选项对话框") {
        @Override
        public void actionPerformed(ActionEvent e) {
            //选项对话框
            String[] ss={"大号","中号","小号"};
            //返回值是数组下标,最后一个参数是默认选项
            int res=JOptionPane.showOptionDialog(s,"你选择的型号是:","选项对话框",
                    JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE,null,ss,"中号");
            if(res==0){
                text.append("用户选择"+ss[0]);
            }
            if(res==1){
                text.append("用户选择"+ss[1]);
            }
            if(res==2){
                text.append("用户选择"+ss[2]);
            }
        }
    });
    public void init(){
        s.add(text);
        s.add(b,BorderLayout.SOUTH);

        s.setDefaultCloseOperation(3);
        s.pack();
        s.setVisible(true);
    }
}

Guess you like

Origin blog.csdn.net/gaoqiandr/article/details/128706689