Reputation: 261
I am trying to display roughly 7 or 8 multiple text fields and JLabels in a single frame. I am having trouble getting them all to show up. The only way I know to display the individual parts of the GUI is to create a JFrame, then create a container within that frame, then create multiple panels and add them to the container. Is there a better way to do this? One that isn't so restricting and allows me to display more than 4 frames? (I'm limited to only choosing NORTH, EAST, SOUTH, WEST and only one item will show up at those locations)
Here is my code:
Container content = n.getContentPane();
Container contentTwo = n.getContentPane();
JTextField JField = new JTextField(agentID);
JTextField stateField = new JTextField("Running");
JTextField transField = new JTextField(5);
JTextField opsField = new JTextField("0");
stateField.setEnabled(false);
transField.setEnabled(false);
JField.setEnabled(false);
opsField.setEnabled(false);
JLabel stateLabel = new JLabel("State:");
JLabel transLabel = new JLabel("Amount transferred:");
JLabel opsLabel = new JLabel("Operations Completed:");
JPanel fundsPanel = new JPanel(new BorderLayout());
JLabel agentLabel = new JLabel("Agent ID: ");
agentLabel.setDisplayedMnemonic(KeyEvent.VK_ENTER);
stateLabel.setLabelFor(stateField);
transLabel.setLabelFor(transField);
agentLabel.setLabelFor(JField);
opsLabel.setLabelFor(opsField);
fundsPanel.add(agentLabel, BorderLayout.WEST);
fundsPanel.add(JField, BorderLayout.CENTER);
content.add(fundsPanel, BorderLayout.NORTH);
JLabel accLabel = new JLabel("Amount in $: ");
JPanel accPanel = new JPanel(new BorderLayout());
JPanel accPanelTwo = new JPanel(new BorderLayout());
JPanel accPanelThree = new JPanel(new BorderLayout());
accLabel.setDisplayedMnemonic(KeyEvent.VK_ENTER);
JFormattedTextField accTextField = new JFormattedTextField(amount);
accTextField.setEnabled(false);
JLabel opLabel = new JLabel("Operations per second:");
opLabel.setDisplayedMnemonic(KeyEvent.VK_ENTER);
JTextField accTextFieldTwo = new JTextField(Double.toString(opsPerSec), 5);
accTextFieldTwo.setEnabled(false);
accLabel.setLabelFor(accTextField);
opLabel.setLabelFor(accTextFieldTwo);
accPanel.add(accLabel, BorderLayout.WEST);
accPanel.add(accTextField, BorderLayout.CENTER);
accPanelTwo.add(opLabel, BorderLayout.WEST);
accPanelTwo.add(accTextFieldTwo, BorderLayout.CENTER);
accPanelTwo.add(opsLabel, BorderLayout.EAST);
accPanelTwo.add(opsField, BorderLayout.NORTH);
accPanelThree.add(stateLabel, BorderLayout.WEST);
accPanelThree.add(stateField, BorderLayout.CENTER);
content.add(accPanel, BorderLayout.CENTER);
content.add(accPanelTwo, BorderLayout.EAST);
contentTwo.add(accPanelThree, BorderLayout.EAST);
JButton jButton1 = new JButton("Stop agent");
JButton jButton2 = new JButton("Dismiss");
jButton1.addActionListener(handler);
jButton2.addActionListener(handler);
buttonPanel.setLayout(new GridLayout(2, 3, 3, 3));
n.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
buttonPanel.add(jButton2, null);
buttonPanel.add(jButton1, null);
n.pack();
Upvotes: 1
Views: 102
Reputation: 51495
I just coded the first few fields of your panel using the GridBagLayout to show you how to use the layout.
You'll have to finish the coding to get your full form created.
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GridBagPanel {
protected static final Insets leftInsets = new Insets(10, 10, 0, 0);
protected static final Insets rightInsets = new Insets(10, 10, 0, 10);
protected JPanel mainPanel;
protected String agentID;
public GridBagPanel() {
createPartControl();
}
private void createPartControl() {
mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
int gridy = 0;
JLabel stateLabel = new JLabel("State:");
addComponent(mainPanel, stateLabel, 0, gridy, 1, 1, leftInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JTextField stateField = new JTextField("Running");
stateField.setEnabled(false);
stateLabel.setLabelFor(stateField);
addComponent(mainPanel, stateField, 1, gridy++, 1, 1, rightInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JLabel transLabel = new JLabel("Amount transferred:");
addComponent(mainPanel, transLabel, 0, gridy, 1, 1, leftInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JTextField transField = new JTextField(5);
transField.setEnabled(false);
transLabel.setLabelFor(transField);
addComponent(mainPanel, transField, 1, gridy++, 1, 1, rightInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JLabel opsLabel = new JLabel("Operations Completed:");
addComponent(mainPanel, opsLabel, 0, gridy, 1, 1, leftInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JTextField opsField = new JTextField("0");
opsField.setEnabled(false);
opsLabel.setLabelFor(opsField);
addComponent(mainPanel, opsField, 1, gridy++, 1, 1, rightInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JLabel agentLabel = new JLabel("Agent ID: ");
addComponent(mainPanel, opsLabel, 0, gridy, 1, 1, leftInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JTextField agentField = new JTextField(agentID);
agentField.setEnabled(false);
agentLabel.setLabelFor(agentField);
addComponent(mainPanel, agentField, 1, gridy++, 1, 1, rightInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
}
protected void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
}
Upvotes: 1