标签:
JLable组件显示用户不能修改的信息,JTextField组件是用户可以输入单行文本的区域
创建一个JLable组件和一个JTextField对象,并将它们加入到容器中:
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:将文本与标签右对齐
JLabel.LEFT:将文本与标签左对齐
JLabel.CENTER:居中显文本
Demo:
1 package com.swingdemo.demo; 2 3 import java.awt.FlowLayout; 4 5 import javax.swing.JFrame; 6 import javax.swing.JLabel; 7 import javax.swing.JTextField; 8 import javax.swing.UIManager; 9 10 public class WebAddress extends JFrame { 11 12 private static final long serialVersionUID = 1L; 13 14 public WebAddress() { 15 16 super("Label and Text boxes"); 17 setLookAndFeel(); 18 setSize(400, 100); 19 JLabel pageLabel = new JLabel("Web page address: " , JLabel.RIGHT); 20 JTextField pageAddress = new JTextField(20); 21 //设置对象的文本 22 pageAddress.setText("http://java.sun.com"); 23 //JTextField pageAddress = new JTextField("http://java.sun.com", 20);//创建对象并赋默认值 24 FlowLayout flo = new FlowLayout(); 25 setLayout(flo); 26 add(pageLabel); 27 add(pageAddress); 28 setVisible(true); 29 30 //获取对象包含的文本 31 String countryChoice = pageAddress.getText(); 32 System.out.println(countryChoice); 33 34 } 35 36 private void setLookAndFeel() { 37 38 try { 39 UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 40 } catch (Exception e) { 41 e.printStackTrace(); 42 } 43 44 } 45 46 public static void main(String[] args) { 47 48 WebAddress wa = new WebAddress(); 49 50 } 51 52 }
标签:
原文地址:http://www.cnblogs.com/jinjiyese/p/4733405.html