JFrame Frame Form
The JFrame form is a container that is the carrier of the various components in the swing program, and can be described as a container that hosts these swing components JFrame. The JFrame form has the maximize, minimize, and Close buttons.
Common methods
1. Construction method
JFrame()
Constructs a new form that is not visible initially.
JFrame(String title)
Creates a new, initially invisible, with the specified caption Frame
.
Note: The form that is created by the constructor method is hidden by default.
2. Common methods
Container getContentPane()
returns the object for this form contentPane
. of the component class.
public void setvisible(Boolean b) sets the form to be hidden and displayed. of the window class.
public void setSize(int width, int height) Adjusts the size of the component so that it is width width
and height height
.
voidsetBounds(int x, int y, int width, int height) 移动组件并调整其大小。
void setIconImage(Image image)
Sets the image to display as the icon for this window.
Component
add(Component comp)
appends the specified component to the tail of this container.
Void setDefaultCloseOperation(int operation)
sets the action that the user performs by default when "Close" is initiated on this form.
Value of operation:
Do_nothing_on_close (defined in windowconstants): Performs no action and requires the program to handle the operation in the Windowclosing method of the registered WindowListener object.
Hide_on_close (defined in windowconstants): The form is automatically hidden after any registered WindowListener object is called.
Dispose_on_close (defined in windowconstants): Automatically hides and releases the form after calling any registered WindowListener object.
Exit_on_close (defined in JFrame): Exits the application using the System exit method. Used only in the application.
By default, this value is set to Hide_on_close. Changing the value of this property causes the property change event to be fired and its property name is "Defaultcloseoperation".
code example:
1 Public Static voidMain (string[] args) {2JFrame frame =NewJFrame ("Test form");//create a form with a caption3Frame.setsize (300, 300);//Set Size4Frame.setlocationrelativeto (NULL);//Center Display5Frame.seticonimage (Toolkit.getdefaulttoolkit (). CreateImage ("E:\\workspace\\demo\\src\\img_1880.png"));6Frame.setdefaultcloseoperation (Jframe.exit_on_close);//Click X to exit the virtual machine.7JButton button =NewJButton ("I am a button");8Frame.add (button);//Adding Components9 //Container Container = Frame.getcontentpane ();Ten //container.add (button); OneFrame.setvisible (true);//Set Display A}
View Code
JDialog dialog box
The code examples are as follows:
1 Public Static voidMain (string[] args) {2JFrame frame =NewJFrame ("Test form");//create a form with a caption3Frame.setsize (300, 300);//Set Size4Frame.setlocationrelativeto (NULL);//Center Display5 frame.setdefaultcloseoperation (jframe.exit_on_close);6 7JDialog dialog =NewJDialog (Frame, dialog box));8Dialog.setbounds (300, 300, 100, 100);9 Dialog.setlocationrelativeto (frame);TenDialog.setvisible (true); OneDialog.add (NewJLabel ("I am a dialog box")); AFrame.setvisible (true); -}
View Code
JDialog is not easy to use and does not clearly play a role. So replace them with Joptionpane.
Joptionpane Use the method code as follows:
1 Public classDemodialog {2 Public Static voidMain (string[] args) {3JFrame frame =NewJFrame ("Test form");//create a form with a caption4Frame.setsize (300, 300);//Set Size5Frame.setlocationrelativeto (NULL);//Center Display6 frame.setdefaultcloseoperation (jframe.exit_on_close);7 8 9 //message dialog boxTenJoptionpane.showmessagedialog (frame, "Notification content", "notifications", joptionpane.information_message); One //warning dialog box AJoptionpane.showmessagedialog (frame, "warning content", "warning", joptionpane.warning_message); - //error dialog box -Joptionpane.showmessagedialog (frame, "Error content", "error", joptionpane.error_message); the - //Input Box -String Moeny = Joptionpane.showinputdialog ("Please enter the amount you want to give me ($)"); -System.out.println ("Money:" +moeny); + - //Confirmation Box + intnum = Joptionpane.showconfirmdialog (frame, "Are you sure you want to uninstall?") "); A System.out.println (num); at}
View Code
FileDialog file dialog box
code example:
1 Public classDemo3 {2 3 Public Static voidMain (string[] args) {4JFrame frame =NewJFrame ("form");5 //Create a File dialog box (initially also not visible)6 //FileDialog filedialog = new FileDialog (frame, "Please select Open file", filedialog.load);7FileDialog FileDialog =NewFileDialog (Frame, "Select the Saved path", filedialog.save);8Frameutil.initframe (Frame, 300,400);9Filedialog.setvisible (true);TenSystem.out.println ("directory where the file is located:" +filedialog.getdirectory ()); OneSystem.out.println ("File name:" +filedialog.getfile ()); A } -}
View Code
JPanel Panel
code example:
1 Public classDemo4 {2 3 Public Static voidMain (string[] args) {4JFrame frame =NewJFrame ("form");5 //Create a panel6JPanel Panel =NewJPanel ();7 //set the background color of the panel8 Panel.setbackground (color.red);9 //Add a panel to a formTen Frame.add (panel); OneFrameutil.initframe (frame, 400, 300); A } -}
View Code
Jscorollpane Panel
The Jscorllpane panel is a panel with a scrollbar, which is also a container. However, Jscorllpane can only place one component and cannot use the layout manager. If you need to place multiple components in the Jscorllpane panel, you need to place multiple components on the JPanel panel, and then add the JPanel panel as a whole component on the Jscorllpane panel.
The code examples are as follows:
1 Public Static voidMain (string[] args) {2JFrame frame =NewJFrame ("Test form");//create a form with a caption3Frame.setsize (300, 100);//Set Size4Frame.setlocationrelativeto (NULL);//Center Display5Frame.seticonimage (Toolkit.getdefaulttoolkit (). CreateImage ("E:\\workspace\\demo\\src\\img_1880.png"));6Frame.setdefaultcloseoperation (Jframe.exit_on_close);//Click X to exit the virtual machine.7JLabel label =NewJLabel (" This is a scrolling label which is a scrolling label which is a scrolling label which is a scrolling label which is a scrolling label which is a scrolling label which is a scroll labels which is a scrolling label which is a scrolling label which is a scrolling label This is a scrolling label which is a scrolling label which is a scrolling label which is a scrolling label which is a scrolling label which is a scrolling label which is a scrolling label ");8JScrollPane ScrollPane =NewJScrollPane (label);9 Frame.add (scrollPane);TenFrame.setvisible (true);//Set Display One}
View Code
GUI Programming (ii)-----container Components