我想爲一個人的出生日期添加3個組合框:一個爲月份,然後是一天,然後是一年。當我添加所有3個組合框時,它會改變JTextFields
的寬度。任何幫助如何解決這個問題將不勝感激。JComboBox在GridBagLayout中插入後更改JTextField寬度
還有沒有更好的方法來添加JComboBox
的月份,而不是一個接一個地插入?
下面是代碼:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class Home extends JFrame {
private JButton btnClear, btnNext;
private JTextField txtName, txtAddress, txtCity, txtState,
txtZipCode, txtPhoneNumber, txtEmail;
private JComboBox txtDate, txtYear, txtMonth;
private JLabel labelName, labelAddress, labelCity, labelState,
labelZipCode, labelPhoneNumber, labelEmail, labelDOB;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Home().setVisible(true);
}
});
}
public Home() {
createView();
setTitle("Job Hiring Systems");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 500);
setMinimumSize(new Dimension(600, 500));
//pack();
setLocationRelativeTo(null);
setResizable(false);
}
private void createView() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
//panel.setBorder(new EmptyBorder(25,10,25,10));
getContentPane().add(panel);
/*
* North Panel
*/
JPanel northPanel = new JPanel();
northPanel.setBackground(Color.GRAY);
panel.add(northPanel, BorderLayout.NORTH);
JLabel labelWelcomeMessage = new JLabel("Welcome to the Job Hiring Systems");
labelWelcomeMessage.setFont(new Font("Serif", Font.BOLD, 25));
northPanel.add(labelWelcomeMessage, BorderLayout.CENTER);
/*
* End of North Panel
*/
/*
* Center Panel
*/
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.yellow);
centerPanel.setSize(300, 300);
GridBagLayout layout = new GridBagLayout();
centerPanel.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
labelName = new JLabel("Enter your name: ");
labelName.setFont(new Font("Serif", Font.PLAIN, 15));
centerPanel.add(labelName, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
txtName = new JTextField(25);
txtName.setHorizontalAlignment(JTextField.CENTER);
txtName.setHorizontalAlignment(SwingConstants.LEFT);
centerPanel.add(txtName, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 1;
labelAddress = new JLabel("Enter your address: ");
labelAddress.setFont(new Font("Serif", Font.PLAIN, 15));
centerPanel.add(labelAddress, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
txtAddress = new JTextField(25);
txtAddress.setHorizontalAlignment(JTextField.CENTER);
txtAddress.setHorizontalAlignment(SwingConstants.LEFT);
centerPanel.add(txtAddress, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 2;
labelCity = new JLabel("Enter your city: ");
labelCity.setFont(new Font("Serif", Font.PLAIN, 15));
centerPanel.add(labelCity, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
txtCity = new JTextField(25);
txtCity.setHorizontalAlignment(JTextField.CENTER);
txtCity.setHorizontalAlignment(SwingConstants.LEFT);
centerPanel.add(txtCity, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 3;
labelState = new JLabel("Enter your State: ");
labelState.setFont(new Font("Serif", Font.PLAIN, 15));
centerPanel.add(labelState, gbc);
gbc.gridx = 1;
gbc.gridy = 3;
txtState = new JTextField(25);
txtState.setHorizontalAlignment(JTextField.CENTER);
txtState.setHorizontalAlignment(SwingConstants.LEFT);
centerPanel.add(txtState, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 4;
labelZipCode = new JLabel("Enter your Zip Code: ");
labelZipCode.setFont(new Font("Serif", Font.PLAIN, 15));
centerPanel.add(labelZipCode, gbc);
gbc.gridx = 1;
gbc.gridy = 4;
txtZipCode = new JTextField(25);
txtZipCode.setHorizontalAlignment(JTextField.CENTER);
txtZipCode.setHorizontalAlignment(SwingConstants.LEFT);
centerPanel.add(txtZipCode, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 5;
labelPhoneNumber = new JLabel("Enter your Phone Number: ");
labelPhoneNumber.setFont(new Font("Serif", Font.PLAIN, 15));
centerPanel.add(labelPhoneNumber, gbc);
gbc.gridx = 1;
gbc.gridy = 5;
txtPhoneNumber = new JTextField(25);
txtPhoneNumber.setHorizontalAlignment(JTextField.CENTER);
txtPhoneNumber.setHorizontalAlignment(SwingConstants.LEFT);
centerPanel.add(txtPhoneNumber, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 6;
labelEmail = new JLabel("Enter your Email: ");
labelEmail.setFont(new Font("Serif", Font.PLAIN, 15));
centerPanel.add(labelEmail, gbc);
gbc.gridx = 1;
gbc.gridy = 6;
txtEmail = new JTextField(25);
txtEmail.setHorizontalAlignment(JTextField.CENTER);
txtEmail.setHorizontalAlignment(SwingConstants.LEFT);
centerPanel.add(txtEmail, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 7;
labelDOB = new JLabel("Enter your Date of Birth: ");
labelDOB.setFont(new Font("Serif", Font.PLAIN, 15));
centerPanel.add(labelDOB, gbc);
gbc.gridx = 1;
txtMonth = new JComboBox();
txtMonth.addItem("January");
txtMonth.addItem("February");
txtMonth.addItem("March");
txtMonth.addItem("April");
txtMonth.addItem("May");
txtMonth.addItem("June");
txtMonth.addItem("July");
txtMonth.addItem("August");
txtMonth.addItem("September");
txtMonth.addItem("October");
txtMonth.addItem("November");
txtMonth.addItem("December");
centerPanel.add(txtMonth, gbc);
gbc.gridx = 2;
txtDate = new JComboBox();
for (int i = 0; i < 31; i++) {
txtDate.addItem(i);
}
centerPanel.add(txtDate, gbc);
gbc.gridx = 3;
txtYear = new JComboBox();
for (int i = 1940; i < 2017; i++) {
txtYear.addItem(i);
}
centerPanel.add(txtYear, gbc);
panel.add(centerPanel);
/*
* End of Center Panel
*/
/*
* South Panel
*/
JPanel southPanel = new JPanel();
southPanel.setBackground(Color.GREEN);
panel.add(southPanel, BorderLayout.SOUTH);
btnClear = new JButton("CLEAR");
southPanel.add(btnClear, BorderLayout.CENTER);
btnNext = new JButton("NEXT");
southPanel.add(btnNext, BorderLayout.WEST);
/*
* End of South Panel
*/
}
}
您能否澄清一下您希望JTextField看起來如何? – Daneel
我希望JTextFields與JComboBox的長度相同,但在添加JComboBox之前,我將文本字段長度設置爲25像素,但添加組合框後,所有文本字段寬度都變短了。 –
您傳遞給'JTextField'構造函數是列,而不是像素。 – user1803551