2

我已经为一个项目模拟了一个快速的 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);

}

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

4

1 回答 1

3

..窗口没有重新调整到它所包含的大小。

这个变种是。

测试仪

如果您不知道如何使您的 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);
            }
        });
    }
}
于 2012-05-11T20:38:46.910 回答