颜色对话框:
可以用javax.swing包中的JColorChooser类的静态方法
public staticColorshowDialog (Component
component, String title,
Color initialColor)

创建一个有模式的颜色对话框,其中参数component指定颜色对话框可见时的位置,颜色对话框在参数component指定的组件的正前方显示出来,如果
component为null,颜色对话框在屏幕的正前方显示出来。

title指定对话框的标题,initialColor指定颜色对话框返回的初始台调色板颜色。
用户通过颜色对话框选择颜色后,如果单击“确定”按钮,那么颜色对话框将消失,showDialog()方法返回对话框所选择的颜色对象。如果单击“ 撤销”按钮或关闭图标,那预览样品文本样品文本颜色对话框将消失,showDialog0方 法返回null.
在下面的例子19中,当用户单击按钮时,弹出一个颜色对话框,然后根据用户选择的颜色来改变窗口的颜色。程序中颜色对话框的运行效果如图9.19所示。

public class Example9_19 {public static void main(String args[]) {WindowColor win=new WindowColor();win.setTitle("带颜色对话框的窗口"); win.setBounds(80,90,500,500);}}
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class WindowColor extends JFrame implements ActionListener {JButton button;WindowColor() {button=new JButton("打开颜色对话框"); button.addActionListener(this);setLayout(new FlowLayout());add(button);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public void actionPerformed(ActionEvent e) {Color newColor=JColorChooser.showDialog(this,"调色板",getContentPane().getBackground());if(newColor!=null) {getContentPane().setBackground(newColor); }    }
}


自定义对话框:

创建对话框与创建窗口类似,通过建立JDialog的子类来建立一个对话框类,然后这个类的一个实例,即这个子类创建的一一个对象,就是-个对话框。
对话框是一个容器,它的默认布局是BorderLayout,对话框可以添加组件,

实现与用户的交互操作。需要注意的是,对话框可见时,默认地被系统添加到显示器屏幕上,因此不允许将一个对话框添加到另-一个容器中。以下是构造对话框的两个常用构造方法。
●JDialog0构造-.个无标题的初始不可见的对话框,对话框依赖一个默认的不可见的窗口,该窗口由Java运行环境提供。

●JDialog(JFrame owner) 构造一 个无标题的初始不可见的无模式的对话框,owner是对话框所依赖的窗口,如果owner取null,对话框依赖一个默认的不可见的输入窗口的新标题窗口,该窗口由Java运行环境提供。

下面的使用自定义对话框更改窗口的标题

public class Example9_20 {public static void main(String args[]) {MyWindow win=new MyWindow();win.setTitle("带自定义对话框的窗口"); win.setSize(620,360);}}
import java.awt.*;
import javax.swing.*;
import java.util.Arrays;
public class MyWindow extends JFrame  {JButton button; JTextArea  showRedBall ;//存放红球号码int []  redNumber;MyWindow() {init();setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}void init() {button=new JButton("得到双色球彩票的一组红球"); showRedBall = new JTextArea(10,20);showRedBall.setForeground(Color.red);this.setLayout(new FlowLayout());this.add(button);this.add(showRedBall);showRedBall.setLineWrap(true);//add(button,BorderLayout.NORTH); //add(new JScrollPane(showRedBall),BorderLayout.CENTER); button.addActionListener((e)->{redNumber = MyDialog.showRandomArrayDiolog(this,"红球号码","双色球对话框",MyDialog.YES_NO_OPTION,33,6);if(redNumber!=null) {Arrays.sort(redNumber);showRedBall.append(Arrays.toString(redNumber)+"\n"); } });Font f = new Font("",Font.BOLD,28);SetFont.setFont(f,button,showRedBall);}
}
import java.awt.*;
import javax.swing.*;
import java.util.Random;
import java.util.Arrays;
public class MyDialog  { public static final int YES_NO_OPTION = 1;static int backNumber[] = null; //该返回的数组public static int[] showRandomArrayDiolog(Component parentComponent,String message,String title,int optionType,int max,int amount){ JDialog dialog = new JDialog((JFrame)parentComponent);dialog.setModal(true);dialog.setTitle(title);JLabel mess = new JLabel(message);JTextField showArray = new JTextField(20);//显示得到的一组随机数int [] arraysNumber = getRandomNumber(max,amount);showArray.setText(Arrays.toString(arraysNumber));dialog.setLayout(new FlowLayout());JButton yesButton = new JButton();JButton noButton = new JButton();if(optionType==YES_NO_OPTION) {yesButton.setText("是(Yes)");noButton.setText("否(No)");}else {JOptionPane.showMessageDialog(parentComponent,"参数取值不正确","消息",JOptionPane.ERROR_MESSAGE);return backNumber;}dialog.add(mess);dialog.add(showArray);dialog.add(yesButton);dialog.add(noButton);yesButton.addActionListener((e)->{backNumber = arraysNumber;dialog.setVisible(false);});noButton.addActionListener((e)->{dialog.setVisible(false);});Font f = new Font("",Font.BOLD,28);SetFont.setFont(f,mess,showArray,yesButton,noButton);dialog.setBounds(500,60,600,300);dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);dialog.setVisible(true);return backNumber;}private static int [] getRandomNumber(int max,int amount) {// 1至max之间的amount个不同随机整数(包括1和max)int [] randomNumber = new int[amount];Random random = new Random();int count = 0;while(count<amount){int number = random.nextInt(max)+1;boolean isInArrays=false;for(int m:randomNumber){//m依次取数组randomNumber元素的值if(m == number){isInArrays=true;  //number在数组里了break;}}if(isInArrays==false){//如果number不在数组randomNumber中:randomNumber[count] = number;count++;   }}return  randomNumber;}
}
import javax.swing.JComponent;
import java.awt.Font;
public class SetFont {public static void setFont(Font f,JComponent ...component){for(JComponent c:component)c.setFont(f);}
}

java实用教程——组件及事件处理——对话框(颜色对话框,自定义对话框)相关推荐

  1. java实用教程——组件及事件处理——对话框(消息对话框,输入对话框,确认对话框)

    消息对话框:(这个对话框提供一些信息) 无模式:可多线程的执行 有模式:用户必须处理这个一个对话框.必须解决这个问题后才可以继续相处下去 对话框分为无模式和有模式两种.如果一个对话框 是有模式的对话框 ...

  2. java实用教程——组件及事件处理——ActionEvent事件

    事件源: 文本框,按钮,菜单项,密码框,单选按钮 注册监视器: 能够触发ActionEvent事件的组件使用方法 addActionListener(ActionListener listener) ...

  3. java实用教程——组件及事件处理——对话框(dialog)

    对话框: import java.awt.event.ActionEvent;import java.awt.event.ActionListener; import java.awt.*; impo ...

  4. java实用教程——组件及事件处理——设置组件的位置(相对于窗口具体位置和布局)

    1: 相对于窗口的具体位置 关键点: JButton组件添加到JPanel时,如果想自己位置,需要对JPanel进行如下设置,才能自定义按钮位置 需要将组件添加到画板上去,才可以设置组件的相对具体位置 ...

  5. java实用教程——组件及事件处理——布局的一个小实例

    import javax.swing.*; import java.awt.*;public class BasicComponentDemo {Frame frame = new Frame(&qu ...

  6. java实用教程——组件及事件处理——布局管理(五种)

    1.流式布局FlowLayout public void pack()调整此窗口的大小,以适合其子组件的首选大小和布局.如果该窗口或其所有者仍不可显示,则两者在计算首选大小之前变得可显示.在计算首选大 ...

  7. java实用教程——组件及事件处理——ItemEvent事件(设置字体类型)

    ItemEvent事件源: 选择框,下拉列表都可以触发ItemEvent事件 注册监视器: 能够触发ItemEvent事件的组件使用addItemListener(ItemListener liste ...

  8. java实用教程——组件及事件处理——MouseEvent事件

    MouseEvent事件 任何组件上都可以发生鼠标事件,如鼠标进入组件.退出组件.在组件上方单击鼠标.拖动鼠标等都触发鼠标事件,即导致MouseEvent类自动创建一个 事件对象,事件源注册监视器的方 ...

  9. java实用教程——组件及事件处理——DocumentEvent事件

    DocumentEvent事件源: 文本区Document的维护 注册监视器: 使用addDocumentListener(DocumentListener listen)为事件源添加监视器 Docu ...

最新文章

  1. 最简单的matplotlib写法
  2. bash脚本编程之一 变量、变量类型等
  3. skywalking服务端_skywalking部署
  4. 财会专用计算机,一种财会专用计算机的制作方法
  5. mpvue 微信小程序api_基于mpvue构建微信和支付宝小程序(1)
  6. Filecoin Gas基础费率降至4.67 nanoFIL
  7. 清华大学操作系统OS学习(十四)——文件系统和文件描述符
  8. 在Windows中玩转Docker Toolbox
  9. mysql 校对规则名_MySQL字符集及校对规则的理解
  10. 三菱PLC编程口通信上位机端报文和java示范代码
  11. 【超图+CESIUM】【基础API使用示例】34、超图|CESIUM - 绘制光流流光线段
  12. element Table+Pagination实现分页
  13. perl Data::Dumper和Storable的例子
  14. OSS定制自定义response header
  15. 三菱plc传送文件到服务器,三菱Q系列PLC通过FTP文件传输案例介绍
  16. 【Pytorch】量化
  17. php期末大作业可以做什么,期末要交一个基于php连入数据的大作业
  18. Python中的几种乘法np.dot,np.multiply,*
  19. 国家强制性产品认证实施规则汇总(更新日期:2020年12月)
  20. Word编辑公式 下划线取消自动套用格式 下划线不转义

热门文章

  1. .NET Core第三方开源Web框架YOYOFx
  2. 计算机第一课 教案 纪律,信息技术开学第一课-纪律
  3. mysql505复位密码_mysql5 如何复位根用户密码[官方文档]
  4. Navicat Premium 64 bit 12.1.25
  5. 【遥感数字图像处理】实验:遥感图像显示与数据输入/输出(Erdas版)
  6. SQL Server 2005 cmd工具的使用
  7. C语言试题八十三之输出左下三角形九九乘法表
  8. busybox怎么安装
  9. 1小时零基础赚一千,教你完成图书管理系统,不用打代码绝对学得会![完整全站教学 IVX 实战第四篇]
  10. python删除列表中的偶数_Python:从列表中删除奇数