gpt4 book ai didi

java - JPanel GridLayout 不添加组件

转载 作者:行者123 更新时间:2023-11-29 05:23:44 25 4
gpt4 key购买 nike

我正在尝试使用具有 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);

}
}

最佳答案

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

提示

  1. 参见 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (是的。)
  2. 同时考虑每个细节面板的 GroupLayout,如 this answer 中所示.

关于java - JPanel GridLayout 不添加组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23589396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com