为什么有时摆动的JPanel布局会自动调整为最小

时间:2018-12-05 09:45:28

标签: java swing jpanel

当我双击秋千罐时,它显示正常。按钮,文本字段和标签均按预期显示。 enter image description here 但是一段时间后,JPanel会自动调整为最小,如下所示。 我已经设置了框架setResizable(false) enter image description here 任何人都可以提出一些想法?

    public TestSwing() {
        super(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        log = new JTextArea(18, 45);
        log.setMargin(new Insets(5, 5, 5, 5));
        log.setEditable(true);
        logScrollPane = new JScrollPane(log);
        DefaultCaret caret = (DefaultCaret) log.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

        btn_source = new JButton("open");
        btn_dest = new JButton("open");
        lbl_sourceFile = new JLabel("Source Excel File:");
        lbl_destFile = new JLabel("Destination XML Folder:");
        txt_source = new JTextField(45);
        txt_dest = new JTextField(45);
        btn_convert = new JButton("Convert");

        c.fill = GridBagConstraints.VERTICAL;
        c.insets = new Insets(0, 0, 0, 0);
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        add(lbl_sourceFile, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 0;
        c.gridwidth = 1;
        add(txt_source, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 2;
        c.gridy = 0;
        c.insets = new Insets(0, 10, 2, 0);
        c.gridwidth = 1;
        add(btn_source, c);
    }

    public void actionPerformed(ActionEvent e) {}

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("TestLink Converter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TestSwing());
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                UIManager.put("swing.boldMetal", Boolean.FALSE);
                createAndShowGUI();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

frame.add(new TLSwing());

我们不知道什么是“ TLSing()”类,但是我想这是一个使用GridBagLayout的面板。如果没有足够的空间来显示组件的首选大小,则该组件将以其最小大小显示,因此您的文本组件会缩小。

  

我已经设置了框架setResizable(false)

您应该在打包()框架之前设置调整大小状态。

frame.setResizable(false);
frame.pack();
frame.setVisible(true);
//frame.setResizable(false);