JPanel GridLayout不添加组件

时间:2014-05-11 05:50:52

标签: java swing jpanel layout-manager grid-layout

我正在尝试使用具有GridLayout的JPanel创建GUI。 fromPanel工作正常,但toPanel只会添加JTextFields。面板的代码几乎完全相同,所以我不确定为什么一个有效但另一个无效。我尝试将行或列更改为0,但JLabel仍未显示在toPanel中。

这是我的代码:

public class Driver extends JFrame{
private int WIDTH = 800, HEIGHT = 500, WIDTH2 = 350;
private JPanel toPanel, fromPanel, sizePanel, messagePanel, deliveryPanel,
        totalPanel, bottomPanel;
private JLabel firstLabel, lastLabel, streetLabel, cityLabel, stateLabel, zipLabel;
private JTextField toFirstText, toLastText, toStreetText, toCityText, toStateText, toZipText,
    fromFirstText, fromLastText, fromStreetText, fromCityText, fromStateText, fromZipText;

public Driver(){
    setTitle("JoAnn's Floral");
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    //labels
    lastLabel = new JLabel("Last Name", JLabel.RIGHT);
    firstLabel = new JLabel("First Name", JLabel.RIGHT);
    streetLabel = new JLabel("Street", JLabel.RIGHT);
    cityLabel = new JLabel("City", JLabel.RIGHT);
    stateLabel = new JLabel("State", JLabel.RIGHT);
    zipLabel = new JLabel("ZIP", JLabel.RIGHT);
    buildToPanel();
    add(toPanel);
    buildFromPanel();
    add(fromPanel);
}

public void buildToPanel(){
    toPanel = new JPanel(new GridLayout(6, 2, 5, 5));
    toPanel.setBorder(BorderFactory.createTitledBorder("To"));
    toPanel.setPreferredSize(new Dimension(WIDTH2, HEIGHT/3));
    //text fields
    toLastText = new JTextField(10);
    toFirstText = new JTextField(10);
    toStreetText = new JTextField(10);
    toCityText = new JTextField(10);
    toStateText = new JTextField(10);
    toZipText = new JTextField(10);
    //add to layout
    toPanel.add(firstLabel);
    toPanel.add(toFirstText);       
    toPanel.add(lastLabel);
    toPanel.add(toLastText);
    toPanel.add(streetLabel);
    toPanel.add(toStreetText);
    toPanel.add(cityLabel);
    toPanel.add(toCityText);
    toPanel.add(stateLabel);
    toPanel.add(toStateText);
    toPanel.add(zipLabel);
    toPanel.add(toZipText);
}

public void buildFromPanel(){
    fromPanel = new JPanel(new GridLayout(6, 2, 5, 5));
    fromPanel.setBorder(BorderFactory.createTitledBorder("From"));
    fromPanel.setPreferredSize(new Dimension(WIDTH2, HEIGHT/3));
    //text fields
    fromFirstText = new JTextField(10);
    fromLastText = new JTextField(10);
    fromStreetText = new JTextField(10);
    fromCityText = new JTextField(10);
    fromStateText = new JTextField(10);
    fromZipText = new JTextField(10);
    //add to layout
    fromPanel.add(firstLabel);
    fromPanel.add(fromFirstText);
    fromPanel.add(lastLabel);
    fromPanel.add(fromLastText);
    fromPanel.add(streetLabel);
    fromPanel.add(fromStreetText);
    fromPanel.add(cityLabel);
    fromPanel.add(fromCityText);
    fromPanel.add(stateLabel);
    fromPanel.add(fromStateText);
    fromPanel.add(zipLabel);
    fromPanel.add(fromZipText);
}

public static void main(String[] args) {
Driver drive = new Driver();
drive.setVisible(true);

}
}

1 个答案:

答案 0 :(得分:3)

JComponent一次只能出现在一个容器中。由于每个标签只有一个实例,因此代码只会在屏幕上显示一个。

提示

  1. 请参阅Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(是)。
  2. 还要考虑this answer中显示的每个详情面板的GroupLayout