Java Jframe在最小化之前不显示任何内容的问题
我的初始JFrame登录有一个奇怪的问题。当我运行该程序时,它只会在开始时拉出cancel按钮。然后,如果我最小化框架,它会显示它应该显示的所有内容。这有什么原因吗?如果是,我如何修复它Java Jframe在最小化之前不显示任何内容的问题,java,swing,jframe,Java,Swing,Jframe,我的初始JFrame登录有一个奇怪的问题。当我运行该程序时,它只会在开始时拉出cancel按钮。然后,如果我最小化框架,它会显示它应该显示的所有内容。这有什么原因吗?如果是,我如何修复它 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Arrays; public class example { public static void main (String[] arg
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);
}
}
}
setVisible(true)应该是最后一个方法调用
此外,建议按照Java规范的规定,在自己的线程中运行swing应用程序:
因此,理想情况下,您应该按如下方式运行Swing应用程序:
//example from the referenced java documentation
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
有关这方面的更多信息,请参见:
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(newActionListener())
如我在public example()构造函数中所做的适当修改(移动setVisible(boolean)方法调用),并为您在此处创建的新JFrames添加EXIT_ON_CLOSE。是,
setVisible(true)
应该是您准备好并将所有控件添加到框架中后的最后一次操作。请参见,谢谢!这很有意义。我怀疑我是否曾经发现frame.setVisible(true)导致了此问题。