java笔记 对话框(hasNext简述)

一.消息对话框

有模式的对话框,只能先处理当前对话框,不能激活其他窗口

对于这种对话框,我们可以利用JOptionPane类中的showMessageDialog(Component parentComponent,string message, string title, int messageType),来设置,这个函数返回类型void
parentComponent设定对话框的位置,如果为null,则在屏幕正前方。如果不空,会在组件parentComponent的正前方显示。
message表示对话消息
title表示对话窗口的标题
intJOptionPane中的常量,显示窗口的图标,比如感叹号什么的

INFORMATION_MESSAGE     // i
WARNING_MESSAGE          //  !
ERROR_MESSAGE           //   X
QUESTION_MESSAGE       //   ?
PLAIN_MESSAGE          // 无
//Inner class 事件处理例一
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class WindowMess extends JFrame implements ActionListener
{
    
    
       JTextField inputEnglish;
       JTextArea  show;
       String regex = "[a-z A-Z] + ";   // 如果是a-z,A-Z

       WindowMess(String s)
       {
    
    
           setTitle(s);
           setBounds(80,90,200,300);
           inputEnglish = new JTextField(22);   // 实例输入框
           inputEnglish.addActionListener(this);       // 注册监听器
           show = new JTextArea(20,50);                        // 实例输出框
           add(inputEnglish, BorderLayout.NORTH);         // 给输入框布局
           add(show, BorderLayout.CENTER);                // 给输出框布局

           setVisible(true);
           setDefaultCloseOperation(EXIT_ON_CLOSE);
       }

       public void actionPerformed(ActionEvent e)
       {
    
    
           if(e.getSource() == inputEnglish)
           {
    
    
               String str = inputEnglish.getText();
               if(str.matches(regex))  // 与之前的英文匹配上
               {
    
    
                   show.append(str+",");
               }
               else   // 输出警告
               {
    
    
                   JOptionPane.showMessageDialog(this, "输入非法字符", "消息对话框", JOptionPane.WARNING_MESSAGE);
                   inputEnglish.setText(null);
               }
           }
       }
}

//主类
public class test
{
    
    
    public static void main(String args[])
    {
    
    
        WindowMess win = new WindowMess("带消息的窗口");
    }
}

在这里插入图片描述
z

二.输入对话框

有模式对话窗口,不能激活其他窗口

使用JOptionPane类showInputDialog(Component parentComponent,string message, string title, int messageType)返回String类型返回内容是输入框的东西
parentComponent设定对话框的位置,如果为null,则在屏幕正前方。如果不空,会在组件parentComponent的正前方显示。
message表示对话消息
title表示对话窗口的标题
intJOptionPane中的常量,显示窗口的图标,比如感叹号什么的

INFORMATION_MESSAGE     // i
WARNING_MESSAGE          //  !
ERROR_MESSAGE           //   X
QUESTION_MESSAGE       //   ?
PLAIN_MESSAGE          // 无
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.InputMismatchException;
import java.util.Scanner;

//分析一字符串,分别输出大写字符数、小写字符数和非英文字符数
class WindowInput extends JFrame implements ActionListener
{
    
    
    JTextArea showResult;
    JButton openInput;

    WindowInput(String s)
    {
    
    
        setTitle(s);
        setBounds(80, 90, 200, 300);

        openInput = new JButton("弹出输入对话框");
        showResult = new JTextArea();
        add(openInput, BorderLayout.NORTH);
        add(showResult, BorderLayout.CENTER);
        openInput.addActionListener(this);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e)
    {
    
    
        // 弹出对话框,返回String类型
        String str = JOptionPane.showInputDialog(this, "输入数字", "输入对话框", JOptionPane.PLAIN_MESSAGE);
        System.out.println(str);

        if(str != null)
        {
    
    
            Scanner scanner = new Scanner(str);
            double sum = 0;
            int k = 0;
            while(scanner.hasNext())   // 检测是否还有输入
            {
    
    
                try
                {
    
    
                    double number = scanner.nextDouble();
                    if(k == 0)
                        showResult.append(""+number);  // 第一个数字
                    else
                        showResult.append("+" + number);

                    sum = sum + number;
                    k++;
                }
                catch (InputMismatchException exp)  // 输入异常
                {
    
    
                    String s = scanner.next();
                }
            }

            showResult.append("=" + sum + "\n");
        }
    }
}

public class test
{
    
    
    public static void main(String args[])
    {
    
    
        WindowInput win = new WindowInput("输入对话框");
    }
}

在这里插入图片描述
在这里插入图片描述


输入Scanner类的对象调用hasNext,来判断是否还有输入
利用语句while(scanner.hasNext())来控制输入


三.确认对话框

有模式

使用JOptionPane类showConfirmDialog(Component parentComponent,string message, string title, int messageType)返回int类型返回内容是下面常量之一

JOptionPane.YES_OPTION
JOptionPane.NO_OPTION
JOptionPane.CANCEL_OPTION
JOptionPane.OK_OPTION
JOptionPane.CLOSED_OPTION

parentComponent设定对话框的位置,如果为null,则在屏幕正前方。如果不空,会在组件parentComponent的正前方显示。
message表示对话消息
title表示对话窗口的标题
intJOptionPane中的常量,显示窗口的按钮

JOptionPane.YES_NO_OPTION
JOptionPane.YES_NO_CANCEL_OPTION
JOptionPane.OK_CANCEL_OPTION
//CardLayout之例
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class WindowEnter extends JFrame implements ActionListener
{
    
    
    JTextField inputName;
    JTextArea save;
    WindowEnter()
    {
    
    
        setTitle("带确认框的窗口");
        setBounds(80,90,200,300);

        inputName = new JTextField(22);
        save = new JTextArea();

        inputName.addActionListener(this);
        add(inputName, BorderLayout.NORTH);
        add(save, BorderLayout.CENTER);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e)
    {
    
    
        String s = inputName.getText();

        // 返回 int 类型
        int n = JOptionPane.showConfirmDialog(this, "确认是否正确", "确认对话框", JOptionPane.YES_NO_OPTION);

        if(n == JOptionPane.YES_OPTION)
        {
    
    
            save.append("\n" + s);
        }
        else if (n == JOptionPane.NO_OPTION)
        {
    
    
            inputName.setText(null);
        }
    }
}

public class test{
    
    
    public static void main(String args[])
    {
    
    
        WindowEnter win = new WindowEnter();
    }
}

在这里插入图片描述
在这里插入图片描述

四.自定义对话框

这个对话框是继承JDialog的窗口,他的构造函数两个参数(JFrame, s)
JFrame 是父窗口,s是标题

//CardLayout之例
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MyWindow extends JFrame implements ActionListener  // 窗口的监听
{
    
    
    JButton button;
    MyDialog dialog;


    MyWindow()
    {
    
    
        setTitle("带自定义对话框的窗口");
        setSize(200,300);

        button = new JButton("打开对话框");
        button.addActionListener(this);
        add(button, BorderLayout.NORTH);
        dialog = new MyDialog(this, "我是对话框"); // 对话框依赖于MyWindow的创建,MyWindow是他的父窗口
        dialog.setModal(true);  // 有模式

        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e)
    {
    
    
        dialog.setVisible(true);
        String str = dialog.getTitle();
        setTitle(str);
    }
}

class MyDialog extends JDialog implements ActionListener  // 继承JDialog才能实现
{
    
    
    JTextField inputTitle;
    String title;

    MyDialog(JFrame f, String s)
    {
    
    
        super(f, s);
        inputTitle = new JTextField(10);
        inputTitle.addActionListener(this);
        setLayout(new FlowLayout());
        add(new JLabel("输入窗口标题"));
        add(inputTitle);
        setBounds(60, 60,100,100);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e)
    {
    
    
        title = inputTitle.getText();
        setVisible(false);
    }

    public String getTitle()
    {
    
    
        return title;
    }
}

public class test{
    
    
    public static void main(String args[])
    {
    
    
        MyWindow win = new MyWindow();
    }
}

在这里插入图片描述
在这里插入图片描述
对话框的关闭必须是DISPOSE_ON_CLOSE;

猜你喜欢

转载自blog.csdn.net/yogur_father/article/details/109017940