setLocation of Label

夙愿已清 提交于 2019-11-27 07:27:01

问题


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

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


回答1:


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

Which will display like so:

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

to this:

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

Which results in:

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.




回答2:


Finally got the answer try increasing the size of the JLabel array by 1 and run it will work fine

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

    }


}


来源:https://stackoverflow.com/questions/21208396/setlocation-of-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!