Java <;未找到主类>。。。。。。。。。。。。我怎样才能解决它?
我试图编译我的简单程序。但是当我想编译这个程序时:Java <;未找到主类>。。。。。。。。。。。。我怎样才能解决它?,java,netbeans,mainclass,Java,Netbeans,Mainclass,我试图编译我的简单程序。但是当我想编译这个程序时: import javax.swing.*; public class First { JFrame f; First(){ f=new JFrame(); String i1 = JOptionPane.showInputDialog(f,"Enter Name"); } public static void main(String[] args) { new JOptionPane
import javax.swing.*;
public class First {
JFrame f;
First(){
f=new JFrame();
String i1 = JOptionPane.showInputDialog(f,"Enter Name");
}
public static void main(String[] args) {
new JOptionPane();
}
}
我收到这个消息:
而我的IDE是:netbeans
这是一张照片:
好吧,您创建这个类的方式让我想知道您的项目中是否还有另一个类被认为是启动类,因为为了显示名称输入的输入框,您需要先创建的一个实例(而不是JOptionPane())这样构造函数就可以启动它了 如果这是项目中唯一的类,则仍然可以启动输入框,但需要先在main()方法中创建的实例,如下所示:
public static void main(String[] args) {
/* You could just use: new First(); but you'll see
the NetBeans yellow Warning underline beneath the
code line which you can ignore. Better to provide
a variable to the instance of First as done here. */
First f2 = new First();
}
总的来说,最好直接将名称输入提示放入main()方法中。一旦提供了名称,您还希望将该名称保留到字符串变量中,该字符串变量可能是整个类的全局变量,而不仅仅是在构造函数的范围内stringi1
可能应该声明为类成员变量,并命名更合适的名称,比如:stringusername代码>
我了解JFrame的概念,因为如果没有父组件并且使用了null,JOptionPanes喜欢隐藏在IDE(或其他“顶部”窗口)后面。但是如果您这样做了,那么请将其设置为不会无意中使用默认的EXIT\u ON\u close
属性值关闭您的应用程序。您希望它是DISPOSE\u ON\u CLOSE
。您还需要将JFrame的setAlwaysOnTop属性设置为布尔true。使用JOptionPane后,请务必处理JFrame,否则应用程序将保持活动状态,直到有东西实际关闭它。您可以在下面的示例中看到这一点:
import javax.swing.*;
public class First {
private static JFrame dialogPARENT; // Parent used for dialogs that don't have a parent.
private static String userName; // Holds the User Name supplied in either the Input Box or Setter method.
// Class Constructor
First() {
dialogPARENT = new JFrame();
dialogPARENT.setAlwaysOnTop(true);
dialogPARENT.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialogPARENT.setLocationRelativeTo(null);
userName = JOptionPane.showInputDialog(dialogPARENT, "Enter Your Name:");
dialogPARENT.dispose(); // dispose of the JFrame.
}
public static void main(String[] args) {
First first = new First(); // Fires the constructor
// Display the User Name. As you can see, basic HTML can
// be used in your Message Box dialog display string.
JOptionPane.showMessageDialog(dialogPARENT, "<html>The name you entered is:<br><br>"
+ "<center><font color=red><b>" + userName + "</b></font></center><br></html>",
"Supplied User Name", JOptionPane.INFORMATION_MESSAGE);
dialogPARENT.dispose(); // dispose of the JFrame.
}
}
import javax.swing.*;
头等舱{
私有静态JFrame dialogPARENT;//用于没有父对象的对话框的父对象。
私有静态字符串userName;//保存输入框或Setter方法中提供的用户名。
//类构造函数
第一(){
dialogPARENT=newjframe();
dialogPARENT.setAlwaysOnTop(true);
dialogPARENT.setDefaultCloseOperation(JFrame.DISPOSE\u ON\u CLOSE);
dialogPARENT.setLocationRelativeTo(空);
userName=JOptionPane.showInputDialog(dialogPARENT,“输入您的姓名:”);
dialogPARENT.dispose();//处理JFrame。
}
公共静态void main(字符串[]args){
First First=new First();//激发构造函数
//显示用户名。如您所见,基本HTML可以
//将在消息框对话框中使用显示字符串。
showMessageDialog(dialogPARENT,“您输入的名称是:
”
+“+userName+”
“,
“提供的用户名”,JOptionPane.信息(信息);
dialogPARENT.dispose();//处理JFrame。
}
}
我认为这说明了问题所在。您的代码在BlueJ中编译得很好