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