为什么pack()缩小框架?

时间:2019-10-17 13:32:52

标签: java swing pack

我已经编写了一个登录窗口,如果我将使用setBounds()定义框架的大小,则所有组件都是可见的。但是,使用pack()可以将框架缩小到最小,并且没有任何要显示的组件。

public class Window extends JFrame {
    public Window() {
        setTitle("Login");
        setLocationRelativeTo(null);
        setSize(new Dimension(200, 130));
        pack(); // it make the frame shrink to the minimal
        JLabel lblUser = new JLabel("User:");
        lblUser.setPreferredSize(new Dimension(10, 0));
        lblUser.setHorizontalAlignment(SwingConstants.RIGHT);
        TextField txtUser = new TextField(10);
        JLabel lblPassword = new JLabel("Password:");
        lblPassword.setPreferredSize(new Dimension(10, 0));
        lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
        TextField txtPassword = new TextField(10);
        JPanel pnlData = new JPanel(new GridLayout(2, 2, 5, 5));
        Border titleBorder = new TitledBorder("Login");
        pnlData.setBorder(titleBorder);
        pnlData.add(lblUser);
        pnlData.add(txtUser);
        pnlData.add(lblPassword);
        pnlData.add(txtPassword);
        JButton jbtOk = new JButton("OK");
        JButton jbtCancel = new JButton("Cancel");
        JPanel pnlButton = new JPanel(new FlowLayout(CENTER, 10, 0));
        pnlButton.add(jbtOk);
        pnlButton.add(jbtCancel);
        Box boxOutter = Box.createVerticalBox();
        boxOutter.add(pnlData);
        boxOutter.add(pnlButton);
        add(boxOutter);
        setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:1)

将所有组件添加到框架后,必须调用方法pack。并且必须在setLocationRelativeTo / pack之后调用方法setSize

另一个问题:lblPassword.setPreferredSize(new Dimension(10, 0));。这句话没有意义。

最后一个:不要混合使用AWT和Swing组件。使用JTextField代替TextField

这是我为您纠正的例子。

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.TextField;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

/**
 * <code>Window</code>.
 */
public class Window extends JFrame {
    public Window() {
        setTitle("Login");
        JLabel lblUser = new JLabel("User:");
        lblUser.setPreferredSize(new Dimension(10, 0));
        lblUser.setHorizontalAlignment(SwingConstants.RIGHT);
        JTextField txtUser = new JTextField(10);
        JLabel lblPassword = new JLabel("Password:");
        lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
        TextField txtPassword = new TextField(10);
        JPanel pnlData = new JPanel(new GridLayout(2, 2, 5, 5));
        Border titleBorder = new TitledBorder("Login");
        pnlData.setBorder(titleBorder);
        pnlData.add(lblUser);
        pnlData.add(txtUser);
        pnlData.add(lblPassword);
        pnlData.add(txtPassword);
        JButton jbtOk = new JButton("OK");
        JButton jbtCancel = new JButton("Cancel");
        JPanel pnlButton = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
        pnlButton.add(jbtOk);
        pnlButton.add(jbtCancel);
        Box boxOutter = Box.createVerticalBox();
        boxOutter.add(pnlData);
        boxOutter.add(pnlButton);
        add(boxOutter);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(Window::new);
    }
}