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