JAVA图形用户界面设计
public static void main(String[] args)throws Exception {
//编写一个猜数字的游戏,按下“产生随机数”按钮产生一个0~9的随机数,
//则在屏幕的提示框显示“输入所猜测的数字”,当输入所猜测的数字并按“确定”按钮后,
//在提示框中提示“输入正确”(或“猜小了”、或“猜大了”)。
JButton button = new JButton("我是按钮");
int value;
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
value = (int)Math.random()*10;
}
});
}
为什么在main方法中的value变量在监听器中无法使用
class MyJFrame extends JFrame{
private JButton button ;
private int value;
public void text(){
button = new JButton("产生随机数!");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
value = (int)Math.random()*10;
}
});
}
}
而在自定义类中的监听器中可以使用value变量