当前位置: 代码迷 >> Eclipse >> 照书抄的简单程序,没有异常但是为什么不能运行
  详细解决方案

照书抄的简单程序,没有异常但是为什么不能运行

热度:72   发布时间:2016-04-23 00:48:23.0
照书抄的简单程序,没有错误但是为什么不能运行?
package button;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonFrame extends JFrame 
{
  private JPanel buttonPanel;
  private static final int DEFAULT_WIDTH=300;
  private static final int DEFAULT_HEIGHT=200;
  
  public ButtonFrame() 
  {                                                  
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
JButton yellowButton=new JButton("Yellow");
JButton blueButton=new JButton("Blue");
JButton redButton=new JButton("Red");
buttonPanel=new JPanel();
buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
add(buttonPanel);
ColorAction yellowAction=new ColorAction(Color.YELLOW);
ColorAction blueAction=new ColorAction(Color.BLUE);
ColorAction redAction=new ColorAction(Color.RED);

yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
  }


  private class ColorAction implements ActionListener 
  {
    private Color backgroundColor;
    
    public ColorAction(Color c)
    {
  backgroundColor=c;
    }
  
    public void actionPerformed(ActionEvent event)
    {
  buttonPanel.setBackground(backgroundColor);  
    }
  }
}


所有代码都在这了,此程序来源于《JAVA核心技术第九版》P295。写完后没有错误,但是类上右键RUN AS怎么连JAVA APPLICATION这个选项都没了?不能运行啊,求高手指点一下这是怎么回事?
------解决方案--------------------
没有main方法
------解决方案--------------------
书上的代码一般都有传承性的,前面出现过的关键代码到后面就不重复出现了,节省纸张。
楼主这里确实缺少main方法,找找前面的例子,找到有main方法的,参照着写一个过来,应该就可以了。
------解决方案--------------------
    public static void main(String[] args){
        ButtonFrame b = new ButtonFrame();
    }
------解决方案--------------------
这是普通类诶,没有main没法运行
------解决方案--------------------
引用:
这本书上说这是个完整的程序。另外这是事件处理这一章的第一个程序。我在前面也没找着可以挪来用的MAIN函数。另外,想要程序运行的话可不是必须要用到MAIN函数的哦,比如Paint()就可以,还有没有其它的我不知道。

一般要运行的java SE代码都需要main方法的,一直没学过awt和swing,对这边不太懂。楼主可以加上main方法试试。
  相关解决方案