Java GUI (graphical user interface) exercises

Source: Internet
Author: User

6.1.3 A simple GUI program

Package six; Import javax.swing.*; Import java.awt.*; Import java.awt.event.*; public class Main {private static int numclicks = 0;  Number of records private static JFrame frame = new JFrame ("Simple GUI");p rivate static JLabel label = new JLabel ("button click:" + "0    ");p Riva Te static JButton button = new JButton ("Click me! ~ ~ ~ ");p rivate static JPanel pane = new JPanel ();  Container public static void main (string[] args) {Pane.setborder (Borderfactory.createemptyborder (60,60,20,60)); Pane.setlayout (new GridLayout (0,1));  Set the layout pane.add (button);p ane.add (label), Frame.getcontentpane (). Add (pane,borderlayout.center); button.setmnemonic (keyevent.vk_i); Button.addactionlistener (new ActionListener () {  //mouse click event public void actionperformed ( ActionEvent e) {numclicks++;label.settext ("button clicks:" +numclicks);}); Frame.setdefaultcloseoperation (Jframe.exit_on_close); Frame.pack (); frame.setvisible (True);}}        

6.4.2 using the FlowLayout layout manager

FlowLayout can automatically adjust the layout according to the size of the window

Package six; Import javax.swing.*; Import java.awt.*; Import java.awt.event.*; public class Main {JButton b1,b2,b3,b4,b5; JFrame frame;  Form public void display () {frame = new JFrame ("Test book layout manager"); Frame.setdefaultcloseoperation (jframe.exit_on_close);//b1 = new JButton ("button 1"), b2 = new JButton ("button 2"), B3 = new JButton ("button 3"), B4 = new JButton ("button 4"), B5 = new JButton ("button 5"); FlowLayout FlowLayout = new FlowLayout (); Sequential layout manager Container ContentPane = Frame.getcontentpane ();  Container contentpane.setlayout (FlowLayout); Contentpane.add (B1); Contentpane.add (B2); Contentpane.add (b3); Contentpane.add (B4); Contentpane.add (B5); Contentpane.setsize (200,100); Frame.pack (); frame.setvisible (true);} public static void Main (String[]args) {new Main (). display ();//Silly B}}        
6.4.3 using the Zone layout manager borderlayout

BorderLayout Sub-east, how to scale, its north and south always maintain the best height.

Package six; Import javax.swing.*; Import java.awt.*; Import java.awt.event.*; public class Main {JButton b1,b2,b3,b4,b5; JFrame frame;  Form public void display () {frame = new JFrame ("Test book layout manager"); Frame.setdefaultcloseoperation (jframe.exit_on_close);//b1 = new JButton ("button 1"), b2 = new JButton ("button 2"), B3 = new JButton ("button 3"), B4 = new JButton ("button 4"), B5 = new JButton ("button 5"); BorderLayout borderlayout = new BorderLayout (); Container ContentPane = Frame.getcontentpane ();  Container contentpane.setlayout (borderlayout); Contentpane.add (b1,borderlayout.east); Contentpane.add (B2, Borderlayout.west); Contentpane.add (B3,borderlayout.south); Contentpane.add (B4,borderlayout.north); Contentpane.add (B5,borderlayout.center); contentpane.setsize (200,100); Frame.pack (); frame.setvisible (true);} public static void Main (String[]args) {new Main (). display ();//Silly B}}        

6.4.5 Card Layout Manager cardlayout

The card layout manager takes a container as a card, with only one card visible at a time.

Package six; Import javax.swing.*; Import java.awt.*; Import java.awt.event.ActionEvent;  Import Java.awt.event.ActionListener; public class Main extends jframe{//class main inherits from Jframeprivate JPanel pane = null;private JPanel p = null;private cardlayout C ard = null;private JButton button_1 = null;private JButton button_2 = null;private JButton B1 = null,b2 = Null,b3 = Null;p Rivate JPanel p1 = null,p2 = Null,p3 = Null;public Main ()//{super ("Card layout manager test"); Try{uimanager.setlookandfeel (" Com.sun.java.swing.plaf.windows.WindowsLookAndFeel ");} catch (Exception ex) {ex.printstacktrace ();}  Create a new card layout card = new CardLayout (5,5);p ane = new JPanel (card);p = new JPanel (); button_1 = new JButton ("< previous step"); button_2 = New JButton ("Next >"), B1 = new JButton ("1"), b2 = new JButton ("2"), B3 = new JButton ("3"); B1.setmargin (New Insets (2,2,2, 2); B2.setmargin (new Insets (2,2,2,2)); B3.setmargin (new Insets (2,2,2,2));p. Add (button_1);p. Add (B1);p. Add (B2); P.add (B3);p. Add (button_2);p 1 = new JPanel ();p 2 = new JPanel ();p 3 = new JPanel ();p 1.setBackground (color.red);p 2.setBackground (color.blue);p 3.setBackground (color.green);p 1.add (new JLabel ("Jpanel_1"));p 2.add (New JLabel ("Jpanel_2"));p 3.add (New JLabel ("Jpanel_3"));p Ane.add (P1, "P1");p Ane.add (P2, "P2");p Ane.add (P3, "P3");//Flip Card Layout action Button_1.addactionlistener (new ActionListener () {public void actionperformed ( ActionEvent e) {card.previous (pane);}); Button_2.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {card.next (pane);}}); B1.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {card.show (pane, "P1");}); B2.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {card.show (pane, "P2");}); B3.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {card.show (pane, "P3");}); This.getcontentpane (). Add (Pane), This.getcontentpane (). Add (P,borderlayout.south); This.setdefaultcloseoperation ( Jframe.exit_on_close); This.setsize (+); this.setvisible (true);} Public STAtic void Main (String[]args) {new Main ();//Silly B}}         


Java GUI (graphical user interface) exercises

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.