Java Swing graphical interface

Source: Internet
Author: User

People who have learned Java should be very disgusted with the graphical interface of Java, especially in the short-lived Java people. If you want to drag with the mouse like other languages, you can use the Wondosbulider plugin. But it is not so convenient to use. Of course it's very happy for people who don't like to write code.

However, it is not so troublesome to use code to implement the graphical interface of Java. Sum up is ~

1. Define the panels, buttons, text, tags you need to use.

2. Instantiate these. Then put the button, text, and tags in the way you want to put on the panel.

3. Place the Panel on the interface. If the interface is simple, the panel can not be used, the panel can easily manage some buttons.

How to simply add some buttons to that?

Define Panel buttons ~ Instantiate panel buttons ~ Add buttons to the panel.

 PackageSwing;Importjava.awt.FlowLayout;ImportJava.awt.Frame;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel; Public classFramedemoextendsJFrame {PrivateJPanel Jp,jp1;//Declaration Panel, Panel function: Can add button text, etc., easy to manage the interface.     PrivateJButton B1;//Claim Button    PrivateJButton b2,b3;  PublicFramedemo () {Super("Test Window"); JP=NewJPanel (); B1=NewJButton ("button 1"); B2=NewJButton ("button 2");//instantiation of JP B1 B2Jp.add (B1);                    Jp.add (B2);  This. Add (JP);//The buttons are added to the Panel and the panel is added to the interface.          This. SetSize (300,200);//Set window width 300, height         This. setlocation (100,100);//Sets the coordinates of the upper- left corner of the window, where the window opens. 0.0 with top left point         This. Setdefaultcloseoperation (Jframe.exit_on_close); }     Public Static voidMain (string[] args) {Framedemo frame=NewFramedemo (); Frame.setvisible (true); }}

Here are a few layouts to make your interface more beautiful

Flow layouts ~ builds are arranged from left to right onto the panel. is also the default layout

 PackageSwing;Importjava.awt.FlowLayout;ImportJava.awt.Frame;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel; Public classFramedemoextendsJFrame {PrivateJPanel JP;//Declaration Panel, Panel function: Can add button text, etc., easy to manage the interface.     PrivateJButton B1;//Claim Button    PrivateJButton b2,b3,b4;  PublicFramedemo () {Super("Test Window"); JP=NewJPanel ();//instantiate the panel first, and then lay out the panel. (left-justified, horizontal spacing 10, Vertical spacing 15) can be changed. FlowLayout layout =NewFlowLayout (flowlayout.left,10,25);                 Jp.setlayout (layout); B1=NewJButton ("button 1"); B2=NewJButton ("button 2"); B3=NewJButton ("button 3"); B4=NewJButton ("button 4");           Jp.add (B1);        Jp.add (B2);        Jp.add (B3);        Jp.add (B4);  This. Add (JP);//Buttons added to the panel, Panel added to the interface         This. SetSize (300,200);//Set window width 300, height         This. setlocation (100,100);//Sets the coordinates of the upper- left corner of the window, where the window opens.          This. Setdefaultcloseoperation (Jframe.exit_on_close); }     Public Static voidMain (string[] args) {Framedemo frame=NewFramedemo (); Frame.setvisible (true); }}

Execution results

Border Layout ~ There are five locations, in the cardinal and the middle ~ default is Medium

 PackageSwing;Importjava.awt.BorderLayout;Importjava.awt.FlowLayout;ImportJava.awt.Frame;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel; Public classFramedemoextendsJFrame {PrivateJPanel JP;//Declaration Panel, Panel function: Can add button text, etc., easy to manage the interface.     PrivateJButton B1;//Claim Button    PrivateJButton b2,b3,b4;  PublicFramedemo () {Super("Test Window"); JP=NewJPanel ();//instantiate the panel first, and then lay out the panel. (left-justified, horizontal spacing 10, Vertical spacing 15) can be changed. Jp.setlayout (NewBorderLayout ()); B1=NewJButton ("button 1"); B2=NewJButton ("button 2");//instantiation of JP B1 B2B3 =NewJButton ("button 3"); B4=NewJButton ("button 4");           Jp.add (b1,borderlayout.west);    Jp.add (b2,borderlayout.west); //Button 2 overrides the button 1, the button size is not set, and the length of the button will be the length of the panel. Jp.add (B3,borderlayout.north);        Jp.add (B4,borderlayout.south);  This. Add (JP);//Buttons added to the panel, Panel added to the interface         This. SetSize (500,500);//Set window width 500, height         This. setlocation (100,100);//Sets the coordinates of the upper- left corner of the window, where the window opens.          This. Setdefaultcloseoperation (Jframe.exit_on_close); }     Public Static voidMain (string[] args) {Framedemo frame=NewFramedemo (); Frame.setvisible (true); }}

Note that button 1 is covered by button 2

Grid layout and card layout are the same setup steps, the only change is the layout change. As long as you add components in turn, be aware of the location of the components to do their own required interface.

My favorite layout ~null empty layouts. Add to the Panel as you wish, but it's easy to mess up the layout. I put the text boxes, labels, and buttons in the null layout, and the labels and text boxes are placed the same way as the buttons.

 PackageSwing;Importjava.awt.FlowLayout;ImportJava.awt.Frame;ImportJavax.swing.JButton;ImportJavax.swing.JFormattedTextField;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;ImportJavax.swing.JPanel;ImportJavax.swing.JPasswordField;ImportJavax.swing.JTextField; Public classFramedemoextendsJFrame {PrivateJPanel JP;//Declaration Panel, Panel function: Can add button text, etc., easy to manage the interface.     PrivateJButton B1;//Claim Button    PrivateJButton b2,b3,b4; PrivateJLabel lname, lpwd; PrivateJTextField txtname; PrivateJPasswordField txtpwd;  PublicFramedemo () {Super("Test Window"); JP=NewJPanel (); Jp.setlayout (NULL); B1=NewJButton ("Landing");//Add to button creationB2 =NewJButton ("Cancel"); B1.setbounds (60, 90, 60, 25); B2.setbounds (125, 90, 60, 25);           Jp.add (B1);        Jp.add (B2); LName=NewJLabel ("User name");//add to label creationLpwd =NewJLabel ("Password"); Txtname=NewJTextField (20); Txtpwd=NewJPasswordField (20); Txtpwd.setechochar (‘*‘); Lname.setbounds (30,30,60,25); Txtname.setbounds (95, 30, 120, 25); Lpwd.setbounds (30,60,60,25); Txtpwd.setbounds (95,60,120,25);        Jp.add (lname);        Jp.add (txtname);        Jp.add (LPWD);                Jp.add (TXTPWD);  This. Add (JP);//Buttons added to the panel, panel added to the bounds '         This. SetSize (250,170);  This. setlocation (300,300);//Sets the coordinates of the upper- left corner of the window, where the window opens.          This. setresizable (false);//window cannot be changed size         This. Setdefaultcloseoperation (Jframe.exit_on_close); }     Public Static voidMain (string[] args) {Framedemo frame=NewFramedemo (); Frame.setvisible (true); }}

Would you put some components on your own? The steps to add the check box are the same, but be careful to set up more panels to manage the labels for each check box.

Java Swing graphical interface

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.