Java user interface labels and text boxes

Source: Internet
Author: User

The Jlable component displays information that the user cannot modify, and the JTextField component is the area where the user can enter a single line of text

Create a jlable component and a JTextField object, and add them to the container :

JLabel PageLabel = new JLabel ("Web page address:", jlabel.right);
JTextField pageaddress = new JTextField (20);
FlowLayout Flo = new FlowLayout ();
SetLayout (FLO);
Add (PageLabel);
Add (pageaddress);

jlabel.right: Aligns text to the right of the label

jlabel.left: Aligns text to the left of the label

jlabel.center: center-display text

Demo:

1  PackageCom.swingdemo.demo;2 3 Importjava.awt.FlowLayout;4 5 ImportJavax.swing.JFrame;6 ImportJavax.swing.JLabel;7 ImportJavax.swing.JTextField;8 ImportJavax.swing.UIManager;9 Ten  Public classWebaddressextendsJFrame { One  A     Private Static Final LongSerialversionuid = 1L; -      -      Publicwebaddress () { the  -         Super("Label and Text boxes"); - Setlookandfeel (); -SetSize (400, 100); +JLabel PageLabel =NewJLabel ("Web page address:", jlabel.right); -JTextField pageaddress =NewJTextField (20); +         //set the text of an object APageaddress.settext ("http://java.sun.com"); at         //JTextField pageaddress = new JTextField ("http://java.sun.com", a);//create an object and assign a default value -FlowLayout Flo =NewFlowLayout (); - setlayout (flo); - Add (pagelabel); - Add (pageaddress); -SetVisible (true); in          -         //gets the text that the object contains toString Countrychoice =Pageaddress.gettext (); + System.out.println (countrychoice); -          the     } *  $     Private voidSetlookandfeel () {Panax Notoginseng  -         Try { theUimanager.setlookandfeel ("Com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); +}Catch(Exception e) { A e.printstacktrace (); the         } +          -     } $      $      Public Static voidMain (string[] args) { -          -Webaddress WA =Newwebaddress (); the          -     }Wuyi  the}
View Code

Java user interface labels and text boxes

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.