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:jdialog form when a dialog box is in the swing component,
* The function of JDialog is to eject another form from one form, just like the OK dialog box that pops up when using a browser
*
* 2:jdialog forms are similar to JFrame forms, and you need to call the Getcontentpane () method when you use the
* The form is converted to a container, and then the properties of the form are set in the container
*
* 3:jdialog has five construction methods that can be used to specify a caption, form, and modal dialog box
* @author Biexiansheng
*
*/
Public class Jdialogtest extends jdialog{
Public jdialogtest () {
//Instantiate a JDialog class object, specifying the dialog's parent form, form title, and type
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 JFrame object
Container Container=jf.getcontentpane ();//Convert a form to a container
JButton jb=new JButton ("Popup Dialog");
Jb.setbounds (10, 10, 100, 20);//Set the size of the button
Jb.addactionlistener (New ActionListener () {
//define anonymous internal classes so that you can click on the reaction
@Override
Public void actionperformed (ActionEvent e) {
//TODO auto-generated method stub
new Jdialogtest (). setvisible (true);
}
});
Container.add (JB);//Add a button to the container, which is very important, or it cannot be displayed
//Set the properties of the container's structure
Jf.settitle ("This is the form converted to a container");
jf.setsize (200,200);//Set the size of the container
jf.setvisible (TRUE);//Make the form visible
//Set the closing mode of the form
jf.setdefaultcloseoperation (windowconstants.exit_on_close);
}
Public static void Main (string[] args) {
//TODO auto-generated method stub
jdialogtest jd=new jdialogtest ();
JD. MyFrame ();
}
}
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: Button JButton
* @author Biexiansheng
*
*/
Public class MyFrame extends JFrame {
Public void MyFrame () {
JFrame jf=new JFrame ();//Instantiate a JFrame object
Container Container=jf.getcontentpane ();//Convert a form to a container
//container Container=getcontentpane ();
container.setlayout (null);
JLabel jl=new JLabel ("This is a jframe form");//Set the label in the form
jl.sethorizontalalignment (Jlabel.center);//Place the text in the label in the middle of the label
Container.add (JL);//Add a label to the container
JButton jb=new JButton ("point Me");//Instantiate a button property
Jb.setbounds (20,100, a);
Jb.addactionlistener (New ActionListener () {
@Override
Public void actionperformed (ActionEvent e) {
//TODO auto-generated method stub
//Make myjdialog form visible
new Myjdialog (myframe.this). setvisible (true);
//The above sentence makes the dialog form visible, which enables the function of the dialog box to pop up when the user is single-click the button
}
});
Container.add (JB);//Add button properties to the container
//Set property characteristics inside the container
Container.setbackground (color.blue);
//Set the frame structure characteristics of the container
Jf.settitle ("This is a container");//Set the title of the container
jf.setvisible (TRUE);//Set Container visualization
jf.setsize (450, 400);//Set the size of the container
//Set how the container is closed
jf.setdefaultcloseoperation (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{
//This instance code can see that the JDialog form and the JFrame form form basically the same, even when setting the form's Properties
//When the method name is called basically the same, such as setting the size of the form, setting the closing state of the form, etc.
Public Myjdialog (MyFrame frame) {//Define a constructor method
//Instantiate a JDialog class object, specify the dialog box's parent form, form title, and type
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);
}
}
Simple learning of the Swing part JDialog dialog form in Java Learning