java基础学习之 消息对话款

本文展示了如何在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 }
WindowMess.java
 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 }
Main

颜色对话框

创建一个颜色对话框 

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 }
example
 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 }
Main

 

效果图:

转载于:https://www.cnblogs.com/gongxijun/p/3853612.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值