gpt4 book ai didi

java - JFrame 不处理

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

我试图在单击按钮时处理我的 JFrame。但是,当我单击按钮时,除了 JFrame 没有关闭之外,其他所有内容都执行了!为什么是这样?是不是后面直接打开另一个JFrame有问题?

public static void login() {
JFrame loginFrame = new JFrame();
JPanel loginPanel = new JPanel();
JButton loginButton = new JButton("Login");
JLabel usernameText = new JLabel("Username:");
JLabel passwordText = new JLabel("Password:");
final JTextField usernameField = new JTextField(15);
final JTextField passwordField = new JTextField(15);
status.setForeground(Color.RED);

new LoginBackground();

loginPanel.add(usernameText);
loginPanel.add(usernameField);
loginPanel.add(passwordText);
loginPanel.add(passwordField);
loginPanel.add(loginButton, BorderLayout.CENTER);
loginPanel.add(status);
loginFrame.add(loginPanel);

loginPanel.add(loginBackgroundLabel);
loginFrame.add(loginPanel);

loginFrame.repaint();
loginFrame.setVisible(true);
loginFrame.setSize(1200, 800);

loginPanel.setLayout(null);

usernameText.setSize(100, 100);
passwordText.setSize(100, 100);

usernameText.setFont(new Font("Serif", Font.PLAIN, 20));
usernameText.setForeground(Color.white);

passwordText.setFont(new Font("Serif", Font.PLAIN, 20));
passwordText.setForeground(Color.white);

loginBackgroundLabel.setLocation(0, 0);
usernameText.setLocation(555, 230);
passwordText.setLocation(555, 290);
usernameField.setLocation(500, 300);
passwordField.setLocation(500, 350);
loginButton.setLocation(550, 400);
status.setLocation(500, 450);

loginPanel.revalidate();
loginFrame.repaint();

try {
Thread.sleep(1000);
} catch (InterruptedException e2) {
e2.printStackTrace();
}

new Sound();

login = false;

loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Frame.loginFrame.setVisible(false);
Frame.loginFrame.dispose();
username = usernameField.getText();
password = passwordField.getText();
Connect.checkLogin();
Sound.clip.stop();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
}

最佳答案

设置window action on close, as dispose on close .

setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );

关于java - JFrame 不处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16341857/

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