2013-05-03 86 views
0

我已经在JAVA和SOMETIMES中启动了一个程序,当我运行它或调试它时,它显示一个空的白色窗口。我不知道为什么,但我重新启动它,它正确显示窗口。顺便说一句,它与最后的mysql connect void无关。程序显示一个空的窗口

下面是代码:

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(); 
    } 
} 

} 
+2

'content.setLayout(NULL);'没有我们解决您的** **最后的问题? – 2013-05-03 15:03:07

+2

Usin null-Layout真的是与系统打架。它会导致可怕的代码,非常难以维护,不可能的错误等......适当地使用它们,它们将缓解你的一天。 – 2013-05-03 15:07:59

+1

我刚刚意识到框架尺寸太小,无法显示放置它们的组件。 '使用布局和'pack'()'GUI'参数更多的燃料。 – 2013-05-03 15:13:31

回答

2
frame.setVisible(true); 

使它的最后一条语句,添加所有组件到JFrame后。


而且,它通常是做任何摆动相关的代码在GUI线程(EDT)最佳实践:

public static void main(String[] args) 
{ 
    SwingUtilities.invokeLater(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      createWindow(); 
     } 
    }); 
} 
+0

我看到框架,但有时,我没有看到它的内容。我只看到一切都是白色的。 – 2013-05-03 15:03:36

+0

我在最后添加了可见的设置。看起来它正在工作。谢谢! – 2013-05-03 15:05:35

2

Swing GUI,将应在事件指派线程启动。有关更多详细信息,请参阅Initial Threads

1

调用frame.setVisible(true);在你的方法结束(后加入所有的组件面板)

+0

oops ...当我输入Eng.Fouad回答了这个问题;) – 2013-05-03 15:08:16

相关问题