一个最小的Java程序应该是什么样子?
我编写了以下代码:一个最小的Java程序应该是什么样子?,java,nosuchmethoderror,Java,Nosuchmethoderror,我编写了以下代码: import java.awt.*; import java.awt.event.*; class Party { public void buildInvite() { Frame f = new Frame(); Label l = new Label("Party at Tim's"); Button b = new Button("You bet"); Button c = new Button
import java.awt.*;
import java.awt.event.*;
class Party {
public void buildInvite() {
Frame f = new Frame();
Label l = new Label("Party at Tim's");
Button b = new Button("You bet");
Button c = new Button("Shoot me");
Panel p = new Panel();
p.add(l);
} //more code here...
}
我一直收到以下错误:线程“main”java.lang.NoSuchMethodError中的异常:main
我试图向代码中添加一个额外的main(String[]args)
,但仍然会出现相同的错误
另外,我应该在代码中添加什么,以及它应该放在布局中的什么位置?确保您的
main
方法如下所示:
public static void main(String[] args) {
...
}
使用Java和Swing的初学者应该查看。以下是他们的:
因为您声称自己是绝对的noob,所以请确保在发布java之前使用javac filename.java编译java类filename@Jagat是的,我说对了。哈哈,谢谢,谢谢。我一定会看一看Oracle教程。@Craig:有很多好的示例代码可以运行。另外,如果您想要一本deadtree书籍,那么核心Java()是Swing的良好起点。
package start;
import javax.swing.*;
public class HelloWorldSwing {
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add the ubiquitous "Hello World" label.
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}});}}