当我使用组件创建标准JFrame时,我注意到了这个问题。所有组件都将隐藏,直到不会选择JFrame的大小。我读到了这个问题,但没有看到任何解决这个问题的方法。
public class Authorization extends JFrame {
private String login;
private String password = "";
public Authorization(String name){
super (name);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
setSize(400, 200);
setResizable(false);
setLocationRelativeTo(null);
JLabel text1 = new JLabel("text1");
text1.setFont(new Font(null, Font.PLAIN, 25));
JLabel text2 = new JLabel("text2");
text2.setFont(new Font(null,Font.PLAIN,15));
JLabel text3 = new JLabel("text3");
text3.setFont(new Font(null,Font.PLAIN,15));
JLabel text4 = new JLabel("");
text4.setFont(new Font(null,Font.PLAIN,20));
JTextField loginField = new JTextField("Login", 10);
JPasswordField passwordField = new JPasswordField(10);
JButton button = new JButton("button");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button){
login = loginField.getText();
char[] passwordAr = passwordField.getPassword();
for (int i = 0; i < passwordAr.length; i++) {
password+=passwordAr[i];
}
if (verification())
JOptionPane.showMessageDialog(null,"Done");
else
{
JOptionPane.showMessageDialog(null,"Wrong!");
}
}
}
});
add(text1);
add(text2);
add(loginField);
add(text3);
add(passwordField);
add(button);
add(text4);
}