Reputation: 9040
I've made a JFrame with two JPanels aligned within it, one with a width of 750 pixels and the other with a width of 250 pixels. So far that's worked fine. Next I wanted to add some JTextFields to the panel on the right, so I included them in the constructor. Now when I try to run the code the textfields are positioned in the centre of the other panel, and aren't fully expanded until something is typed in them (they look like thin white strips). I haven't set any layouts at the moment as I just want to draw the textfields initially and arrange them later, only I do want them to be in the correct panel. Why isn't this working?
Main class:
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();
}
}
Panel 1 (this is the 750 pixel panel):
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);
}
}
Panel 2 (where the text fields should be):
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);
}
}
Upvotes: 0
Views: 76
Reputation: 285415
As has been mentioned, you will want to use layout managers to best arrange your components in your GUI. I would override the getPreferredSize()
method of the graphics component but let everything else fall to its own innate preferred size. GridBagLayout could be used to easily arrange a grid of JTextFields, and BorderLayout is great for overall arrangements of the GUI. For instance:
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;
}
}
Which would look like:
Upvotes: 2