JTextField未正确定位

时间:2015-04-10 12:38:09

标签: java swing

我制作了一个JFrame,其中有两个JPanel对齐,一个宽度为750像素,另一个宽度为250像素。到目前为止,这工作正常。接下来我想在右侧的面板中添加一些JTextField,因此我将它们包含在构造函数中。现在,当我尝试运行代码时,文本字段位于另一个面板的中心,并且在其中输入内容之前不会完全展开(它们看起来像薄白条)。我目前没有设置任何布局,因为我只是想最初绘制文本字段并稍后安排它们,只是我希望它们在正确的面板中。为什么这不起作用?

主要课程:

package forces;
import java.awt.BorderLayout;

import javax.swing.*;

public class PrimaryWindow extends JFrame{
    private static final long serialVersionUID = 1L;
    JFrame frame;
    ForcePanel panel;
    DataPanel dpanel;

    public PrimaryWindow()
    {
        frame = new JFrame("Forces");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setSize(1000,800);
        frame.setResizable(false);
        panel = new ForcePanel();
        frame.add(panel);
        dpanel = new DataPanel();
        frame.add(dpanel);
        frame.setVisible(true);
    }
    public static void main(String args[])
    {
        new PrimaryWindow();
    }

}

面板1(这是750像素面板):

package forces;

import javax.swing.*;
import java.awt.*;

public class ForcePanel extends JPanel {
    boolean simulate = false;
    private static final long serialVersionUID = 1L;
    public ForcePanel()
    {
        this.setSize(750,800);
        this.setBackground(Color.BLACK);
    }

}

第2小组(文本字段应为):

package forces;

import javax.swing.*;
import java.awt.Color;
import java.awt.BorderLayout;

public class DataPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    JTextField slopeangle;
    JTextField g;
    JTextField objectmass;


    public DataPanel()
    {
        this.setLayout(new BorderLayout());
        this.setSize(250,800);
        this.setBackground(Color.GRAY);
        slopeangle = new JTextField(20);
        g = new JTextField(20);
        objectmass = new JTextField(20);
        this.add(slopeangle);
        this.add(g);
        this.add(objectmass);
    }

}

1 个答案:

答案 0 :(得分:2)

如前所述,您将需要使用布局管理器来最好地在GUI中排列组件。我会覆盖图形组件的getPreferredSize()方法,但让其他一切都落到它自己固有的首选大小。 GridBagLayout可用于轻松排列JTextField的网格,而BorderLayout非常适合GUI的整体排列。例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
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 PrimaryPanel extends JPanel {
   private ForcePanel forcePanel = new ForcePanel();
   private DataPanel dataPanel = new DataPanel();

   public PrimaryPanel() {
      setLayout(new BorderLayout());
      add(forcePanel, BorderLayout.CENTER);
      add(dataPanel, BorderLayout.LINE_END);
   }

   private static void createAndShowGUI() {
      PrimaryPanel paintEg = new PrimaryPanel();

      JFrame frame = new JFrame("PrimaryPanel");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(paintEg);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }
}

@SuppressWarnings("serial")
class ForcePanel extends JPanel {
   private static final int PREF_W = 750;
   private static final int PREF_H = 800;

   public ForcePanel() {
      setBackground(Color.black);
   }

   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);   }
}

@SuppressWarnings("serial")
class DataPanel extends JPanel {
   private static final int TEXT_FIELD_COLUMNS = 10;
   private static final int GAP = 5;
   private static final Insets RIGHT_GAP_INSETS = new Insets(GAP, GAP, GAP, 2 * GAP);
   private static final Insets BALANCED_INSETS = new Insets(GAP, GAP, GAP, GAP);
   public static final String[] FIELD_LABELS = {
      "Slope Angle", "G", "Object Mass",
      "Time Steps", "Max Time", "Fubarlicious!"
   };
   private Map<String, JTextField> labelFieldMap = new HashMap<>();

   public DataPanel() {
      JPanel labelFieldPanel = new JPanel(new GridBagLayout());
      int row = 0;

      for (String fieldLabelLText : FIELD_LABELS) {
         JLabel fieldLabel = new JLabel(fieldLabelLText);
         JTextField textField = new JTextField(TEXT_FIELD_COLUMNS);
         labelFieldPanel.add(fieldLabel, getGbc(row, 0));
         labelFieldPanel.add(textField, getGbc(row, 1));
         labelFieldMap.put(fieldLabelLText, textField);
         row++;
      }

      setLayout(new BorderLayout(GAP, GAP));
      add(labelFieldPanel, BorderLayout.PAGE_START);

   }

   public static GridBagConstraints getGbc(int row, int column) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = column;
      gbc.gridy = row;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      if (column == 0) {
         gbc.anchor = GridBagConstraints.LINE_START;
         gbc.fill = GridBagConstraints.BOTH;
         gbc.insets = RIGHT_GAP_INSETS;
      } else {
         gbc.anchor = GridBagConstraints.LINE_END;
         gbc.fill = GridBagConstraints.HORIZONTAL;
         gbc.insets = BALANCED_INSETS;
      }

      return gbc;
   }
}

看起来像是:

enter image description here