import java.awt.*;
import java.awt.event.*;
public class TheButton extends Frame //按钮测试类
{
static TheButton fr=new TheButton("the button");
public static void main(String args[])
{
TheAdapterTest tat=new TheAdapterTest();
fr.addWindowListener(tat);
fr.setLayout(new FlowLayout());
fr.addButton();
fr.setSize(200,200);
fr.setBackground(Color.pink);
fr.setVisible(true);
}
TheButton(String str)
{
super(str);
}
public void addButton()
{
Button b1=new Button();
Button b2=new Button("have label");
Button b3=new Button("write");
MouseClicked mc=new MouseClicked();
b3.addMouseListener(mc);
add(b1);
add(b2);
add(b3);
}
}class TheAdapterTest extends WindowAdapter //Window适配器
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}class MouseClicked extends MouseAdapter //Mouse适配器
{
public void mouseClicked(MouseEvent e)
{
Button bt=(Button)e.getSource();
Graphics g=TheButton.fr.getGraphics();
g.setColor(Color.red);
g.drawString("it's label is "+bt.getLabel(),10,80);
g.drawString("it's label is "+bt.getActionCommand(),10,100);
}
}这个代码看好久,总是不明白在按钮测试类TheButton中的addButton()方法中,为什么
add(b1);
add(b2);
add(b3);
不用任何对象调用呢? add()从何而来呢?
我的理解应该是
fr.add(b1);
fr.add(b2);
fr.add(b3);
或者
this.add(b1);
this.add(b2);
this.add(b3);
但省略fr或者this并没有错
import java.awt.event.*;
public class TheButton extends Frame //按钮测试类
{
static TheButton fr=new TheButton("the button");
public static void main(String args[])
{
TheAdapterTest tat=new TheAdapterTest();
fr.addWindowListener(tat);
fr.setLayout(new FlowLayout());
fr.addButton();
fr.setSize(200,200);
fr.setBackground(Color.pink);
fr.setVisible(true);
}
TheButton(String str)
{
super(str);
}
public void addButton()
{
Button b1=new Button();
Button b2=new Button("have label");
Button b3=new Button("write");
MouseClicked mc=new MouseClicked();
b3.addMouseListener(mc);
add(b1);
add(b2);
add(b3);
}
}class TheAdapterTest extends WindowAdapter //Window适配器
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}class MouseClicked extends MouseAdapter //Mouse适配器
{
public void mouseClicked(MouseEvent e)
{
Button bt=(Button)e.getSource();
Graphics g=TheButton.fr.getGraphics();
g.setColor(Color.red);
g.drawString("it's label is "+bt.getLabel(),10,80);
g.drawString("it's label is "+bt.getActionCommand(),10,100);
}
}这个代码看好久,总是不明白在按钮测试类TheButton中的addButton()方法中,为什么
add(b1);
add(b2);
add(b3);
不用任何对象调用呢? add()从何而来呢?
我的理解应该是
fr.add(b1);
fr.add(b2);
fr.add(b3);
或者
this.add(b1);
this.add(b2);
this.add(b3);
但省略fr或者this并没有错
解决方案 »
- 一个易语言也能轻松调用win32 API为什么java
- JTree节点名字修改?
- 一个简单的编程题,大家帮忙做做
- 用Swing,怎样设置整个Application的字体?
- 如何通过反射获得方法的返回值?请勿回帖不看贴
- 一个jdbc封装问题
- 谁能告诉我最后一句话是什么意思啊?
- 这句话URL url=new URL(new URL("http://www.abc.com")"/java/intro/")怎么理解?
- 如何用程序设置某一行被选中?
- 第一次可以可是第二次就报出以下错误 为什么会出现这样的错误,我实在没办法了
- 为什么有的panel能跟着窗口大小自动调整控件大小,有的则不可以?
- JAVA Beans到底是什么?
用fr.add()不对吧?