java基础学习之 消息对话款

时间:2022-05-05
本文章向大家介绍java基础学习之 消息对话款,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
 1 package Dome;
 2 import java.awt.event.*;
 3 import java.awt.*;
 4 import javax.swing.*;
 5 
 6 public class WindowMess extends JFrame implements ActionListener
 7 {
 8    JTextField inputEnglish ;
 9    JTextArea show ;
10    String regex = "[a-zZ-Z]+";
11    WindowMess()
12    {
13        inputEnglish = new JTextField(22);
14        inputEnglish.addActionListener(this);
15        show = new JTextArea();
16        add(inputEnglish,BorderLayout.NORTH);  //默认布局
17        add(show,BorderLayout.CENTER);
18        setVisible(true);
19        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
20    }
21    public void actionPerformed(ActionEvent e)
22    {
23       if(e.getSource()==inputEnglish)
24       {
25         String str=inputEnglish.getText();
26         if(str.matches(regex))
27             show.append(str+"");
28       else
29       {
30         //弹出“警告”消息对话框
31           JOptionPane.showMessageDialog(this, "8 干呀6","消息对话款",JOptionPane.WARNING_MESSAGE);
32           inputEnglish.setText(null);
33       } 
34      }
35    }
36    
37 }
 1 package Dome;
 2 
 3 public class example_1 {
 4     public static void main(String args[])
 5     {
 6         WindowMess win = new WindowMess();
 7         win.setTitle("带消息对话框的窗口");
 8         win.setBounds(80,90,200,300);
 9     }
10 }

颜色对话框

创建一个颜色对话框 

public static Color showDialog( Component  component ,String title ,Color initialColor)
/*
创建一个有模式的颜色对话框,其中参数component 指定颜色对话框可见时的位置,颜色对话框在参数,component 指定的组件的正前方显示出来,如果component为null,颜色对话框在屏幕的正前方显示出来。title指定对话框的标题,initialColor指定颜色对话框返回的初始值。用户通过颜色对话框选择颜色后,如果单击“确定”按钮,那么颜色对象,如果单击“撤销”按钮或者关闭图标,那么颜色对话框将消失,showDialog()方法返回null
*/
 1 package tes;
 2 import java.awt.*;
 3 import java.awt.event.*;
 4 import javax.swing.*;
 5 
 6 public class WindowColor extends JFrame implements ActionListener
 7 {
 8   JButton button ;
 9   WindowColor()
10   {
11     button = new JButton("打开颜色对话框");
12     button.addActionListener(this);
13     setLayout(new FlowLayout());
14     add(button);
15     setVisible(true);
16     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
17   }
18   public void actionPerformed(ActionEvent e)
19   {
20       Color newColor = JColorChooser.showDialog(this,"调色板",getContentPane().getBackground());
21       if(newColor!=null)  //将this所指的颜色传送给newColor.......
22           getContentPane().setBackground(newColor);   //重置背景颜色
23   }
24 }
 1 package tes;
 2 
 3 public class example {
 4   public static void main(String args[])
 5   {
 6     WindowColor win = new WindowColor();
 7     win.setTitle("带颜色对话框的窗口");
 8     win.setBounds(80, 90, 200,300);
 9   }
10 }

效果图: