Java使用者介面之標籤和文字框

來源:互聯網
上載者:User

標籤:

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 }
View Code

 

Java使用者介面之標籤和文字框

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.