有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java设置标签的位置

我所有的标签都工作正常,但是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) 个答案

  1. # 1 楼答案

    1. 不要使用setLocation、setBounds、null布局或绝对定位
    2. 而是使用布局管理器,包括嵌套的JPanel,每个都使用自己的布局管理器来实现易于维护的GUI
    3. 要获得更多帮助,请展示一幅您试图实现的、您实际实现的图片,并发布一个minimal working example小代码,它可以编译和运行,并向我们展示您的问题

    例如

    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

    无需计算如何更改颜色标签或JTextField的位置,因为布局管理器会为您完成所有繁重的工作

  2. # 2 楼答案

    最终得到了答案尝试将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);
    
        }
    
    
    }