gpt4 book ai didi

java - JFrame 未调整大小以适合 JPanel

转载 作者:行者123 更新时间:2023-12-04 05:46:57 25 4
gpt4 key购买 nike

我为一个项目模拟了一个快速的 GUI,我可以看到标签和文本字段,但由于某种原因,窗口没有重新调整大小以适应它所包含的内容。

这是基本的运行:

创建 JFrame 并添加 JPanel:

   JFrame frame1 = new JFrame("Hotel Reservation App");
frame1.getContentPane().add(rViewPan, BorderLayout.CENTER);
frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

frame1.pack();
frame1.setVisible(true);

这是 rViewPan 的 JPanel 代码:
 private void initComponents() {

setLayout(new BorderLayout());

arrivalDateTF = new JTextField();


departureDateTF = new JTextField();
roomCategoryTF = new JTextField();
roomQtyTF = new JTextField();

JTextField[] textFields = { arrivalDateTF, departureDateTF, roomCategoryTF, roomQtyTF };
JLabel[] textLabels = { new JLabel("1 : "), new JLabel("2 : "),
new JLabel("3 : "), new JLabel("4 : ")
};

JPanel displayPan = new JPanel();
GridBagLayout gridBagLay = new GridBagLayout();
GridBagConstraints gridBagC = new GridBagConstraints();

displayPan.setLayout(gridBagLay);
SwingUtilities.addTextElementsAsRows(textLabels, textFields, gridBagLay, displayPan);

gridBagC.gridwidth = GridBagConstraints.REMAINDER;
gridBagC.anchor = GridBagConstraints.EAST;
gridBagC.weightx = 1.0;

displayPan.add(new JLabel(" "), gridBagC);

submitB = new JButton("Soumettre");
displayPan.add(submitB, gridBagC);

SwingUtilities.addStdBorder(displayPan, "Reservation");
add(displayPan, BorderLayout.CENTER);

}

我基本上得到了一个大小合适的窗口,但是这些字段是水平隐藏的。

最佳答案

..the window isn't re-sized to what it contains.



这个变种是。

TestGui

如果您无法弄清楚如何使您的 GUI 运行,我建议您制作并发布 SSCCE。
import java.awt.*;
import javax.swing.*;

// TODO don't extend JPanel, just create an instance
class TestGui extends JPanel {

JTextField arrivalDateTF;
JTextField departureDateTF;
JTextField roomCategoryTF;
JTextField roomQtyTF;
JButton submitB;

TestGui() {
initComponents();
}

private void initComponents() {
setLayout(new BorderLayout());

arrivalDateTF = new JTextField(6);
departureDateTF = new JTextField(6);
roomCategoryTF = new JTextField(8);
roomQtyTF = new JTextField(2);

JTextField[] textFields = { arrivalDateTF, departureDateTF, roomCategoryTF, roomQtyTF };
JLabel[] textLabels = { new JLabel("1 : "), new JLabel("2 : "),
new JLabel("3 : "), new JLabel("4 : ")
};

JPanel displayPan = new JPanel();
GridBagLayout gridBagLay = new GridBagLayout();
GridBagConstraints gridBagC = new GridBagConstraints();

displayPan.setLayout(gridBagLay);
// SwingUtilities.addTextElementsAsRows(textLabels, textFields, gridBagLay, displayPan);
for (int ii=0; ii<textFields.length; ii++) {
displayPan.add( textLabels[ii] );
displayPan.add( textFields[ii] );
}

gridBagC.gridwidth = GridBagConstraints.REMAINDER;
gridBagC.anchor = GridBagConstraints.EAST;
gridBagC.weightx = 1.0;

displayPan.add(new JLabel(" "), gridBagC);

submitB = new JButton("Soumettre");
displayPan.add(submitB, gridBagC);

// SwingUtilities.addStdBorder(displayPan, "Reservation");
add(displayPan, BorderLayout.CENTER);
}

public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame1 = new JFrame("Hotel Reservation App");
frame1.getContentPane().add(new TestGui(), BorderLayout.CENTER);
frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

frame1.pack();
frame1.setVisible(true);
}
});
}
}

关于java - JFrame 未调整大小以适合 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10557546/

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