Java 程序显示一个空窗口

Java 程序显示一个空窗口,java,swing,window,Java,Swing,Window,我用JAVA启动了一个程序,有时,当我运行或调试它时,它会显示一个空白的白色窗口。我不知道为什么,但我重新调试了它,它正确地显示了窗口。顺便说一句,这与最后的mysql连接无效无关 代码如下: package com.hinx.client; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.s

我用JAVA启动了一个程序,有时,当我运行或调试它时,它会显示一个空白的白色窗口。我不知道为什么,但我重新调试了它,它正确地显示了窗口。顺便说一句,这与最后的mysql连接无效无关

代码如下:

package com.hinx.client;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.sql.*;


public class Main {

public static void main(String [] args) 
{
    createWindow();
}


static void createWindow()
{


    //Create panel
    JPanel content = new JPanel();
    content.setLayout(null);
    //Build the frame
    JFrame frame = new JFrame("Hinx - A marketplace for apps - Client ALPHA_0.0.1");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(700, 233);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.add(content);
    frame.setVisible(true);

    //Create username label
    JLabel username = new JLabel("Username:");
    username.setFont(new Font("Arial", Font.BOLD, 15));
    username.setForeground(Color.white);
    username.setBounds(34, 8, 100, 50);

    //Create password label
    JLabel password = new JLabel("Password:");
    password.setFont(new Font("Arial", Font.BOLD, 15));
    password.setForeground(Color.white);
    password.setBounds(36, 85, 100, 50);

    //Create username field
    JTextField usernamet = new JTextField(20);
    usernamet.setBounds(12, 50, 125, 30);
    usernamet.setBorder(javax.swing.BorderFactory.createEmptyBorder());

    //Create password field
    JTextField passwordt = new JTextField(20);
    passwordt.setBounds(12, 125, 125, 30);
    passwordt.setBorder(javax.swing.BorderFactory.createEmptyBorder());

    //Add the login button
    JButton login = new JButton("Login");
    login.setBounds(0, 175, 150, 30);
    login.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {

        }
    });

    //Create login panel
    JPanel loginpanel = new JPanel();
    loginpanel.setLayout(null);
    loginpanel.setBounds(0, 0, 150, 400);
    loginpanel.setBackground(Color.gray);

    //Add the items to the loginpanel
    loginpanel.add(username);
    loginpanel.add(password);
    loginpanel.add(usernamet);
    loginpanel.add(passwordt);
    loginpanel.add(login);

    //Add the items to the content panel
    content.add(loginpanel);
}

protected void connect()
{
    String driver = "com.mysql.jdbc.Driver";
    String dbadress = "";
    String dbname = "";
    String username = "";
    String password = "";
    try
    {
        Class.forName(driver).newInstance();
        Connection conn = DriverManager.getConnection(dbadress+dbname, username,password);
        Statement st = conn.createStatement();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

}
将所有组件添加到JFrame后,将其作为最后一条语句


此外,在GUI线程(EDT)中执行任何与swing相关的代码通常是最佳实践:

将所有组件添加到JFrame后,将其作为最后一条语句


此外,在GUI线程(EDT)中执行任何与swing相关的代码通常是最佳实践:


Swing GUI应该在事件调度线程上启动。有关更多详细信息,请参阅。

应在事件调度线程上启动Swing GUI。有关更多详细信息,请参阅。

调用
frame.setVisible(true)
在方法末尾(将所有组件添加到面板后)

调用
frame.setVisible(true)
在方法末尾(将所有组件添加到面板后)

content.setLayout(null)我们不是在你的上一个问题中提到过吗?使用空布局确实是在与系统作斗争。它会导致可怕的代码,很难维护,不可能的错误等。。。适当地使用它们,它们会让你轻松一天。我刚刚意识到框架尺寸太小,无法显示你放置它们的组件。“使用布局和
pack()
thegui”参数更具说服力。我赞成完全重复,
content.setLayout(null)我们不是在你的上一个问题中提到过吗?使用空布局确实是在与系统作斗争。它会导致可怕的代码,很难维护,不可能的错误等。。。适当地使用它们,它们会让你轻松一天。我刚刚意识到框架尺寸太小,无法显示你放置它们的组件。“使用布局和
pack()
GUI”参数更具说服力。我赞成完全复制,我看到了框架,但有时看不到其中的内容。我只是看到所有东西都是白色的。我在末尾添加了可见的设置。看起来它起作用了。非常感谢。我看到了框架,但有时我看不到其中的内容。我只是看到所有东西都是白色的。我在末尾添加了可见的设置。看起来它起作用了。非常感谢。哎呀。。。当我打字时,福阿德回答了这个问题;)哎呀。。。当我打字时,福阿德回答了这个问题;)
frame.setVisible(true);
public static void main(String[] args)
{
    SwingUtilities.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            createWindow();
        }
    });
}