setLocation of Label

时间:2014-01-18 18:48:44

标签: java swing jlabel null-layout-manager

我让所有标签都正常工作,但userLabel [3]没有正确定位 无论我做什么,标签“颜色:”总是出现在框架上,x坐标为0,y坐标为框架的一半。

    JLabel[] userLabel = new JLabel[4];
    for(int p = 0; p < userLabel.length; p++){
        userLabel[p] = new JLabel();
        userLabel[p].setSize(100,50);
        frameSetUp.add(userLabel[p]);
    }
    userLabel[0].setText("Width of Frame:");
    userLabel[1].setText("Height of Frame:");
    userLabel[2].setText("# OF Balls:");
    userLabel[3].setText("Color:");

    userLabel[0].setLocation(10,35);
    userLabel[1].setLocation(10,85);
    userLabel[2].setLocation(10,135);
    userLabel[3].setLocation(0,0); //no matter what coordinates I change this too, it wont reposition

图像: [IMG] http://i41.tinypic.com/23jfo9l.png [/ IMG] http://i41.tinypic.com/23jfo9l.png

2 个答案:

答案 0 :(得分:5)

  1. 不要使用setLocation,setBounds,null布局或绝对定位。
  2. 而是使用布局管理器,包括可能嵌套的JPanel,每个都使用自己的布局管理器来实现令人满意的易维护GUI。
  3. 如需更多帮助,请展示您要实现的目标,实际实现的目标,并发布minimal working example代码,这些代码很小,可编译并运行,并向我们展示您的问题。

  4. 如,

    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class InputForm extends JPanel {
       private static final int COLUMNS = 10;
       private static final int GAP = 3;
       private static final Insets LABEL_INSETS = new Insets(GAP, GAP, GAP, 15);
       private static final Insets TEXTFIELD_INSETS = new Insets(GAP, GAP, GAP, GAP);
       private String[] labelTexts;
       private Map<String, JTextField> fieldMap = new HashMap<String, JTextField>();
    
       public InputForm(String[] labelTexts) {
          this.labelTexts = labelTexts;
          setLayout(new GridBagLayout());
          for (int i = 0; i < labelTexts.length; i++) {
             String text = labelTexts[i];
             JTextField field = new JTextField(COLUMNS);
             fieldMap.put(text, field);
    
             addLabel(text, i);
             addTextField(field, i);
          }
       }
    
       public String[] getLabelTexts() {
          return labelTexts;
       }
    
       private void addTextField(JTextField field, int row) {
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.gridwidth = 1;
          gbc.gridheight = 1;
          gbc.gridx = 1;
          gbc.gridy = row;
          gbc.anchor = GridBagConstraints.EAST;
          gbc.fill = GridBagConstraints.HORIZONTAL;
          gbc.insets = TEXTFIELD_INSETS;
          gbc.weightx = 1.0;
          gbc.weighty = 1.0;
          add(field, gbc);
       }
    
       private void addLabel(String text, int row) {
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.gridwidth = 1;
          gbc.gridheight = 1;
          gbc.gridx = 0;
          gbc.gridy = row;
          gbc.anchor = GridBagConstraints.WEST;
          gbc.fill = GridBagConstraints.BOTH;
          gbc.insets = LABEL_INSETS;
          gbc.weightx = 1.0;
          gbc.weighty = 1.0;
          add(new JLabel(text), gbc);
       }
    
       public String getFieldText(String key) {
          String text = "";
          JTextField field = fieldMap.get(key);
          if (field != null) {
             text = field.getText();
          }
          return text;
       }
    
       private static void createAndShowGui() {
          String[] labelTexts = new String[] { "Width of Frame:",
                "Height of Frame:", "# OF Balls:", "Color:" };
          InputForm inputForm = new InputForm(labelTexts);
    
          int result = JOptionPane.showConfirmDialog(null, inputForm, "Input Form",
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
          if (result == JOptionPane.OK_OPTION) {
             for (String text : labelTexts) {
                System.out.printf("%20s %s%n", text, inputForm.getFieldText(text));
             }
          }
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    将显示如下:

    enter image description here

    这段代码的美妙之处在于,如果你想添加另一个字段,比如一个行厚度字段,并想要添加它以使其倒数第二,那么代码所需的唯一更改就是更改这个:

      String[] labelTexts = new String[] { "Width of Frame:",
            "Height of Frame:", "# OF Balls:", "Color:" };
    

    到此:

      String[] labelTexts = new String[] { "Width of Frame:",
            "Height of Frame:", "# OF Balls:", "Line Thickness:", "Color:" };
    

    结果是:

    enter image description here

    无需计算如何更改Color标签或JTextField的位置,因为布局管理器会为您完成所有艰苦的工作。

答案 1 :(得分:-1)

最后得到答案尝试将JLabel数组的大小增加1并运行它将正常工作

import javax.swing.JFrame;
import javax.swing.JLabel;


public class Labelss{

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setBounds(50, 50, 700, 550);
        JLabel[] userLabel = new JLabel[6];
        for(int p = 0; p < userLabel.length; p++){
            userLabel[p] = new JLabel();
        }
        userLabel[0].setBounds(10,35,100,50);
        userLabel[1].setBounds(10,85,100,50);
        userLabel[2].setBounds(10,135,100,50);
        userLabel[3].setBounds(10,185,100,50);
        userLabel[4].setBounds(10,235,100,50);
        userLabel[0].setText("Width of Frame:");
        userLabel[1].setText("Height of Frame:");
        userLabel[2].setText("# OF Balls:");
        userLabel[3].setText("Color:");
        userLabel[4].setText("Stack overflow:");


        for(int p = 0; p < userLabel.length; p++){
            frame.add(userLabel[p]);
        }

        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }


}