标签的setLocation [英] setLocation of Label

查看:105
本文介绍了标签的setLocation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have all of the labels working correctly but the userLabel[3] is not positioning properly No matter what I do, the label "Color:" always shows up on the frame with a x-coordinate of 0 and a y-coordinate that is half way down the frame.

    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

推荐答案


  1. 不要使用setLocation,setBounds,null布局或绝对定位。

  2. 而是使用布局管理器,包括可能嵌套的JPanel,每个都使用自己的布局管理器来实现令人满意的易维护GUI。

  3. 如需更多帮助,请展示您想要实现的目标,实际目标,并发布最小的工作示例,代码很小,编译和运行,并向我们展示您的问题。

  1. Don't use setLocation, setBounds, null layouts or absolute positioning.
  2. Instead use the layout managers including perhaps nested JPanels, each using its own layout manager to achieve pleasing easy to maintain GUI's.
  3. For more help, show a picture of what you're trying to achieve, what you actually are achieving, and post a minimal working example, code that is small, that compiles and runs, and shows us your problem.






例如,


e.g.,

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();
         }
      });
   }
}

这将显示如下:

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

The beauty of this code, is if you wish to add another field, say a line thickness field, and want to add it so that it is second to last, then the only change needed to the code would be to change this:

  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:" };

导致:

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

No need to have to calculate how to change the Color label or JTextField's locations as the layout manager does all the hard work for you.

这篇关于标签的setLocation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆