我是Java GUI的初学者。我试图在Netbeans中创建一个简单的登录表单。它由名为TestSuiteFrame的JFrame和一个名为TestSuiteEntryPanel的JPanel组成,其中包含几个标签,文本框和一个按钮。我还没有添加“业务逻辑”。
问题在于,当我运行项目时,没有任何反应。几秒钟后,我得到这个作为输出: 跑: Java结果:-1073740771 建立成功(总时间:15秒)
就是这样。没有窗口出现。我究竟做错了什么?
以下是代码:
package TPackage;
public class TestSuiteFrame extends javax.swing.JFrame {
/**
* Creates new form TestSuiteFrame
*/
public TestSuiteFrame() {
initComponents();
TestSuiteEntryPanel a = new TestSuiteEntryPanel();
add(a);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TestSuiteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TestSuiteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TestSuiteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TestSuiteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//System.out.println("abc");
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TestSuiteFrame b = new TestSuiteFrame();
b.setTitle("Login");
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//b.pack();
b.setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
小组代码:
package TPackage;
public class TestSuiteEntryPanel extends javax.swing.JPanel {
/**
* Creates new form TestSuiteEntryPanel
*/
public TestSuiteEntryPanel() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
labelEntryName = new javax.swing.JLabel();
labelEntryPassword = new javax.swing.JLabel();
ButtonEntryLogin = new javax.swing.JButton();
TextEntryName = new javax.swing.JTextField();
TextEntryPassword = new javax.swing.JPasswordField();
setPreferredSize(new java.awt.Dimension(350, 325));
labelEntryName.setText("Name:");
labelEntryPassword.setText("Password:");
ButtonEntryLogin.setText("Login");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(labelEntryPassword)
.addComponent(labelEntryName))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(TextEntryName, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
.addComponent(TextEntryPassword))
.addContainerGap(24, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(ButtonEntryLogin)
.addGap(45, 45, 45))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(labelEntryName)
.addComponent(TextEntryName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(labelEntryPassword)
.addComponent(TextEntryPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(ButtonEntryLogin)
.addContainerGap(182, Short.MAX_VALUE))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JButton ButtonEntryLogin;
private javax.swing.JTextField TextEntryName;
private javax.swing.JPasswordField TextEntryPassword;
private javax.swing.JLabel labelEntryName;
private javax.swing.JLabel labelEntryPassword;
// End of variables declaration
}
答案 0 :(得分:1)
从initComponents
课程中删除整个TestSuiteFrame
方法(您甚至不需要它),并在pack
方法中取消注释main
。这是GroupLayout
的问题。
您的代码应如下所示:
import javax.swing.*;
public class TestSuiteFrame extends javax.swing.JFrame {
public TestSuiteFrame() {
TestSuiteEntryPanel a = new TestSuiteEntryPanel();
add(a);
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TestSuiteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TestSuiteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TestSuiteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TestSuiteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TestSuiteFrame b = new TestSuiteFrame();
b.setTitle("Login");
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.pack();
b.setVisible(true);
}
});
}
}
另请注意,在pack
上添加组件后会出现JFrame
。学习如何手动编写Java Swing代码,避免使用那个可怕的Matisse GUI构建器。
答案 1 :(得分:1)
你需要调用pack();将所有组件添加到框架后。 所以推荐pack();调用setVilisble(true);并删除构造函数中的那个: - ) 我希望有所帮助。
答案 2 :(得分:0)
确保导入必要的包(即使您已经导入了)
import java.awt.*;
import javax.swing.*;
运行你的程序。如果没有显示任何内容,并且您使用的是Windows,请按alt
- tab
以循环显示已打开的应用程序。
或者,执行程序后。按ctrl
+ alt
+ del
,然后在Windows任务管理器的Applications
标签下查看是否存在名为Login
的任务(您的java app)。
我尝试了您的代码,会显示一个窗口。您的程序中的JFrame可能会被其他应用程序隐藏。
如果您使用像Netbeans这样的IDE,请确保main program file
指向main()
内的TestSuiteFrame
。
如果仍然没有显示,请尝试运行单个文件而不是整个项目。 shift
+ F5
或右键单击TestSuiteFrame.java =&gt; Netbeans run file
。
除了所有内容之外,请确保您运行的是正确的文件。您只需在System.out.println()
内编写一些main()
语句即可。只需检查控制台是否在运行程序时打印这些行。有很多方法可以检查。
如果控制台没有打印那些println语句,那么BINGO!您可能没有运行正确的java文件/项目。