600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > java实用教程——组件及事件处理——对话框(颜色对话框 自定义对话框)

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

时间:2021-02-11 10:08:31

相关推荐

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

颜色对话框:

可以用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);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。