In JAVA learning, simple learning of the Swing part JDialog Dialog Box form, swingjdialog

Source: Internet
Author: User

In JAVA learning, simple learning of the Swing part JDialog Dialog Box form, swingjdialog

Package com. swing;

Import java. awt. Color;
Import java. awt. Container;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;

Import javax. swing. JButton;
Import javax. swing. JDialog;
Import javax. swing. JFrame;
Import javax. swing. JLabel;
Import javax. swing. WindowConstants;

/**
* 1: the dialog box in the Swing component in the JDialog form,
* The JDialog function is to pop up another form from one form, just like the confirmation dialog box popped up when using a browser.
*
* 2: The JDialog form is similar to the JFrame form. You must call the getContentPane () method
* Convert the form to a container, and then set the form features in the container.
*
* 3: JDialog has five constructor methods to specify the title, form, and mode dialog box.
* @ Author biexiansheng
*
*/
Public class JDialogTest extends JDialog {

Public JDialogTest (){
// Instantiate a JDialog Class Object, specifying the parent form, Form title, and type of the dialog box
Super ();
Container container = getContentPane ();
Container. setBackground (Color. green );
Container. add (new JLabel ("this is a dialog box "));
SetBounds (120,120,100,100 );
}

Public void MyFrame (){
JFrame jf = new JFrame (); // instantiate a JFrame object
Container container = jf. getContentPane (); // convert the form to a Container
JButton jb = new JButton ("pop-up dialog box ");
Jb. setBounds (10, 10,100, 20); // set the button size
Jb. addActionListener (new ActionListener (){
// Define the anonymous internal class so that the response can be clicked
@ Override
Public void actionreceivmed (ActionEvent e ){
// TODO Auto-generated method stub
New JDialogTest (). setVisible (true );;
}
});
Container. add (jb); // add the button to the container. This is very important, otherwise it cannot be displayed.
// Set the characteristics of the container structure
Jf. setTitle ("this is the form to container ");
Jf. setSize (200,200); // set the container size
Jf. setVisible (true); // make the form visible
// Set the close mode of the form
Jf. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE );
}
Public static void main (String [] args ){
// TODO Auto-generated method stub
JDialogTest jd = new JDialogTest ();
Jd. MyFrame ();
}

}

The standard cases are as follows:

Package com. swing;

Import java. awt. Color;
Import java. awt. Container;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;

Import javax. swing. JButton;
Import javax. swing. JDialog;
Import javax. swing. JFrame;
Import javax. swing. JLabel;
Import javax. swing. WindowConstants;

/**
* 1: JButton
* @ Author biexiansheng
*
*/
Public class MyFrame extends JFrame {

Public void MyFrame (){
JFrame jf = new JFrame (); // instantiate a JFrame object
Container container = jf. getContentPane (); // convert the form to a Container
// Container container = getContentPane ();
Container. setLayout (null );

JLabel jl = new JLabel ("This Is A JFrame form"); // set a label in the form
Jl. setHorizontalAlignment (JLabel. CENTER); // place the text in the tag in the middle of the tag
Container. add (jl); // add the tag to the container

JButton jb = new JButton ("Click me"); // instantiate a button Property
Jb. setBounds (20, 20,100, 50 );
Jb. addActionListener (new ActionListener (){

@ Override
Public void actionreceivmed (ActionEvent e ){
// TODO Auto-generated method stub
// Make the MyJDialog form visible
New MyJDialog (MyFrame. this). setVisible (true );
// Make the dialog box form visible in the previous sentence, so that the dialog box will pop up after the button is clicked on a single machine.
}
});
Container. add (jb); // add the button property to the container

// Set the attribute features in the container
Container. setBackground (Color. blue );
// Set the framework structure characteristics of the container
Jf. setTitle ("this is a container"); // set the container title
Jf. setVisible (true); // sets the container visualization.
Jf. setSize (450,400); // set the container size
// Set the container closing Method
Jf. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE );
}
Public static void main (String [] args ){
// TODO Auto-generated method stub
MyFrame fm = new MyFrame ();
Fm. MyFrame ();
}

}

Class MyJDialog extends JDialog {
// The code of this instance shows that the JDialog form and JFrame form are basically the same, and even the features of the form are set.
// The Name Of The method called at the time is basically the same, for example, setting the size of the form, setting the closing status of the form, etc.
Public MyJDialog (MyFrame frame) {// defines a constructor
// Instantiate a JDialog Class Object, specifying the parent form, Form title, and type of the dialog box
Super (frame, "first JDialog form", true );
Container container = getContentPane (); // create a Container
Container. add (new JLabel ("this is a dialog box"); // add a label to the container
Container. setBackground (Color. green );
SetBounds (120,120,100,100 );

}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.