Java GUI programming (swing) Five swing panel, text box, password box, label

Source: Internet
Author: User

Panel assembly (Jpanel). An interface with only one jframe, but can have multiple JPanel components and JPanel components can be placed FlowLayout, borderlayout,gridlayout components, such a combination of use to achieve more complex layout effects   such as:  package gui;import java.awt.borderlayout;import Java.awt.gridlayout;import java.awt.FlowLayout;import Javax.swing.jbutton;import javax.swing.jframe;import javax.swing.jpanel;/** Panel assembly * Created by admin on 2017/7/2.*/ public class Myjpanel extends jframe{//defines the component to be used private JButton jbutton[] = new Jbutton[6];p rivate JPanel jPanel1, JPanel 2;public static void Main (string[] args) {Myjpanel Myjpanel = new Myjpanel ();} Initialize constructor public Myjpanel () {//Create two JPanel panel JPanel default is streaming layout jPanel1 = new JPanel (); jPanel2 = new JPanel ();//Create button component for (int i=0; i<jbutton.length; i++) {Jbutton[i] = new JButton (string.valueof (i));} Add components to JPanel//Jpanel1.add (jbutton[0]); Jpanel1.add (jbutton[1]); Jpanel1.add (jbutton[2]); Jpanel2.add (Jbutton[3] ); Jpanel2.add (Jbutton[4]); Jpanel2.add (jbutton[5]);//Add JPanel to JFrame and specify position this.add (JPanel1, Borderlayout.north); This.add (Jbutton[0], BorderlayoUt. CENTER); This.add (JPanel2, Borderlayout.south);//Set JFrame property This.settitle ("Use of panel JPanel"); This.setlocation (500, 250 ); This.setsize (+); this.setdefaultcloseoperation (jframe.exit_on_close); this.setvisible (True);}}  jpanel Note: JPanel also belongs to the container class component, which can be placed on other components JPanel the default layout of the components placed on the flow layout is the streaming layouts flowlayout  file box component (JTextField) in the AWT package   Password box JPasswordField in swing package   use label + text box + Password box + panel to do landing page  package gui;import java.awt.gridlayout;import java.awt. Textfield;import javax.swing.jpasswordfield;import javax.swing.*; /** File box, Password box, Label Introduction * Use these components to make a login box * Created by Admin on 2017/7/2.*/public class MyTextField extends jframe{//define the controls that need to be used, buttons 2 Panels 3 labels 3private JButton JButton, JButton2 ;p rivate JPanel JPanel, JPanel2, jpanel3;private TextField textfield;private JLabel JLabel, jlabel2;private JPasswordField jpasswordfield;public static void Main (string[] args) {MyTextField MyTextField = new MyTextField ();} Class initialization, when initializing the control to create the public MyTextField () {//create panel component JPanel = new JPanel (); jPanel2 = new JpaneL (); jPanel3 = new JPanel ();//Create label Component JLabel = new JLabel ("username:"); jLabel2 = new JLabel ("Password:");//Create text box component 10 for width TextField = New TextField (10);//Create Password Box component 10 is width JPasswordField = new JPasswordField (10);//Create button component JButton = new JButton ("login"); JButton2 = New JButton ("register");//Set JFrame layout style to Grid layout, 3 rows 1 columns this.setlayout (new GridLayout (3, 1));//Add individual components//user name tag + textbox jpanel.add (jLa BEL); Jpanel.add (TextField);//password tag + Password box jpanel2.add (JLABEL2); Jpanel2.add (JPasswordField);//Login + Register button Jpanel3.add ( JButton); Jpanel3.add (jButton2);//Add panel Jframethis.add (jPanel); This.add (JPanel2); This.add (JPANEL3);// Set JFrame property this.settitle ("Login"); This.setlocation (+); this.setsize (n); this.setresizable (false); This.setdefaultcloseoperation (Jframe.exit_on_close); this.setvisible (True);}}

Java GUI programming (swing) Five swing panel, text box, password box, label

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.