Java GUI (graphical user interface) exercises

Source: Internet
Author: User

Java GUI (graphical user interface) exercises

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 "); private static JLabel label = new JLabel ("button clicks:" + "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); // you can specify pane for the layout. add (button); pane. add (label); frame. getContentPane (). add (pane, BorderLayout. CENTER); button. setMnemonic (KeyEvent. VK_ I); button. addActionListener (new ActionListener () {// click the event public void actionreceivmed (ActionEvent e) {numclicks ++; label. setText ("button clicks:" + numclicks) ;}}); frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); frame. pack (); frame. setVisible (true );}}

6.4.2 use FlowLayout layout manager

FlowLayout can automatically adjust the layout according to the window size

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 sequence layout manager"); frame. setdefaclocloseoperation (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 (); // The 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 use the regional layout manager BorderLayout

BorderLayout is divided into the east, west, and north, and 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 sequence layout manager"); frame. setdefaclocloseoperation (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 uses a 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 inherited from JFrameprivate 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 card = new CardLayout (5, 5); pane = new JPanel (card); p = new JPanel (); button_1 = new JButton ("<previous"); button_2 = new JButton ("next>"); b1 = new JButton ("1 "); b2 = new JButton ("2"); b3 = new JButton ("3"); b1.setMargin (new Insets (,); b2.setMargin (new Insets, ); b3.setMargin (new Insets (,); 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"); pane. add (p2, "p2"); pane. add (p3, "p3"); // flip card layout action button_1.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {card. previous (pane) ;}}); button_2.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {card. next (pane) ;}}); b1.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {card. show (pane, "p1") ;}}); b2.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {card. show (pane, "p2") ;}}); b3.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {card. show (pane, "p3") ;}}); this. getContentPane (). add (pane); this. getContentPane (). add (p, BorderLayout. SOUTH); this. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); this. setSize (300,200); this. setVisible (true);} public static void main (String [] args) {new Main (); // silly B }}


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.