2014-02-09 64 views
2

嗨我正在開發swing應用程序,但我面臨着一個問題。JPanel&組件自動更改位置

當我第一次運行應用程序JPanel被定位在適當的位置 我決定在裏面設置組件。但是,當

我再次最小&最大化框架窗口jpanel自動更改 它的位置問題發生。

下面的圖像顯示了差異

enter image description here

enter image description here

正如我們在第二圖像部件看到改變它的位置 自動。

爲此,我寫下面的代碼,

jpanel_addPurchase = new JPanel(); 
jpanel_addPurchase.setLayout(null); 
jpanel_addPurchase.setBounds(400, 0, 500, 500); 
jpanel_addPurchase.setBackground(Color.white); 
JLabel lbl_title = new JLabel("Purchase Form"); 
lbl_title.setBounds(90, 20, 100, 100); 
jpanel_addPurchase.add(lbl_title); 

,並將此面板中使用框架,

setContentPane(getJPanel()); 

在哪裏,我錯了?

+1

考慮張貼[最小的,完整的,經過測試和讀示例](http://stackoverflow.com/help/mcve) –

+0

Java的圖形用戶界面可能需要對一些工作平臺,不同的屏幕分辨率和使用不同的PLAF。因此,它們不利於組件的準確放置。爲了組織強大的圖形用戶界面,請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[空格]的佈局填充和邊框(http: //stackoverflow.com/q/17874717/418556)。 –

回答

10

我認爲原始的(非破損的)佈局看起來很古怪,會讓最終用戶很難追隨行和標籤/字段。我建議使用GroupLayout來對齊標籤,並將包含值的字段左對齊。就像這樣:

enter image description here

import java.awt.*; 
import java.util.HashMap; 
import java.util.Map; 
import javax.swing.*; 

class TwoColumnLayoutWithHeader { 

    /** 
    * Provides a JPanel with two columns (labels & fields) laid out using 
    * GroupLayout. The arrays must be of equal size. 
    * 
    * Typical fields would be single line textual/input components such as 
    * JTextField, JPasswordField, JFormattedTextField, JSpinner, JComboBox, 
    * JCheckBox.. & the multi-line components wrapped in a JScrollPane - 
    * JTextArea or (at a stretch) JList or JTable. 
    * 
    * @param labels The first column contains labels. 
    * @param fields The last column contains fields. 
    * @param addMnemonics Add mnemonic by next available letter in label text. 
    * @return JComponent A JPanel with two columns of the components provided. 
    */ 
    public static JComponent getTwoColumnLayout(
      JLabel[] labels, 
      JComponent[] fields, 
      boolean addMnemonics) { 
     if (labels.length != fields.length) { 
      String s = labels.length + " labels supplied for " 
        + fields.length + " fields!"; 
      throw new IllegalArgumentException(s); 
     } 
     JComponent panel = new JPanel(); 
     GroupLayout layout = new GroupLayout(panel); 
     panel.setLayout(layout); 
     // Turn on automatically adding gaps between components 
     layout.setAutoCreateGaps(true); 
     // Create a sequential group for the horizontal axis. 
     GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup(); 
     GroupLayout.Group yLabelGroup = layout.createParallelGroup(GroupLayout.Alignment.TRAILING); 
     hGroup.addGroup(yLabelGroup); 
     GroupLayout.Group yFieldGroup = layout.createParallelGroup(); 
     hGroup.addGroup(yFieldGroup); 
     layout.setHorizontalGroup(hGroup); 
     // Create a sequential group for the vertical axis. 
     GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup(); 
     layout.setVerticalGroup(vGroup); 

     int p = GroupLayout.PREFERRED_SIZE; 
     // add the components to the groups 
     for (JLabel label : labels) { 
      yLabelGroup.addComponent(label); 
     } 
     for (Component field : fields) { 
      yFieldGroup.addComponent(field, p, p, p); 
     } 
     for (int ii = 0; ii < labels.length; ii++) { 
      vGroup.addGroup(layout.createParallelGroup(). 
        addComponent(labels[ii]). 
        addComponent(fields[ii], p, p, p)); 
     } 

     if (addMnemonics) { 
      addMnemonics(labels, fields); 
     } 

     return panel; 
    } 

    private final static void addMnemonics(
      JLabel[] labels, 
      JComponent[] fields) { 
     Map<Character, Object> m = new HashMap<Character, Object>(); 
     for (int ii = 0; ii < labels.length; ii++) { 
      labels[ii].setLabelFor(fields[ii]); 
      String lwr = labels[ii].getText().toLowerCase(); 
      for (int jj = 0; jj < lwr.length(); jj++) { 
       char ch = lwr.charAt(jj); 
       if (m.get(ch) == null && Character.isLetterOrDigit(ch)) { 
        m.put(ch, ch); 
        labels[ii].setDisplayedMnemonic(ch); 
        break; 
       } 
      } 
     } 
    } 

    /** 
    * Provides a JPanel with two columns (labels & fields) laid out using 
    * GroupLayout. The arrays must be of equal size. 
    * 
    * @param labelStrings Strings that will be used for labels. 
    * @param fields The corresponding fields. 
    * @return JComponent A JPanel with two columns of the components provided. 
    */ 
    public static JComponent getTwoColumnLayout(
      String[] labelStrings, 
      JComponent[] fields) { 
     JLabel[] labels = new JLabel[labelStrings.length]; 
     for (int ii = 0; ii < labels.length; ii++) { 
      labels[ii] = new JLabel(labelStrings[ii]); 
     } 
     return getTwoColumnLayout(labels, fields); 
    } 

    /** 
    * Provides a JPanel with two columns (labels & fields) laid out using 
    * GroupLayout. The arrays must be of equal size. 
    * 
    * @param labels The first column contains labels. 
    * @param fields The last column contains fields. 
    * @return JComponent A JPanel with two columns of the components provided. 
    */ 
    public static JComponent getTwoColumnLayout(
      JLabel[] labels, 
      JComponent[] fields) { 
     return getTwoColumnLayout(labels, fields, true); 
    } 

    public static String getProperty(String name) { 
     return name + ": \t" 
       + System.getProperty(name) 
       + System.getProperty("line.separator"); 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 

      @Override 
      public void run() { 
       JComponent[] components = { 
        new JTextField(15), 
        new JTextField(10), 
        new JTextField(8), 
        new JSpinner(new SpinnerNumberModel(1,0,10,1)), 
        new JSpinner(new SpinnerNumberModel(9.95,0d,100d,.01)), 
        new JSpinner(new SpinnerNumberModel(9.95,0d,1000d,.01)), 
        new JSpinner(new SpinnerNumberModel(9.95,0d,100d,.01)), 
        new JSpinner(new SpinnerNumberModel(9.95,0d,1000d,.01)), 
        new JSpinner(new SpinnerNumberModel(9.95,0d,100d,.01)), 
        new JSpinner(new SpinnerNumberModel(9.95,0d,1000d,.01)) 
       }; 

       String[] labels = { 
        "Product Name:", 
        "Product Unit Name:", 
        "Purchase Date:", 
        "Quantity:", 
        "Price Per Unit:", 
        "Total Price:", 
        "Discount:", 
        "Total:", 
        "VAT:", 
        "Grand Total:" 
       }; 

       JComponent labelsAndFields = getTwoColumnLayout(labels,components); 
       JComponent orderForm = new JPanel(new BorderLayout(5,5)); 
       orderForm.add(new JLabel("Purchase Form", SwingConstants.CENTER), 
        BorderLayout.PAGE_START); 
       orderForm.add(labelsAndFields, BorderLayout.CENTER); 

       JOptionPane.showMessageDialog(null, orderForm); 
      } 
     }; 
     // Swing GUIs should be created and updated on the EDT 
     // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html 
     SwingUtilities.invokeLater(r); 
    } 
} 
+1

偉大的工作對我來說很好....我不希望這對我的問題有很大的幫助......你真的做得很好...... – Aniket

+0

不客氣。 :) –