我已經在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();
}
}
}
'content.setLayout(NULL);'沒有我們解決您的** **最後的問題? – 2013-05-03 15:03:07
Usin null-Layout真的是與系統打架。它會導致可怕的代碼,非常難以維護,不可能的錯誤等......適當地使用它們,它們將緩解你的一天。 – 2013-05-03 15:07:59
我剛剛意識到框架尺寸太小,無法顯示放置它們的組件。 '使用佈局和'pack'()'GUI'參數更多的燃料。 – 2013-05-03 15:13:31