日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/4 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java实用教程——组件及事件处理——对话框(颜色对话框,自定义对话框) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

顏色對(duì)話框:
可以用javax.swing包中的JColorChooser類的靜態(tài)方法
public staticColorshowDialog (Component
component, String title,
Color initialColor)

創(chuàng)建一個(gè)有模式的顏色對(duì)話框,其中參數(shù)component指定顏色對(duì)話框可見時(shí)的位置,顏色對(duì)話框在參數(shù)component指定的組件的正前方顯示出來(lái),如果
component為null,顏色對(duì)話框在屏幕的正前方顯示出來(lái)。

title指定對(duì)話框的標(biāo)題,initialColor指定顏色對(duì)話框返回的初始臺(tái)調(diào)色板顏色。
用戶通過顏色對(duì)話框選擇顏色后,如果單擊“確定”按鈕,那么顏色對(duì)話框?qū)⑾?#xff0c;showDialog()方法返回對(duì)話框所選擇的顏色對(duì)象。如果單擊“ 撤銷”按鈕或關(guān)閉圖標(biāo),那預(yù)覽樣品文本樣品文本顏色對(duì)話框?qū)⑾?#xff0c;showDialog0方 法返回null.
在下面的例子19中,當(dāng)用戶單擊按鈕時(shí),彈出一個(gè)顏色對(duì)話框,然后根據(jù)用戶選擇的顏色來(lái)改變窗口的顏色。程序中顏色對(duì)話框的運(yùn)行效果如圖9.19所示。

public class Example9_19 {public static void main(String args[]) {WindowColor win=new WindowColor();win.setTitle("帶顏色對(duì)話框的窗口"); 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("打開顏色對(duì)話框"); 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,"調(diào)色板",getContentPane().getBackground());if(newColor!=null) {getContentPane().setBackground(newColor); } } }


自定義對(duì)話框:

創(chuàng)建對(duì)話框與創(chuàng)建窗口類似,通過建立JDialog的子類來(lái)建立一個(gè)對(duì)話框類,然后這個(gè)類的一個(gè)實(shí)例,即這個(gè)子類創(chuàng)建的一一個(gè)對(duì)象,就是-個(gè)對(duì)話框。
對(duì)話框是一個(gè)容器,它的默認(rèn)布局是BorderLayout,對(duì)話框可以添加組件,

實(shí)現(xiàn)與用戶的交互操作。需要注意的是,對(duì)話框可見時(shí),默認(rèn)地被系統(tǒng)添加到顯示器屏幕上,因此不允許將一個(gè)對(duì)話框添加到另-一個(gè)容器中。以下是構(gòu)造對(duì)話框的兩個(gè)常用構(gòu)造方法。
●JDialog0構(gòu)造-.個(gè)無(wú)標(biāo)題的初始不可見的對(duì)話框,對(duì)話框依賴一個(gè)默認(rèn)的不可見的窗口,該窗口由Java運(yùn)行環(huán)境提供。

●JDialog(JFrame owner) 構(gòu)造一 個(gè)無(wú)標(biāo)題的初始不可見的無(wú)模式的對(duì)話框,owner是對(duì)話框所依賴的窗口,如果owner取null,對(duì)話框依賴一個(gè)默認(rèn)的不可見的輸入窗口的新標(biāo)題窗口,該窗口由Java運(yùn)行環(huán)境提供。

下面的使用自定義對(duì)話框更改窗口的標(biāo)題

public class Example9_20 {public static void main(String args[]) {MyWindow win=new MyWindow();win.setTitle("帶自定義對(duì)話框的窗口"); win.setSize(620,360);}} import java.awt.*; import javax.swing.*; import java.util.Arrays; public class MyWindow extends JFrame {JButton button; JTextArea showRedBall ;//存放紅球號(hào)碼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,"紅球號(hào)碼","雙色球?qū)υ捒?#34;,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; //該返回的數(shù)組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);//顯示得到的一組隨機(jī)數(shù)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,"參數(shù)取值不正確","消息",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個(gè)不同隨機(jī)整數(shù)(包括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依次取數(shù)組randomNumber元素的值if(m == number){isInArrays=true; //number在數(shù)組里了break;}}if(isInArrays==false){//如果number不在數(shù)組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);} }

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的java实用教程——组件及事件处理——对话框(颜色对话框,自定义对话框)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。