JFrame未调整大小以适应JPanel

时间:2012-05-11 19:37:01

标签: java swing layout-manager gridbaglayout

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

}

我基本上得到一个大小适当高度的窗口,但字段是水平隐藏的。

1 个答案:

答案 0 :(得分:3)

  

..窗口没有根据其包含的内容重新调整大小。

这个变种是。

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);
            }
        });
    }
}