javaSwing实现对话框(消息,确定,输入,颜色,自定义对话框)

消息对话框

使用JOptionPane中的方法

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

参数parentComponent为null,对话框会在屏幕的正前方显示,如果组件parentComponent不为空,对话框会在组件parentComponent正前方显示;message是对话框上显示的内容。title为对话框的标题。messageType是对话框的类型,是长量,其中对话框有以下类型:
INFORMATION_MESSAGE
WARNING_MESSAGE
ERROR_MESSAGE
QUESTION_MESSAGE
PLAIN_MESSAGE
下面是实例。输入字符,如果是英文字母,则在右边文本框显示,如果不是,则弹出消息对话框提示警告

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class showMessageDialog extends JFrame {
    JTextField inputText;
    JTextArea showText;
    String regex="[a-zA-Z]+";

    showMessageDialog(){
        setLayout(new FlowLayout());
        inputText=new JTextField(10);
        showText=new JTextArea(9,20);
        inputText.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String str=inputText.getText();
                if(str.matches(regex)){
                    showText.append(str+",");
                }else {
                    JOptionPane.showMessageDialog(showMessageDialog.this,"输入非法字符","消息对话框",JOptionPane.WARNING_MESSAGE);
                }
            }
        });

        add(inputText,BorderLayout.EAST);
        add(showText,BorderLayout.CENTER);
        setVisible(true);
        setBounds(100,100,400,200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        showMessageDialog showMessageDialog=new showMessageDialog();
    }
}

效果为:
javaSwing实现对话框(消息,确定,输入,颜色,自定义对话框)_第1张图片

输入对话框

确认按钮输入框会返回用户在对话框的文本框中输入的字符串,否则返回null

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

public class WindowInput extends JFrame implements ActionListener {
    JTextArea jTextArea;
    JButton jButton;
    WindowInput(){
        setLayout(new FlowLayout());
        jTextArea=new JTextArea(9,20);
        jButton=new JButton("弹出输入对话框");
        setVisible(true);
        add(jButton,BorderLayout.NORTH);
        add(new JScrollPane(jTextArea),BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jButton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String str=JOptionPane.showInputDialog(WindowInput.this,"输入数字,用空格分割","输入对话框",JOptionPane.PLAIN_MESSAGE);
        if(str!=null){
            Scanner scanner=new Scanner(str);
            double sum=0;
            int k=0;
            while(scanner.hasNext()){
                double number=scanner.nextDouble();
                if(k==0){
                    jTextArea.append(""+number);
                }else{
                    jTextArea.append("+"+number);
                }
                sum+=number;
                k++;
            }
            jTextArea.append("="+sum+"\n");
        }

    }

    public static void main(String[] args) {
        WindowInput input=new WindowInput();
        input.setBounds(100,100,400,400);
    }
}

效果图:
javaSwing实现对话框(消息,确定,输入,颜色,自定义对话框)_第2张图片

确认对话框

optionType有以下几种类型:
YES_NO_OPTION:对话框上有yes和no显示
YES_NO_CANCEL_OPTION
OK_CANCEL_OPTION

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class confirmDialog extends JFrame {
    JTextField inputText;
    JTextArea save;

    confirmDialog(){
        setLayout(new FlowLayout());
        inputText=new JTextField(10);
        save=new JTextArea(9,20);
        setVisible(true);
        setBounds(100,100,400,400);
        inputText.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String s=inputText.getText();
                int r=JOptionPane.showConfirmDialog(confirmDialog.this,"确认是否正确","confirmDialog",JOptionPane.YES_NO_CANCEL_OPTION);
                if(r==JOptionPane.YES_OPTION){
                    save.append("\n"+s);
                }else{
                    save.append(null);
                }
            }
        });

        add(inputText,BorderLayout.NORTH);
        add(save,BorderLayout.CENTER);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        confirmDialog confirmDialog=new confirmDialog();
    }
}

效果图:
javaSwing实现对话框(消息,确定,输入,颜色,自定义对话框)_第3张图片

颜色对话框

JColorChooser中的静态方法:

public static Color showDialog(Component component,String title,Color initialColor)

initialColor指定颜色对话框返回初始颜色。点击确定,showDialog()方法返回对话框所选择的颜色;点击撤销或关闭,颜色对话框消失,返回null

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class colorDialog extends JFrame {
    JButton jButton;

    colorDialog(){
        setLayout(new FlowLayout());
        jButton=new JButton("选择颜色");
        add(jButton,BorderLayout.CENTER);
        setVisible(true);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Color color=JColorChooser.showDialog(colorDialog.this,"调色板",getContentPane().getBackground());
                if(color!=null){
                    getContentPane().setBackground(color);
                }
            }
        });
        setBounds(100,100,400,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        colorDialog colorDialog=new colorDialog();
    }
}

效果图:
javaSwing实现对话框(消息,确定,输入,颜色,自定义对话框)_第4张图片
javaSwing实现对话框(消息,确定,输入,颜色,自定义对话框)_第5张图片
javaSwing实现对话框(消息,确定,输入,颜色,自定义对话框)_第6张图片

自定义对话框

通过建立JDialog的子类来建立一个对话框,这个类的子类即是一个对话框,对话框是一个容器,默认布局是BorderLayout,对话框可以添加组件。对话框可见时,默认被系统添加到显示屏幕上,因此不允许把一个对话框添加到另一个容器里。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class WindowDialog extends JDialog {
    JTextField inputText;
    String Title;

    @Override
    public String getTitle() {
        return Title;
    }

    public WindowDialog(Frame owner, String title) {
        super(owner, title);
        setLayout(new FlowLayout());
        inputText=new JTextField(10);
        inputText.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Title=inputText.getText();
                setVisible(false);
            }
        });
        add(new JLabel("输入窗口的新标题"));
        setVisible(true);
        add(inputText);
        setBounds(100,100,200,200);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    }
//注意对话框关闭方式,不能是EXIT,否则报错:
Exception in thread "main" java.lang.IllegalArgumentException: defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, or DISPOSE_ON_CLOSE
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Window extends JFrame {
    JButton jButton;
    WindowDialog windowDialog;
    Window(){
        setLayout(new FlowLayout());
        jButton=new JButton("对话框");
        windowDialog=new WindowDialog(this,"dialog");
        windowDialog.setModal(true);
        add(jButton,BorderLayout.CENTER);
        setVisible(true);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                windowDialog.setVisible(true);
                String str=windowDialog.getTitle();
                setTitle(str);
            }
        });
    }

    public static void main(String[] args) {
        Window window=new Window();
        window.setBounds(100,100,200,200);
    }
}

你可能感兴趣的:(java基础)