Java GUI (graphical user interface) exercises

Source: Internet
Author: User
Tags pack

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;  Record number
	private static JFrame frame = new JFrame ("Simple GUI");
	private static JLabel label = new JLabel (Button click Number: + 0    );
	private static JButton button = new JButton ("click Me.") ~~~");
	private 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);
		Pane.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 click Number:" +numclicks);
			}
		);
		Frame.setdefaultcloseoperation (jframe.exit_on_close);
		Frame.pack ();
		Frame.setvisible (True);
	}

        

6.4.2 using FlowLayout layout Manager

FlowLayout can automatically adjust the layout depending on 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 Regional layout manager borderlayout

In the borderlayout, how to scale, the 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 the container as a card and only one card is 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 JFrame private JPanel pane = null;
	Private JPanel p = null;
	Private CardLayout card = null;
	Private JButton button_1 = null;
	Private JButton button_2 = null;
	Private JButton B1 = null,b2 = NULL,B3 = null;
	Private 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 cards = CardLayout (5,5);
		pane = 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);
		P1 = new JPanel ();
		P2 = new JPanel ();
		P3 = new JPanel ();
		P1.setbackground (color.red);
		P2.setbackground (Color.Blue);
		
		P3.setbackground (Color.green);
		P1.add (New JLabel ("Jpanel_1"));
		P2.add (New JLabel ("jpanel_2"));
		P3.add (New JLabel ("Jpanel_3"));
		
		Pane.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.prev
			IOUs (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 (300, 200);
	This.setvisible (TRUE);
         public static void Main (String[]args) {new Main ();//Silly B}}


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.