我刚刚创建了一个新的java项目并打开了一个jframe文件。当我添加几个按钮并预览它时,它看起来不错。但是当我运行文件(indexx.java)时,它只向我显示标准的“Hello World”输出,而没有其他内容。它没有显示 JFrame。
我查看了一些关于为什么会发生这种情况的教程,但没有一个起作用。 -去设置项目配置并将主类设置为indexx不起作用
它只是向我展示了取得成功
这是我的indexx.java 文件
import javax.swing.JOptionPane;
public class indexx extends javax.swing.JFrame {
public indexx() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
student = new javax.swing.JButton();
admin = new javax.swing.JButton();
exit = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setUndecorated(true);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
student.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
student.setIcon(new javax.swing.ImageIcon(getClass().getResource("/studentedited.png"))); // NOI18N
student.setText("Student");
student.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
studentActionPerformed(evt);
}
});
getContentPane().add(student, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 120, 150, -1));
admin.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
admin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/admin_1.png"))); // NOI18N
admin.setText("Admin");
admin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
adminActionPerformed(evt);
}
});
getContentPane().add(admin, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 200, 150, -1));
exit.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/exitt.png"))); // NOI18N
exit.setText("Exit");
exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitActionPerformed(evt);
}
});
getContentPane().add(exit, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 280, 150, -1));
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/t.jpg"))); // NOI18N
jLabel1.setText(" ");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 840, 480));
pack();
}// </editor-fold>
private void studentActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void exitActionPerformed(java.awt.event.ActionEvent evt) {
int a = JOptionPane.showConfirmDialog(null, "Exit Application?", "Select", JOptionPane.YES_NO_OPTION);
if(a==0){
System.exit(0);
}else{}
// TODO add your handling code here:
}
private void adminActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @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(indexx.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(indexx.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(indexx.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(indexx.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new indexx().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton admin;
private javax.swing.JButton exit;
private javax.swing.JLabel jLabel1;
private javax.swing.JButton student;
// End of variables declaration
}
这是我的主课
package com.mycompany.quiz_management;
//import javax.swing.JFrame;
/**
*
* @author HP
*/
public class Quiz_Management {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
我几乎没有添加任何内容,并且我正在遵循的教程没有遇到此问题。任何建议/帮助将不胜感激。
发生这种情况是因为在
main
类中的 com.mycompany.quiz_management
下,您只打印 “Hello World”,而实际上并没有创建 indexx
JFrame 的实例。
在您的
indexx
上实例化您的 main
。
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new indexx().setVisible(true);
}
});
}