如下,Pbutton为JButton的子类,没有改动什么,只增加了x,y两个int;
在Testicon类中,用Pbutton生成按钮j无法在new Pbutton()中直接定义按钮的文本和ICON,但是如果是直接用JButton生成按钮对象的话就可以,是我的子类少了什么吗?
import javax.swing.*;
import java.awt.*;
public class Pbutton extends JButton
{
int x,y;
}
import javax.swing.*;
class Testicon extends JFrame
{
ImageIcon icon = new ImageIcon("new.gif");
Pbutton j = new Pbutton();
//Pbutton j = new Pbutton("直接setText不行");
//Pbutton j = new Pbutton(icon); //直接setIcon也不行
Testicon()
{
j.setIcon (icon);//这里可以
this.add(j);
this.setSize(200,100);
this.setVisible(true);
j.x=0;
j.y=100;
System.out.println ("按钮的赋值x= "+j.x+" 按钮的赋值y="+j.y);
}
public static void main(String args[])
{
Testicon my = new Testicon();
my.j.setText ("这个可以");
}
}
谢谢!