2016-12-24 89 views
-2

我需要一個標籤上的文本字段,但是當我運行此代碼時屏幕上沒有文本字段。我如何解決它。我如何一起使用JTextField和JLabel?

JFrame jf = new JFrame() ; 

JPanel panel = new JPanel() ; 

JLabel label = new JLabel() ; 

JTextField tField = new JTextField("asd" , 10) ; 

label.add(tField) ; 
panel.add(label) ; 

jf.setSize(500,400) ; 
jf.add(panel) ; 
jf.setVisible(true) ; 

回答

3

JLabel的沒有默認佈局管理器,等等,而你的JTextField 添加托特他的JLabel,它沒有顯示,因爲標籤不知道如何表現出來。

可以有幾種方法來解決這個問題取決於你想要達到的目的:

  • 給選擇JLabel佈局管理器,然後添加JTextField的它:但隨後的JTextField 涵蓋 JLabel,它的文本(如果有的話)和它的圖標(如果有的話)不好。
  • 創建一個JPanel來保存兩者,並給它一個合適的佈局管理器:可能是一個不錯的選擇。
  • 使用佈局管理器將它們添加到相同的JPanel中,可以輕鬆地將它們放置在關聯中:另一個不錯的選擇。 GridBagLayout適用於此。

不要忘了還叫JLabel的setLabelFor(...)方法將其緊密地與JTextField中,爲每JLabel Tutorial

例如關聯:

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.awt.event.KeyEvent; 
import java.util.HashMap; 
import java.util.Map; 
import javax.swing.*; 

public class GridBagEg { 
    private static void createAndShowGui() { 
     PlayerEditorPanel playerEditorPane = new PlayerEditorPanel(); 

     int result = JOptionPane.showConfirmDialog(null, playerEditorPane, "Edit Player", 
       JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); 
     if (result == JOptionPane.OK_OPTION) { 
      // TODO: do something with info 

      for (PlayerEditorPanel.FieldTitle fieldTitle : PlayerEditorPanel.FieldTitle.values()) { 
       System.out.printf("%10s: %s%n", fieldTitle.getTitle(), 
         playerEditorPane.getFieldText(fieldTitle)); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGui(); 
      } 
     }); 
    } 
} 

@SuppressWarnings("serial") 
class PlayerEditorPanel extends JPanel { 
    enum FieldTitle { 
     NAME("Name", KeyEvent.VK_N), SPEED("Speed", KeyEvent.VK_P), STRENGTH("Strength", KeyEvent.VK_T); 
     private String title; 
     private int mnemonic; 

     private FieldTitle(String title, int mnemonic) { 
      this.title = title; 
      this.mnemonic = mnemonic; 
     } 

     public String getTitle() { 
      return title; 
     } 

     public int getMnemonic() { 
      return mnemonic; 
     } 
    }; 

    private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5); 
    private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0); 
    private Map<FieldTitle, JTextField> fieldMap = new HashMap<FieldTitle, JTextField>(); 

    public PlayerEditorPanel() { 
     setLayout(new GridBagLayout()); 
     setBorder(BorderFactory.createCompoundBorder(
       BorderFactory.createTitledBorder("Player Editor"), 
       BorderFactory.createEmptyBorder(5, 5, 5, 5))); 
     GridBagConstraints gbc; 
     for (int i = 0; i < FieldTitle.values().length; i++) { 
      FieldTitle fieldTitle = FieldTitle.values()[i]; 
      JLabel label = new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT); 
      JTextField textField = new JTextField(10); 
      label.setDisplayedMnemonic(fieldTitle.getMnemonic()); 
      label.setLabelFor(textField); 
      gbc = createGbc(0, i); 
      add(label, gbc); 
      gbc = createGbc(1, i); 
      add(textField, gbc); 

      fieldMap.put(fieldTitle, textField); 
     } 
    } 

    private GridBagConstraints createGbc(int x, int y) { 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = x; 
     gbc.gridy = y; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 

     gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST; 
     gbc.fill = (x == 0) ? GridBagConstraints.BOTH : GridBagConstraints.HORIZONTAL; 

     gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS; 
     gbc.weightx = (x == 0) ? 0.1 : 1.0; 
     gbc.weighty = 1.0; 
     return gbc; 
    } 

    public String getFieldText(FieldTitle fieldTitle) { 
     return fieldMap.get(fieldTitle).getText(); 
    } 

} 

該款顯示器作爲

enter image description here

請注意,JLabels h AVE強調的助記符字符,字符,當按下Alt鍵組合將帶來的是一個JLabel經,setLabelFor(...)掛焦點JTextField的,由該代碼導致的:

FieldTitle fieldTitle = FieldTitle.values()[i]; // an enum that holds label texts 
JLabel label = new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT); // create JLabel 
JTextField textField = new JTextField(10); // create JTextField 

// set the label's mnemonic -- brings focus to the linked text field 
label.setDisplayedMnemonic(fieldTitle.getMnemonic()); 

// *** here we *link* the JLabel with the JTextField 
label.setLabelFor(textField);