Applet 中 paint() 方法内的无限循环不允许我与显示的按钮交互
Infinite loop inside paint() method in an Applet does not let me interact with the buttons displayed
我想做的是抛出 2 个线程的 Appled,每个线程 运行 一个通过无限循环增加自身的计数器
然后我在 Applet 的 paint() 方法中使用 while(true),它连续绘制计数器,问题是我还有 2 个按钮,每个按钮都旨在停止每个线程,但 paint() 方法中的无限循环没有不要让我点击其中的 none 也不要关闭 Applet 的 window 或任何东西
这里是截图,后面是代码
顺便说一句,我确定问题出在 paint() 循环上,就好像我禁用了循环一样我可以与按钮交互,但计数器显然没有更新,奇怪的是我将鼠标光标放在显示它的按钮采用的形式就像你想调整 windows 的大小时一样,但 imrpant 没有捕获它:/
http://i.imgur.com/PJnDI4u.png
public class MainApplet 扩展 Applet 实现 ActionListener {
private static final long serialVersionUID = -2500043816999861110L;
private Font fuente;
private Button bUno, bDos;
private HiloContador hUno, hDos;
public void init() {
setBackground(Color.LIGHT_GRAY);
fuente = new Font("Verdana",Font.BOLD,26);
bUno = new Button("Parar");
bUno.addActionListener(this);
bDos = new Button("Parar");
bDos.addActionListener(this);
bUno.setSize(40,20);
add(bUno);
bDos.setSize(40,20);
add(bDos);
hUno = new HiloContador(20);
hUno.start();
hDos = new HiloContador(40);
hDos.start();
}
@SuppressWarnings({ "deprecation", "static-access" })
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(bUno)){
hUno.parar();
bUno.setLabel("1 parado");
}else if (e.getSource().equals(bDos)){
hDos.parar();
bDos.setLabel("2 parado");
}
}
public void paint(Graphics g) {
while (true){
g.clearRect(1,1,getSize().width,getSize().height); //dibuja la ventana
g.setFont(fuente);
g.drawString(hUno.getContador()+"",40,60);
g.drawString(hDos.getContador()+"",100,60);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
万一它对任何人有帮助,解决了删除无限循环并添加此方法的问题
Timer timer = new Timer();
timer.schedule( new TimerTask() {
public void run() {
repaint();}
}, 0, 1000);
我想做的是抛出 2 个线程的 Appled,每个线程 运行 一个通过无限循环增加自身的计数器 然后我在 Applet 的 paint() 方法中使用 while(true),它连续绘制计数器,问题是我还有 2 个按钮,每个按钮都旨在停止每个线程,但 paint() 方法中的无限循环没有不要让我点击其中的 none 也不要关闭 Applet 的 window 或任何东西
这里是截图,后面是代码
顺便说一句,我确定问题出在 paint() 循环上,就好像我禁用了循环一样我可以与按钮交互,但计数器显然没有更新,奇怪的是我将鼠标光标放在显示它的按钮采用的形式就像你想调整 windows 的大小时一样,但 imrpant 没有捕获它:/
http://i.imgur.com/PJnDI4u.png
public class MainApplet 扩展 Applet 实现 ActionListener {
private static final long serialVersionUID = -2500043816999861110L;
private Font fuente;
private Button bUno, bDos;
private HiloContador hUno, hDos;
public void init() {
setBackground(Color.LIGHT_GRAY);
fuente = new Font("Verdana",Font.BOLD,26);
bUno = new Button("Parar");
bUno.addActionListener(this);
bDos = new Button("Parar");
bDos.addActionListener(this);
bUno.setSize(40,20);
add(bUno);
bDos.setSize(40,20);
add(bDos);
hUno = new HiloContador(20);
hUno.start();
hDos = new HiloContador(40);
hDos.start();
}
@SuppressWarnings({ "deprecation", "static-access" })
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(bUno)){
hUno.parar();
bUno.setLabel("1 parado");
}else if (e.getSource().equals(bDos)){
hDos.parar();
bDos.setLabel("2 parado");
}
}
public void paint(Graphics g) {
while (true){
g.clearRect(1,1,getSize().width,getSize().height); //dibuja la ventana
g.setFont(fuente);
g.drawString(hUno.getContador()+"",40,60);
g.drawString(hDos.getContador()+"",100,60);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
万一它对任何人有帮助,解决了删除无限循环并添加此方法的问题
Timer timer = new Timer();
timer.schedule( new TimerTask() {
public void run() {
repaint();}
}, 0, 1000);