当前位置: 技术问答>java相关
一个小问题啊~~~~~~~~~~~大家看看~~~~`谢谢
来源: 互联网 发布时间:2017-04-22
本文导语: 我只想实现动态加按扭的功能~`` import java.awt.*; import java.awt.event.*; import java.applet.*; public class Exame2 extends Applet implements ActionListener{ public void init(){ int x = 0; setLayout(new GridLayout(3,3)); ...
我只想实现动态加按扭的功能~``
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Exame2 extends Applet implements ActionListener{
public void init(){
int x = 0;
setLayout(new GridLayout(3,3));
Button b1 = new Button();
Button b2 = new Button();
add(b1);
b1.addActionListener(this);
}
public void start(){}
public void paint(Graphics g){}
public void destroy(){
System.exit(0);
}
public void actionPerformed(ActionEvent e){
add(b2); //这说有错~~`cannot resolve symbol
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Exame2 extends Applet implements ActionListener{
public void init(){
int x = 0;
setLayout(new GridLayout(3,3));
Button b1 = new Button();
Button b2 = new Button();
add(b1);
b1.addActionListener(this);
}
public void start(){}
public void paint(Graphics g){}
public void destroy(){
System.exit(0);
}
public void actionPerformed(ActionEvent e){
add(b2); //这说有错~~`cannot resolve symbol
}
}
|
1.Button b2 的声明放在init()外,actionPerformed函数才可以引用到b2
2. add(b2);后面加上validate()
2. add(b2);后面加上validate()
|
这是变量作用域的问题