如何调用另外一个方法里的变量!谢谢
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class A
{
public static void main(String[] args)
{
B b=new B();
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.show();
}
}
class B extends JFrame
{
public B()
{
setTitle("Ò»´íÔÙ´í");
setSize(W,H);
C c=new C();
Container content=getContentPane();
content.add(c);
}
public static final int W=300;
public static final int H=200;
}
class C extends JPanel
{
public C()
{
JButton button1=new JButton("bule");
JButton button2=new JButton("white");
add(button1);
add(button2);
button1.addActionListener(
new ActionListener(){
public void d()
{
background=Color.blue;
}
//How to transfer the variable of another function
public void actionPerformed(ActionEvent event)
{
setBackground(background);
repaint();
}
private Color background;
});
button2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event)
{
background=Color.white;
setBackground(background);
repaint();
}
private Color background;
});
}
}
麻烦大家看看这个代码啊!
public void d()
{
background=Color.blue;
}
//How to transfer the variable of another function
public void actionPerformed(ActionEvent event)
{
setBackground(background);
repaint();
}
这段有点问题(这段代码好象是没有实现调用功能的)。我想知道actionPerformed这样可以调用到public void d()里的
background呢?
我不详改成
public void actionPerformed(ActionEvent event)
{
background=Color.white;
setBackground(background);
repaint();
}
这样,我主要想知道调用别的方法里的变量是怎样作到的!谢谢大家了!