首先,如果要在内部类mypro中引用外部变量b,必须把外部变量声明为final。
其次在“b.addMouseListener(mypro);”中,mypro只是一个类名,而不是具体的对象。
//我想在鼠????作一些?作
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class a{
public static void main(String[] arag)
{
Frame f=new Frame("sdfsdfsdf");
final JButton b=new JButton("sdf");
//==============================================
class mypro extends MouseAdapter
{int count=0;
public void mouseClicked(MouseEvent e)
{
count++;
if (count%2==1)
b.setBackground(Color.red); else
b.setBackground(Color.black);
}
}
//=============================================
b.setBounds(50,50,60,50);
b.addMouseListener(new mypro());
// b.addMouseListener(mypro);
f.add(b);
f.setLayout(null);
f.setBounds(10,10,300,200);
f.show();
} }修改成这样可以通过编译
其次在“b.addMouseListener(mypro);”中,mypro只是一个类名,而不是具体的对象。
//我想在鼠????作一些?作
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class a{
public static void main(String[] arag)
{
Frame f=new Frame("sdfsdfsdf");
final JButton b=new JButton("sdf");
//==============================================
class mypro extends MouseAdapter
{int count=0;
public void mouseClicked(MouseEvent e)
{
count++;
if (count%2==1)
b.setBackground(Color.red); else
b.setBackground(Color.black);
}
}
//=============================================
b.setBounds(50,50,60,50);
b.addMouseListener(new mypro());
// b.addMouseListener(mypro);
f.add(b);
f.setLayout(null);
f.setBounds(10,10,300,200);
f.show();
} }修改成这样可以通过编译
Frame f=new Frame("sdfsdfsdf");
JButton b=new JButton("sdf");
定义成类变量。
分不够可以开贴再加!
或者用一个DELPHI 的问题交换好吗!
谢谢各位了!
我刚学
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class a extends Frame {
int count = 0;
JButton b;
a () {
setTitle("sdfsdfsdf"); addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
b=new JButton("sdf"); mypro m = new mypro();
b.setBounds(50,50,60,50);
b.addMouseListener(new mypro());
add(b);
setLayout(null);
setBounds(10,10,300,200);
show();
} public static void main(String[] arag)
{
Frame frame = new a();
}
class mypro extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
count++;
if (count%2==1)
b.setBackground(Color.red); else
b.setBackground(Color.black);
}
}
}