猿问

各位慕友啊,为什么我的40~42行显示找不到符号的错误呢?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.lang.String;

public class Calculate
{
public static void main (String[] args)
{
FrameSize frame = new FrameSize();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class FrameSize extends JFrame
{
public FrameSize()
{  
setSize(600,400); //设置框架尺寸  
ButtonPanel panel2 = new ButtonPanel();  
Container contentPane = getContentPane();
contentPane.add(panel2);  
}
}

//创建按钮面板
class ButtonPanel extends JPanel 
{
public ButtonPanel()
{
JButton Button1 = new JButton("0"); //创建按钮
JButton Button2 = new JButton("1");
JButton Button3 = new JButton("2");

add(Button1);  
add(Button2);
add(Button3);

ButtonAction ButtonAction1 = new ButtonAction("1"); //40行找不到符号
ButtonAction ButtonAction2 = new ButtonAction("2"); //41行找不到符号
ButtonAction ButtonAction3 = new ButtonAction("3"); //42行找不到符号

Button1.addActionListener(ButtonAction1);  
Button2.addActionListener(ButtonAction2);
Button3.addActionListener(ButtonAction3);

}

private class ButtonAction implements ActionListener
{

public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if(source == Button1) ButtonText = "0";
else if(source == Button2) ButtonText = "1";
else if(source == Button3) ButtonText = "2";  
repaint();
}
}
private JButton Button1;
private JButton Button2;
private JButton Button3;

public String ButtonText=" ";
}

神不在的星期二
浏览 80回答 2
2回答

尚方宝剑之说

你这BUTTONACTION什么意思啊!?有这么一个语句的吗?private class ButtonAction implements ActionListener这个里面,你自己都写了点3个BUTTON的不同情况时候设计BUTTONTEXT的情况了.为什么还要搞3个BUTTONACTION?试一下把下面3行删除ButtonAction ButtonAction1 = new ButtonAction("1"); //40行找不到符号ButtonAction ButtonAction2 = new ButtonAction("2"); //41行找不到符号ButtonAction ButtonAction3 = new ButtonAction("3"); //42行找不到符号下面的是改的:Button1.addActionListener(new ButtonAction() );  Button2.addActionListener(new ButtonAction());Button3.addActionListener(new ButtonAction());这样应该能达到你的效果.

蛊毒传说

你并没有定义ButtonAction类的构造函数但在声名对象时却传入了参数,当然不行啊!例如ButtonAction ButtonAction1 = new ButtonAction("1");中的参数"1",给删了,能运行你这个程序是测试程序吗?运行没有实际的效果出现。
随时随地看视频慕课网APP

相关分类

Java
我要回答