gpt4 book ai didi

java - 为什么 pack() 会缩小框架?

转载 作者:行者123 更新时间:2023-11-30 05:26:43 26 4
gpt4 key购买 nike

我编写了一个登录窗口,如果我使用 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);
}
}

最佳答案

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

另一个问题: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);
}
}

关于java - 为什么 pack() 会缩小框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58433944/

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