Four common layouts for Java Swing

Source: Internet
Author: User

The third card layout can be used to complete a simple lottery program, which is quite interesting.

One: Flow layout flowlayout

Implementation code:

Import java.awt.FlowLayout; import javax.swing.JFrame; import Javax.swing.JButton; class tester{public    static void Main (String args[])    {        JFrame frame = new JFrame ("streaming layout");        Frame.setlayout (New FlowLayout (flowlayout.center,3,3));        JButton button = null;        for (int i=0;i<20;i++)        {button            = new JButton ("button" +i);            Frame.add (button);        }        Frame.setsize (600,600);        Frame.setvisible (True);}    }









II: Boundary layout Manager

Code:

Import java.awt.BorderLayout; import Javax.swing.JButton; import javax.swing.JFrame; class tester{public   static void Main (String args[])   {       JFrame frame = new JFrame ("Boundary Layout");       Frame.setlayout (New BorderLayout (3,3));       Frame.add (New JButton ("East"), Borderlayout.east);       Frame.add (New JButton ("West"), borderlayout.west);       Frame.add (New JButton ("South"), Borderlayout.south);       Frame.add (New JButton ("North"), Borderlayout.north);       Frame.add (New JButton ("Medium"), borderlayout.center);       Frame.setsize (600,600);       Frame.setvisible (True);}   }







Three: Table layout (GridLayout)

Code:

Import java.awt.GridLayout; import javax.swing.JFrame; import Javax.swing.JButton; class tester{public    static void Main (String args[])    {        JFrame frame = new JFrame ("Table layout");        Frame.setlayout (New GridLayout (3,5,13,13));  The four digits correspond to the horizontal spacing of the ranks, the vertical spacing        JButton button = null;        for (int i=0;i<20;i++)        {button            = new JButton ("button" +i);            Frame.add (button);        }        Frame.pack ();   Automatically adjusts        frame.setvisible (true) according to the panel size required by the component;}    }






Four: Card layout

Code:

Import java.awt.Container; import javax.swing.JFrame; import Javax.swing.JLabel; import java.awt.CardLayout; class        tester{public static void Main (String args[]) {JFrame frame = new JFrame ("card management");        CardLayout card = new CardLayout ();        Frame.setlayout (card);        Container con = Frame.getcontentpane ();        Con.add (New JLabel ("Candidate number One", Jlabel.center), "first");        Con.add (New JLabel ("Candidate No. Second", Jlabel.center), "second");        Con.add (New JLabel ("Candidate No. Third", Jlabel.center), "third");        Con.add (New JLabel ("Candidate No. Fourth", Jlabel.center), "fourth");        Con.add (New JLabel ("Candidate No. Fifth", Jlabel.center), "fifth");        Frame.pack ();        Frame.setvisible (TRUE); Card.show (Con, "third");//show a third for (int i=0;i<5;i++) {try {Thread.slee           P (1500);           }catch (Interruptedexception IE) {ie.printstacktrace ();  } card.next (Con); Traversal}}}




Four common layouts for Java Swing

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.