Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Java awt dialog box

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "Java awt dialog box how to achieve", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java awt dialog box how to achieve" it!

The Java awt- dialog box simply implements 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 (300400) F.setLocation (300200); 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 (220150); dialog.setLocation (350250); 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 (dialog box) learning

Dialog is a subclass of the Window class, a container class, and a special component. A dialog box is a top-level window that can exist independently, so it is used almost exactly the same as a normal window.

The dialog box has the following two points to note

(1) Dialogs usually depend on other windows, that is, there is usually a parent window

(2) there are two types of dialog boxes: non-modal and modal. When a mode dialog box is opened, the mode dialog box is always on top of the window on which it depends; until the mode dialog box is closed, the window it depends on cannot get focus.

The dialog box has multiple overloaded constructors

Its constructor may have the following three parameters:

(1) owner: specify the window on which the dialog box depends, either a window or a dialog box.

(2) title: specify the window title of the dialog box

(3) modal: specify whether the dialog box is modal, true goods false

/ * the program demonstrates the use of modal and modeless dialogs * / 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 ("test"); Dialog D1 = new Dialog (f, "modal dialogs", true) Dialog D2 = new Dialog (f, "modeless dialog", false); Button b1 = new Button ("Open modal dialog"); Button b2 = new Button ("open modeless dialog"); 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 ();}}

The above program creates two dialogs D1 and D2, where D1 is a modal dialog box and D2 is a modeless dialog box (both dialogs are empty). Two buttons are also provided in this window, which are used to open the modal dialog box and the modeless dialog box. The mouse cannot activate the original Test window after opening the modal dialog box, but you can also activate the original Test window after opening the modeless dialog box.

Run the program, and the result is as follows:

Both modal and modeless dialogs cannot be closed when opened because the program does not write event listeners for both dialogs. If the main program needs the input value received in the dialog box, the dialog box should be set to the mode dialog box, because the mode dialog box will block the program; if the dialog box is set to non-mode dialog box, it may cause the dialog box to be opened, but it is not used to operate the dialog box, and there is no input to the dialog box, which will cause the exception of the main program.

The Dialog class also has a subclass: FileDialog, which represents a file dialog box for opening or saving files. FileDialog also provides several constructors that support three construction parameters: parent, title and mode, respectively, in which parent and title specify the parent window and title of the file dialog box, while mode specifies that the window is used to open or save files. This parameter supports the following two parameter values: FileDialog.LOAD, FileDialog.SAVE.

FileDialog cannot be specified as a modal dialog box or a non-modal dialog box, because FileDialog depends on the implementation of the running platform, and if the running platform's file dialog box is modal, then FileDialog is also modal; otherwise, it is non-modal.

FileDialog provides two methods to get the path of the opened / saved file

(1) getDirectory (): get the absolute path of the FileDialog opened / saved file

(2) getFile (): get the file name of the FileDialog opened / saved file

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, "Select the file you want to open", FileDialog.LOAD); FileDialog D2 = new FileDialog (f, "choose the path to save the file", FileDialog.SAVE) Button b1 = new Button ("open file"); Button b2 = new Button ("save file"); 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 ();}}

Run the program, and the result is as follows:

Thank you for your reading, the above is the content of "how to achieve the Java awt dialog box", after the study of this article, I believe you have a deeper understanding of how to achieve the Java awt dialog box, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report