2016-09-20 48 views
2

我想爲一個人的出生日期添加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 
     */ 
    } 
} 
+0

您能否澄清一下您希望JTextField看起來如何? – Daneel

+0

我希望JTextFields與JComboBox的長度相同,但在添加JComboBox之前,我將文本字段長度設置爲25像素,但添加組合框後,所有文本字段寬度都變短了。 –

+0

您傳遞給'JTextField'構造函數是列,而不是像素。 – user1803551

回答

2

當我加入所有3組合框,它改變了JTextField的寬度。任何幫助如何解決這個問題將不勝感激。

默認情況下,GridBagLayout中的每個組件佔用1個單元格(一個空間垂直,一個水平)。每次添加組合框時,都會增加網格中的列數,但文本字段仍然會佔用1個水平空間,與第一個組合框對齊。

enter image description here

你想要的文本字段贊同最後組合框,或者換句話說,採取一切空間保持在網格中。您可以通過指定

gbc.gridwidth = GridBagConstraints.REMAINDER; 

爲您的文本字段。 (在你的情況下,gbc.gridwidth = 3也可以,但如果你添加更多的組件,你可能需要改變它。)

請注意,由於佈局管理器爲組件分配空間,在文本域的構造函數中指定一個數字沒有太多,

還有一種更好的方式來添加JComboBox的月份,而不是一個接一個地插入它。

是的,我建議你利用新的Date-Time API。有一個Enum Month,你的常數準備就緒。不要忘了指定泛型類型爲你的組合框:

JComboBox<Month> txtMonth = new JComboBox<>(); 
for (Month month : Month.values()) 
    txtMonth.addItem(month); 

,我就不打擾你修正自己的資本(提示:getDisplayName)。

+0

感謝您的幫助 –

0

關於第二個問題,您可以直接與月的陣列創建的JComboBox:

String[] months = {"January", ..., "December"}; // put all the months here 
JComboBox txtMonth = new JComboBox(months); 

編輯:

爲了您的第一個問題:如果您希望每個JTextField的長度與三個JComboBox的長度相同,那麼您應該寫在添加任何JTextField之前添加。

詳見這裏(中間主要的例子,它是真正清楚):https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

+0

感謝您的幫助 –