Swing getting started-simple understanding + demo (1), swing getting started with demo

Source: Internet
Author: User

Swing getting started-simple understanding + demo (1), swing getting started with demo

I have been developing based on java web and have no experience in GUI development for java. Fortunately, I have used winform development before. With the basic routines, there is no pressure on Swing! I searched the internet for relevant learning materials, including drag-and-drop controls and code implementation. For better understanding and understanding, let's get started with code ~

Refer to the relevant blog online, first have a general understanding, such as: http://www.blogjava.net/jerry-zhaoj/articles/283170.html

I chose a PDF as a reference for learning: Java Swing graphic interface development and case study. .wang peng. .pdf

Before learning about Swing, let's take a look at AWT:

AWT (Abstract Windows Toolkit): An Abstract Window toolset that provides interfaces for interacting with a local graphical interface. The underlying layer calls the local operating system. Since the implementation depends on the local operating system, it is a heavyweight component. Swing expands on the basis of AWT implementation, and the components are implemented in pure java mode, it does not depend on the underlying operating system, so it is a lightweight component. AWT was designed to develop a graphical interface for small applications, rather than a toolkit dedicated to user interfaces (UIS, for example, clipboard, pop-up menu, scroll, and other functions are not supported.

Next, we made three demos based on the example in Chapter 1. You don't need to worry about the specific code details in the demo. Just look at the general framework first.

Demo1: logon window

Demo2: menu bar and toolbar

Demo3: tab

Demo1:

Package com. comall. fastsvn. test. chapter1; import javax. swing. *; import java. awt. *;/*** logon interface */public class SwingDemo1 extends JPanel {private static final int JFRAME_WIDTH = 300; private static final int JFRAME_HEIGHT = 150; private SwingDemo1 () {// window-top-layer panel JFrame jFrame = new JFrame ("Logon window"); jFrame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); // grid layout, container method GridBagLayout gridBagLayout = new GridBagLayout (); setLayout (gridBagLayout); jFrame. add (this, BorderLayout. WEST); jFrame. setSize (JFRAME_WIDTH, JFRAME_HEIGHT); // position: center Dimension dimension = Toolkit. getdefatooltoolkit (). getScreenSize (); jFrame. setLocation (int) (dimension. getWidth ()-JFRAME_WIDTH)/2, (int) (dimension. getHeight ()-JFRAME_HEIGHT)/2); // JLabel tipLabel = new JLabel ("Welcome to Java World"); JLabel userLabel = new JLabel ("username :"); JLabel pwdLabel = new JLabel ("Password:"); JTextField userText = new JTextField (15); JTextField pwdText = new JTextField (15 ); JButton submitBtn = new JButton ("login"); JButton cancelBtn = new JButton ("cancel"); // GridBagConstraints gridBagConstraints = new GridBagConstraints (); gridBagConstraints. fill = GridBagConstraints. NONE; gridBagConstraints. anchor = GridBagConstraints. EAST; gridBagConstraints. weightx = 3; gridBagConstraints. weighty = 4; // Add the component and specify the position of the component addComponent (tipLabel, gridBagConstraints,); addComponent (userLabel, gridBagConstraints,); addComponent (dlabel, gridBagConstraints,); addComponent (userText, plain,); addComponent (pwdText, plain,); addComponent (submitBtn, gridBagConstraints ); addComponent (cancelBtn, gridBagConstraints,); // The jFrame is visible on the panel and cannot be edited. setResizable (false); jFrame. setVisible (true );} /*** add component ** @ param component * @ param gridBagConstraints * @ param x row * @ param y column * @ param w width * @ param h height */private void addComponent (Component component, gridBagConstraints gridBagConstraints, int x, int y, int w, int h) {gridBagConstraints. gridx = x; gridBagConstraints. gridy = y; gridBagConstraints. gridwidth = w; gridBagConstraints. gridheight = h; // add (component, gridBagConstraints) in the Container;} public static void main (String [] args) {new SwingDemo1 ();}}

Running effect:

Demo2:

Package com. comall. fastsvn. test. chapter1; import javax. swing. *; import java. awt. *;/*** menu bar, toolbar */public class SwingDemo2 {private static final int JFRAME_WIDTH = 600; private static final int JFRAME_HEIGHT = 400; public SwingDemo2 () {// window-top-layer panel JFrame jFrame = new JFrame ("menu toolbar"); jFrame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); jFrame. setSize (JFRAME_WIDTH, JFRAME_HEIGHT); // Add the JPanel jPanel = new JPanel (); jFrame in the window. setContentPane (jPanel); // Add JMenuBar menuBar = new JMenuBar (); jFrame to the menu bar. setJMenuBar (menuBar); // Add JMenu menu1 = new JMenu ("menu 1") to the menu bar; JMenu menu2 = new JMenu ("menu 2"); menuBar. add (menu1); menuBar. add (menu2); // add sub-menu JMenuItem menuItem11 = new JMenuItem ("sub-menu 11"); JMenuItem menuItem12 = new JMenuItem ("sub-menu 12 "); JMenuItem menuItem21 = new JMenuItem ("sub menu 21"); JMenuItem menuItem22 = new JMenuItem ("sub menu 22"); menu1.add (menuItem11); dimension (); menu1.add (menuItem12 ); menu2.add (menuItem21); menu2.addSeparator (); menu2.add (menuItem22); // Add the JButton jButton1 = new JButton ("tool 1") to the toolbar on the panel "); JButton jButton2 = new JButton ("tool 2"); JButton jButton3 = new JButton ("tool 3"); JToolBar toolBar = new JToolBar (); toolBar. add (jButton1); toolBar. add (jButton2); toolBar. add (jButton3); // layout BorderLayout borderLayout = new BorderLayout (); jPanel. setLayout (borderLayout); jPanel. add ("North", toolBar); // location: center Dimension dimension = Toolkit. getdefatooltoolkit (). getScreenSize (); jFrame. setLocation (int) (dimension. getWidth ()-JFRAME_WIDTH)/2, (int) (dimension. getHeight ()-JFRAME_HEIGHT)/2); // The Panel is visible and cannot be edited. setResizable (false); jFrame. setVisible (true);} public static void main (String [] args) {new SwingDemo2 ();}}

Running effect:

 

Demo3:

Package com. comall. fastsvn. test. chapter1; import javax. swing. *; import java. awt. *;/*****/public class SwingDemo3 extends JTabbedPane {private static final int JFRAME_WIDTH = 600; private static final int JFRAME_HEIGHT = 400; private SwingDemo3 () {// window-top-layer panel JFrame jFrame = new JFrame ("tab"); jFrame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); jFrame. setSize (JFRAME_WIDTH, JFRAME_HEIGHT); jFrame. setVisible (true); // panel JPanel jPanel = new JPanel (); jFrame. setContentPane (jPanel); setLayout (new BorderLayout (); // subpanel JPanel jPanel1 = new JPanel (); JPanel jPanel2 = new JPanel (); jPanel1.setLayout (new BorderLayout (); addTab ("panel1", jPanel1); setEnabledAt (0, true); setTitleAt (0, "test page 1 "); addTab ("panel2", jPanel2); setEnabledAt (1, true); setTitleAt (1, "test page 2"); setPreferredSize (new Dimension (500,200); setTabPlacement (JTabbedPane. TOP); setTabLayoutPolicy (JTabbedPane. SCROLL_TAB_LAYOUT); jPanel. add ("Center", this); setVisible (true);} public static void main (String [] args) {new SwingDemo3 ();}}

Running effect:

 

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.