Java Swing Learning

Source: Internet
Author: User

In the course of Java learning, we often lose the fun of learning Java because of the boring console program, so today we start to learn about the Java swing. GUI (graphical user interface), when applied to the GUI, you can realize the fun of programming.

Let me show you how to set up a window and the layout of components in a container in Java, as an example of a program I've written. The results of the program run as follows:

Start with the image itself: we can see a window with so five buttons on top of the window, and then five buttons at the border of the form. So there is the border layout. From the program we also use this logic to achieve. Create a window to apply to the Java.awt.Frame class

The code is as follows:

Call a constructor of the parent class to create a window super ("Border layout") titled "Boundary Layout";//Set the size of the window, in pixels this.setsize (200,300);// Sets the starting position of the window (upper-left corner coordinates) this.setlocation (100,100);//Set the default action This.setdefaultcloseoperation (jframe.exit_on_close) When the window is closed;

Then the Create button

// instantiating a Panel object     New JPanel (); // create a Boundary Layout manager object and set the layout to the panel  P.setlayout (new  borderlayout ());   New JButton ("button East");   New JButton ("button West");   New JButton ("button South");   New JButton ("button North");   New JButton ("in button");

Finally, add the buttons to the panel and set the layout style.

1 p.add (b1,borderlayout.east); 2 p.add (b2,borderlayout.west); 3 p.add (B3,borderlayout.south); 4 p.add (B4,borderlayout.north); 5  P.add (B5,borderlayout.center);

The classes contained therein are java.awt.BorderLayout, and the main code is these.

The complete code is as follows:

1  Packagecom.example;2 Importjava.awt.BorderLayout;3 Importjavax.swing.*;4 5 6  Public classBorderlayoutdemoextendsjframe{7     PrivateJPanel p;8     PrivateJButton b1,b2,b3,b4,b5;9      PublicBorderlayoutdemo () {Ten         Super("Border Layout"); Onep =NewJPanel (); A         //create a Boundary Layout manager object and set the layout to the panel -P.setlayout (NewBorderLayout ()); -B1 =NewJButton ("button East"); theB2 =NewJButton ("button West"); -B3 =NewJButton ("button South"); -B4 =NewJButton ("button North"); -B5 =NewJButton ("in button"); +  - p.add (b1,borderlayout.east); + p.add (b2,borderlayout.west); A p.add (B3,borderlayout.south); at p.add (B4,borderlayout.north); - p.add (b5,borderlayout.center); -  -          This. Add (p); -          This. SetSize (1000,1000); -          This. setlocation (100,100); in          This. Setdefaultcloseoperation (jframe.exit_on_close); -     } to      Public Static voidMain (String[]args) { +Borderlayoutdemo f =NewBorderlayoutdemo (); -F.setvisible (true); the     } *}

Java Swing Learning

Related Article

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.