Java?awt-對話框簡單實現(xiàn)方式
Java awt-對話框簡單實現(xiàn)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class dia
{
public static void main(String args[])
{
JButton btn1=new JButton("modal");
JButton btn2=new JButton("unmodal");
JFrame f=new JFrame("DIaloDemo");
f.setSize(300,400);
f.setLocation(300,200);
f.setLayout(new FlowLayout());
f.add(btn1);
f.add(btn2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
final JLabel label=new JLabel();
final JDialog dialog=new JDialog(f,"Dialog");
dialog.setSize(220,150);
dialog.setLocation(350,250);
dialog.setLayout(new FlowLayout());
final JButton btn3=new JButton("sure");
dialog.add(btn3);
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dialog.setModal(true);
if(dialog.getComponents().length==1){
dialog.add(label);
}
label.setText("modal dialog,click to close");
dialog.setVisible(true);
}
});
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dialog.setModal(false);
if(dialog.getComponents().length==1){
dialog.add(label);
}
label.setText("unmodal dialog,click to close");
dialog.setVisible(true);
}
});
btn3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dialog.dispose();
}
});
}
}



Java awt Dialog(對話框)學(xué)習(xí)
Dialog是Window類的子類,是一個容器類,屬于特殊組件。對話框是可以獨立存在的頂級窗口,因此用法與普通窗口的用法幾乎完全一樣。
對話框有如下兩點需要注意
(1)對話框通常依賴于其他窗口,就是通常有一個parent窗口
(2)對話框有非模式(non-modal)和模式(modal)兩種,當(dāng)某個模式對話框被打開之后,該模式對話框總是位于它依賴的窗口之上;在模式對話框被關(guān)閉之前,它依賴的窗口無法獲得焦點
對話框有多個重載構(gòu)造器
它的構(gòu)造器可能有如下3個參數(shù):
(1)owner:指定該對話框所依賴的窗口,既可以是窗口,也可以是對話框。
(2)title:指定該對話框的窗口標(biāo)題
(3)modal:指定該對話框是否是模式的,true貨false
/**
* 程序示范了模式對話框和非模式對話框的用法
*/
package codes11;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogTest {
Frame f = new Frame("測試");
Dialog d1 = new Dialog(f, "模式對話框", true);
Dialog d2 = new Dialog(f, "非模式對話框", false);
Button b1 = new Button("打開模式對話框");
Button b2 = new Button("打開非模式對話框");
public void init() {
d1.setBounds(20, 30, 300, 400);
d2.setBounds(20, 30, 300, 400);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
d1.setVisible(true);
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
d2.setVisible(true);
}
});
f.add(b1);
f.add(b2, BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new DialogTest().init();
}
}
上面程序創(chuàng)建了d1和d2兩個對話框,其中d1是一個模式對話框,而d2是一個非模式對話框(兩個對話框都是空的)。該窗口中還提供了兩個按鈕,分別用于打開模式對話框和非模式對話框。打開模式對話框后鼠標(biāo)無法激活原來的”測試窗口“;但打開非模式對話框后還可以激活原來的”測試窗口“。
運(yùn)行程序,結(jié)果如下圖:

不管是模式對話框還是非模式對話框,打開后都無法關(guān)閉它們,因為程序沒有為這兩個對話框?qū)懯录O(jiān)聽器。如果主程序需要對話框里接收的輸入值,則應(yīng)該把對話框設(shè)置成模式對話框,因為模式對話框會阻塞該程序;如果把對話框設(shè)置成非模式對話框,則可能造成對話框被打開了,但用于并沒有操作該對話框,也沒有向?qū)υ捒蚶镞M(jìn)行輸入,這就會引起主程序的異常。
Dialog類還有一個子類:FileDialog,它代表一個文件對話框,用于打開或者保存文件。FileDialog也提供了幾個構(gòu)造器,可分別支持parent、title和mode三個構(gòu)造參數(shù),其中parent、title指定文件對話框的所屬父窗口和標(biāo)題;而mode指定該窗口用于打開文件或保存文件,該參數(shù)支持如下兩個參數(shù)值:FileDialog.LOAD、FileDialog.SAVE。
FileDialog不能指定是模式對話框或非模式對話框,因為FileDialog依賴于運(yùn)行平臺的實現(xiàn),如果運(yùn)行平臺的文件對話框是模式的,那么FileDialog也是模式的;否則就是非模式的。
FileDialog提供了兩個方法來獲取被打開/保存文件的路徑
(1)getDirectory():獲取FileDialog被打開/保存文件的絕對路徑
(2)getFile():獲取FileDialog被打開/保存文件的文件名
package codes11;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileDialogTest {
Frame f = new Frame("Test");
FileDialog d1 = new FileDialog(f, "選擇需要打開的文件", FileDialog.LOAD);
FileDialog d2 = new FileDialog(f, "選擇需要保存文件的路徑", FileDialog.SAVE);
Button b1 = new Button("打開文件");
Button b2 = new Button("保存文件");
public void init() {
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
d1.setVisible(true);
System.out.println(d1.getDirectory() + d1.getFile());
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
d2.setVisible(true);
System.out.println(d2.getDirectory() + d2.getFile());
}
});
f.add(b1);
f.add(b2, BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new FileDialogTest().init();
}
}
運(yùn)行程序,結(jié)果如下圖:

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Cloud Gateway內(nèi)置的斷言和過濾器作用說明
這篇文章主要介紹了Spring Cloud Gateway內(nèi)置的斷言和過濾器作用說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
SpringSecurity登錄使用JSON格式數(shù)據(jù)的方法
這篇文章主要介紹了SpringSecurity登錄使用JSON格式數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
SpringCloud-Spring?Boot?Starter使用測試及問題小結(jié)
Spring?Boot?Starter?是在?SpringBoot?組件中被提出來的一種概念、簡化了很多煩瑣的配置、通過引入各種?Spring?Boot?Starter?包可以快速搭建出一個項目的腳手架,這篇文章主要介紹了SpringCloud-Spring?Boot?Starter使用測試,需要的朋友可以參考下2022-07-07
SpringBoot2.6.x升級后循環(huán)依賴及Swagger無法使用問題
這篇文章主要為大家介紹了SpringBoot2.6.x升級后循環(huán)依賴及Swagger無法使用問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
JDBC實現(xiàn)Mysql自動重連機(jī)制的方法詳解
最近在工作中發(fā)現(xiàn)了一個問題,通過查找相關(guān)的資料終于解決了,下面這篇文章主要給大家介紹了關(guān)于JDBC實現(xiàn)Mysql自動重連機(jī)制的相關(guān)資料,文中給出多種解決的方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07

