问题
I am trying to create 4 JLabel
components 4 JTextfield
components for my Swing based application. I am adding these lables and text fields in one of the many panels of the application.
The class for this panel is as follows:
public class CustomerInfoPanel extends JPanel implements ActionListener {
private Car[] carList;
private CarSalesSystem carSystem;
private int currentIndex = 0;
private JLabel headingLabel = new JLabel("Enter your details and let us find a car for you");
private JLabel ageLabel = new JLabel("Age");
private JLabel genderLabel = new JLabel("Gender");
private JLabel incomeLabel = new JLabel("Income");
private JLabel interestLabel = new JLabel("Age");
private JButton searchButton = new JButton("Search");
private JButton resetButton = new JButton("Reset");
private JButton previousButton = new JButton("Previous");
private JButton nextButton = new JButton("Next");
//text fields
private JTextField ageTextField = new JTextField();
private JTextField genderTextField = new JTextField();
private JTextField incomeTextField = new JTextField();
private JTextField interestTextField = new JTextField();
private JPanel topPanel = new JPanel();
private JPanel agePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
private JPanel searchButtonsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
private JPanel navigateButtonsPanel = new JPanel();
private CarDetailsComponents carComponents = new CarDetailsComponents();
/**
* @param carSys links to a CarSalesSystem object
* @param dest where the panel will be displayed on the main frame
*/
public CustomerInfoPanel(CarSalesSystem carSys)
{
carSystem = carSys;
Insets currentInsets;
GridBagConstraints gridBagConstraints;
setLayout(new BorderLayout(0, 20));
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
previousButton.addActionListener(this);
nextButton.addActionListener(this);
resetButton.addActionListener(this);
searchButton.addActionListener(this);
String currentFont = ageLabel.getFont().getName();
currentInsets = new Insets(0, 10, 0, 30);
ageLabel.setFont(new Font(currentFont, Font.BOLD, 12));
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.WEST;
gridBagConstraints.insets = currentInsets;
agePanel.add(ageLabel, gridBagConstraints);
genderLabel.setFont(new Font(currentFont, Font.BOLD, 12));
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = GridBagConstraints.WEST;
gridBagConstraints.insets = currentInsets;
agePanel.add(genderLabel, gridBagConstraints);
incomeLabel.setFont(new Font(currentFont, Font.BOLD, 12));
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.anchor = GridBagConstraints.WEST;
gridBagConstraints.insets = currentInsets;
agePanel.add(incomeLabel, gridBagConstraints);
interestLabel.setFont(new Font(currentFont, Font.BOLD, 12));
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.anchor = GridBagConstraints.WEST;
gridBagConstraints.insets = currentInsets;
agePanel.add(interestLabel, gridBagConstraints);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridwidth = GridBagConstraints.RELATIVE;
gridBagConstraints.anchor = GridBagConstraints.WEST;
gridBagConstraints.weightx = 1.0;
agePanel.add(ageTextField, gridBagConstraints);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.gridwidth = GridBagConstraints.RELATIVE;
gridBagConstraints.anchor = GridBagConstraints.WEST;
gridBagConstraints.weightx = 1.0;
agePanel.add(genderTextField, gridBagConstraints);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 2;
gridBagConstraints.gridwidth = GridBagConstraints.RELATIVE;
gridBagConstraints.anchor = GridBagConstraints.WEST;
gridBagConstraints.weightx = 1.0;
agePanel.add(incomeTextField, gridBagConstraints);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 3;
gridBagConstraints.gridwidth = GridBagConstraints.RELATIVE;
gridBagConstraints.anchor = GridBagConstraints.WEST;
gridBagConstraints.weightx = 1.0;
agePanel.add(interestTextField, gridBagConstraints);
searchButtonsPanel.add(searchButton);
searchButtonsPanel.add(resetButton);
navigateButtonsPanel.add(previousButton);
navigateButtonsPanel.add(nextButton);
agePanel.setBorder(new javax.swing.border.EmptyBorder(new Insets(0, 5, 0, 0)));
searchButtonsPanel.setBorder(new javax.swing.border.EmptyBorder(new Insets(0, 5, 0, 0)));
headingLabel.setAlignmentX(0.5f);
topPanel.add(Box.createVerticalStrut(10));
topPanel.add(headingLabel);
topPanel.add(Box.createVerticalStrut(10));
topPanel.add(agePanel);
topPanel.add(searchButtonsPanel);
topPanel.add(Box.createVerticalStrut(15));
carComponents.add(navigateButtonsPanel, "Center");
carComponents.setVisible(false);
add(topPanel, "North");
add(carComponents, "Center");
}
But when I execute the above code, I end up getting the following
As you can clearly see from the image that the text field and labels are not positioned properly. What am I doing wrong here?
回答1:
The agePanel
is using a FlowLayout
, but you're supplying GridBagConstraints
to it when you're adding your components. You need to make up you mind over which layout manager you want to use.
Start by changing the layout manager for agePanel
to a GridBagLayout
private JPanel agePanel = new JPanel(new GridBagLayout());
Then supply a column hint the JTextField
private JTextField ageTextField = new JTextField(10);
private JTextField genderTextField = new JTextField(10);
private JTextField incomeTextField = new JTextField(10);
private JTextField interestTextField = new JTextField(10);
Take a closer look at Laying Out Components Within a Container and focus on the differences between the different layout managers
回答2:
If you want to develop customized user interface you have to use below method and bind controls freely where ever you want setLayout(null) and use setBounds(x, y, width, height) method to bind controls without using any Layout Manager.
来源:https://stackoverflow.com/questions/31448217/setting-jlabels-in-rows-and-placing-the-equivalent-jtextfield-right-next-to-it