【发布时间】:2015-05-26 22:28:39
【问题描述】:
我知道 Java 需要 main 函数,我有一个。此代码编译没有错误(此代码是设计模式书的源代码),但是当我执行时
D:\ java SwingObserverExample
Error: Could not find or load main class SwingObserverExample
这里是源代码:
package headfirst.designpatterns.observer.swing;
import java.awt.*;
import javax.swing.*;
public class SwingObserverExample{
JFrame frame;
public static void main(String[] args) {
SwingObserverExample example = new SwingObserverExample();
example.go();
}
public void go() {
frame = new JFrame();
JButton button = new JButton("Should I do it?");
// Without lambdas
//button.addActionListener(new AngelListener());
//button.addActionListener(new DevilListener());
// With lambdas
button.addActionListener(event ->
System.out.println("Don't do it, you might regret it!")
);
button.addActionListener(event ->
System.out.println("Come on, do it!")
);
frame.getContentPane().add(BorderLayout.CENTER, button);
// Set frame properties
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(BorderLayout.CENTER, button);
frame.setSize(300,300);
frame.setVisible(true);
}
/*
* Remove these two inner classes to use lambda expressions instead.
*
class AngelListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.out.println("Don't do it, you might regret it!");
}
}
class DevilListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.out.println("Come on, do it!");
}
}
*/
为什么java找不到这个程序的主类?
我的java文件的目录是D:\workplace\Head-First-Design-Patterns-master\Head-First-Design-Patterns-master\src\headfirst\designpatterns\observer\swing
我从那里运行 java 命令。
【问题讨论】: