2012-06-22 53 views
3

我創建使用的GridBagLayout一個簡單的GUI,幾乎一切順利吧,但這個細節:有兩列,左邊的一個標籤和右一爲其他部件一樣的JTextArea,我想第一列與右邊對齊。對齊與GridBagLayout的

所以我試圖用錨屬性附加傷害設定爲GridBagConstraints.NORTHEAST或。東,和他們沒有工作。

我的代碼如下:

private void createDataPanel() { 
     data = new JPanel(); 
     data.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 
     data.setBorder(new TitledBorder("NOTAM proposal parameters")); 

     c.gridx = 0; 
     c.gridy = 0; 
     c.insets = new Insets(5, 10, 5, 10); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     data.add(new JLabel("Start time (DD-MM-YYYY HH:MM:SS):"), c); 

     c.gridx++; 
     long today = System.currentTimeMillis(); 
     startTimePicker = new JXDatePicker(new Date(today)); 
     startTimePicker.setFormats(dateFormatter); 
     data.add(startTimePicker); 

     c.gridx = 0; 
     c.gridy++; 
     c.anchor = GridBagConstraints.EAST; 
     data.add(new JLabel("End time (DD-MM-YYYY HH:MM:SS):"), c); 

     c.gridx++; 
     c.anchor = GridBagConstraints.CENTER; 
     long tomorrow = today + 86400000; 
     endTimePicker = new JXDatePicker(new Date(tomorrow)); 
     endTimePicker.setFormats(dateFormatter); 
     data.add(endTimePicker, c); 

     c.gridx = 0; 
     c.gridy++; 
     c.anchor = GridBagConstraints.NORTHEAST; 
     JPanel itemEPanel = new JPanel(); 
     itemEPanel.setLayout(new BoxLayout(itemEPanel, BoxLayout.Y_AXIS)); 
     itemEPanel.add(new JLabel("Item E:")); 
     remainingChars = new JLabel(Integer.toString(availableChars) + " characters remaining"); 
     itemEPanel.add(remainingChars); 
     data.add(itemEPanel, c); 

     c.gridx++; 
     c.anchor = GridBagConstraints.CENTER; 
     itemEText = new JTextArea(8, 1); 
     JScrollPane itemEScroll = new JScrollPane(itemEText); 
     itemEText.setLineWrap(true); 
     itemEText.setWrapStyleWord(true); 
     itemEText.setBackground(Color.WHITE); 
     itemEText.getDocument().addDocumentListener(new DocumentListener() { 

      @Override 
      public void removeUpdate(DocumentEvent e) { 
       updateRemainingChars(e.getDocument()); 
      } 

      @Override 
      public void insertUpdate(DocumentEvent e) { 
       updateRemainingChars(e.getDocument()); 
      } 

      @Override 
      public void changedUpdate(DocumentEvent e) { 
       updateRemainingChars(e.getDocument()); 
      } 

      public void updateRemainingChars(Document doc) { 
       availableChars = maxItemECharacters - doc.getLength(); 
       if(availableChars>=0){ 
        remainingChars.setText(Integer.toString(availableChars) + " characters remaining"); 
        remainingChars.setForeground(Color.BLACK); 
        if(!okButton.isEnabled()) okButton.setEnabled(true); 
       } 
       else{ 
        remainingChars.setText("Message too long: " + Integer.toString(availableChars)); 
        remainingChars.setForeground(Color.RED); 
        if(okButton.isEnabled()) okButton.setEnabled(false); 
       } 
      } 
     }); 
     data.add(itemEScroll, c); 

     c.gridx = 0; 
     c.gridy++; 
     JPanel itemDPanel = new JPanel(new FlowLayout()); 
     applicable = new JCheckBox("Applicable"); 
     applicable.setSelected(true); 
     applicable.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       itemDText.setEnabled(applicable.isSelected()); 
      } 
     }); 
     itemDPanel.add(applicable); 
     itemDPanel.add(new JLabel("Item D:")); 
     data.add(itemDPanel, c); 

     c.gridx++; 
     itemDText = new JTextField(); 
     data.add(itemDText, c); 
    } 

不過,目前看起來是這樣的: enter image description here

它該如何調整到正確的呢?

回答

6

設置GridBagConstraints.NORTHEAST或。東但是使用填充= NONE

+0

aaaach顯然+1 – mKorbel

1
JLabel.setHorizontalTextPosition(JLabel.RIGHT); 
+0

現在沒有工作或者 –

1

的問題是,你使用這個約束:

c.fill = GridBagConstraints.HORIZONTAL; 

因此所有組件均橫向拉長,留下alignement而不是GridBagLayout。

5

你的意思是你希望你的JLabel的文本是在右側。如果是嘗試初始化JLabel就像這個

JLabel someLabel = new JLabel("My Text will align towards RIGHT", JLabel.RIGHT);