我试图创建一个基本的登录屏幕,但当我运行它时,文本框是巨大的,当我最大化窗口时会增加。如何使它保持固定尺寸?
public class LoginScreen {
LoginScreen()
{
JFrame frame = new JFrame();
Box b = Box.createVerticalBox();
JLabel title = new JLabel("Welcome to login screen");
b.add(title);
title.setAlignmentX(JFrame.CENTER_ALIGNMENT);
title.setFont(title.getFont().deriveFont(20.0f));
title.setBorder(new EmptyBorder(20,10,20,10));
Box c = Box.createHorizontalBox();
JLabel login = new JLabel("Login Id");
login.setAlignmentX(JFrame.CENTER_ALIGNMENT);
JTextField loginf = new JTextField("");
loginf.setAlignmentX(JFrame.CENTER_ALIGNMENT);
c.add(login);
c.add(loginf);
b.add(c);
frame.add(b);
frame.setSize(450,350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new LoginScreen();
}
});
}
}
我意识到它就像使用setMaximumSize()一样简单。
https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html#size-从这里得到的。