我的初始JFrame登录时遇到了一个奇怪的问题。当我运行程序时,它首先只会取消取消JButton。然后,如果我最小化框架,它会显示它应该的一切。是否有一个原因?如果是这样,我该如何解决?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
public class example
{
public static void main (String[] args)
{
JFrame frame = new JFrame("Login");
frame.setVisible(true);
frame.setSize(350,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label1 = new JLabel("User Id: ");
JLabel label2 = new JLabel("Password: ");
JTextField txt2 = new JTextField(20);
JButton button = new JButton("login");
JTextField txt = new JTextField(20);
frame.add(label1);
frame.add(txt);
frame.add(label2);
frame.add(txt2);
String user = txt.getText();
String password = txt2.getText();
JButton button2 = new JButton("Cancel");
frame.add(button);
frame.add(button2);
button2.addActionListener (new Action2());
frame.setVisible(true);
frame.setLayout(new FlowLayout());
button.addActionListener(new ActionListener()
{
public void actionPerformed( ActionEvent e)
{
if ("abc".equals(txt.getText()) && "123".equals(txt2.getText()))
{
JFrame frame2 = new JFrame("Student");
frame2.setVisible(true);
frame2.setSize(200,200);
frame.setVisible(false);
}
if ("def".equals(txt.getText()) && "456".equals(txt2.getText()))
{
JFrame frame2 = new JFrame("Instructor");
frame2.setVisible(true);
frame2.setSize(200,200);
frame.setVisible(false);
}
if ("ghi".equals(txt.getText()) && "789".equals(txt2.getText()))
{
JFrame frame2 = new JFrame("Teacher");
frame2.setVisible(true);
frame2.setSize(200,200);
frame.setVisible(false);
}
else
{
System.out.println("Invalid Password");
}
}
});
}
static class Action2 implements ActionListener {
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
}
}
答案 0 :(得分:2)
setVisible(true)应该是最后一个方法调用。
此外,建议在Java规范中指定的自己的线程中运行swing应用程序: Concurrency in Swing > Initial Threads: docs.oracle.com
所以你应该理想地运行Swing应用程序:
//example from the referenced java documentation
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
有关这方面的更多信息可能更清楚: Will the real Swing Single Threading Rule please stand up?
createAndShowGUI()方法不是必需的,它是一个任意的方法名称,它的功能是调用一个方法,它基本上完成了你已经为你的Swing应用程序编写的所有内容(修改了setVisible(true))最后在最后调用。)
所以你应该这样做:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
public class example
{
public static void main (String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
//you can alternatively replace 'createAndShowGUI()' with 'new example()'
}
});
}
public static void createAndShowGUI() {
new example();
}
public example() {
JFrame frame = new JFrame("Login");
//frame.setVisible(true);
frame.setSize(350,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label1 = new JLabel("User Id: ");
JLabel label2 = new JLabel("Password: ");
JTextField txt2 = new JTextField(20);
JButton button = new JButton("login");
JTextField txt = new JTextField(20);
frame.add(label1);
frame.add(txt);
frame.add(label2);
frame.add(txt2);
String user = txt.getText();
String password = txt2.getText();
JButton button2 = new JButton("Cancel");
frame.add(button);
frame.add(button2);
button2.addActionListener (new Action2());
//frame.setVisible(true);
frame.setLayout(new FlowLayout());
button.addActionListener(new ActionListener()
{
public void actionPerformed( ActionEvent e)
{
if ("abc".equals(txt.getText()) && "123".equals(txt2.getText()))
{
JFrame frame2 = new JFrame("Student");
frame2.setVisible(true);
frame2.setSize(200,200);
frame.setVisible(false);
}
if ("def".equals(txt.getText()) && "456".equals(txt2.getText()))
{
JFrame frame2 = new JFrame("Instructor");
frame2.setVisible(true);
frame2.setSize(200,200);
frame.setVisible(false);
}
if ("ghi".equals(txt.getText()) && "789".equals(txt2.getText()))
{
JFrame frame2 = new JFrame("Teacher");
frame2.setVisible(true);
frame2.setSize(200,200);
frame.setVisible(false);
}
else
{
System.out.println("Invalid Password");
}
}
});
frame.setVisible(true);
}
static class Action2 implements ActionListener {
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
}
}
您还应该编辑button.addActionListener(new ActionListener() 正如我在公共example()构造函数中所做的那样进行适当的修改(移动setVisible(boolean)方法调用),并为你在这里创建的新JFrame添加EXIT_ON_CLOSE。