关于类中类的方法调用

qq_36219416 2016-09-23 01:54:12
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example04 extends Applet implements ActionListener {

Button buttonPlay,buttonLoop,buttonStop;
TextField text;
Thread thread;
public Example04(){
buttonPlay=new Button("开始播放");
buttonLoop=new Button("循环播放");
buttonStop=new Button("停止播放");
text=new TextField(12);
}
public void runx() {
buttonPlay.setBackground(Color.red);
buttonLoop.setBackground(Color.green);
text.setText("您可以播放了...");
System.out.println("test");
}

@Override
public void init(){
thread=new Thread(new Runnable(){
@Override
public void run(){
(new Example04()).runx();

}

public void runx(){
text.setBackground(Color.BLACK);

}

});
thread.setPriority(Thread.MIN_PRIORITY);
buttonPlay.addActionListener(this);
buttonLoop.addActionListener(this);
buttonStop.addActionListener(this);
add(buttonPlay);
add(buttonLoop);
add(buttonStop);
add(text);
}
public void start(){
thread.start();
}
public void stop(){

}
public void actionPerformed(ActionEvent e) {

}


}


在thread的参数里,有一个方法, runx(), 在example04里也有一个方法runx().
如果我想在参数里面运行一个Example04的runx()方法,那么我就要创建一个新的example04的实例,但是改变新的实例的button的颜色,不会改变现在这个applet的button的颜色。

所以想请问一下,如何在调用类外类的 同名 方法,但是是在当前的实例中改变呢?(如何调用example04的runx()的方法,改变applet的button的颜色)
...全文
227 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_36219416 2016-09-23
  • 打赏
  • 举报
回复
引用 2 楼 lonrence 的回复:
每个内部类都包含了一个外部类的引用 这个引用可以表示为Example04.this 可以这样调用 Example04.this.runx();
再多问一句,如果是外部类想引用内部类的话,这个引用为什么呢?
qq_36219416 2016-09-23
  • 打赏
  • 举报
回复
引用 3 楼 u014037219 的回复:
把你要操作的对象引用传到你要执行的线程中。如果你开这个线程的目的仅仅是为了去设置当前对象背景色,我不知道为什么要开这个线程。
我只是想试试thread这个类,在学习而已。
qq_36219416 2016-09-23
  • 打赏
  • 举报
回复
引用 2 楼 lonrence 的回复:
每个内部类都包含了一个外部类的引用 这个引用可以表示为Example04.this 可以这样调用 Example04.this.runx();
高人!!正解!!!
  • 打赏
  • 举报
回复
把你要操作的对象引用传到你要执行的线程中。如果你开这个线程的目的仅仅是为了去设置当前对象背景色,我不知道为什么要开这个线程。
  • 打赏
  • 举报
回复
每个内部类都包含了一个外部类的引用 这个引用可以表示为Example04.this 可以这样调用 Example04.this.runx();
  • 打赏
  • 举报
回复
等待中。。。。

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧