循环问题 while(true){ try{ Thread.sleep(1000); }catch(InterruptedException d){return;} 在代码中有这个 运行后button按钮就不能被按动了 怎么解决 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 while(true)//你这一致在休眠,执行个球啊,死循环 就是用一個button让他退出循环 可是点不了啊 是循环导致的 怎么解决呀 就是要求循环下去啊 等到按了button就退出 可是按不了啊 你需要定义一个变量,例如:static boolean isStart = true;while(isStart){ //...}然后点击按钮的时候把isStart设置为false 或者在循环里面做判断,一旦button被点击了,则break; 代碼有點长 就贴关键的一部分吧public void actionPerformed(ActionEvent e) { if(e.getSource() == button1){x=a; y=b; z=c; isStart = true; } if(e.getSource()==button2){isStart = false;System.exit(0);} repaint(); } public void paint(Graphics g) {super.paint(g); while(isStart){ try{ Thread.sleep(1000); }catch(InterruptedException d){return;} 順便問下 怎麼在paint中设置背景色 再增加一个类单独实现Runable接口,然后把这个类的实例作一个全局变量。再写两个方法,一个用来启动线程,一个用来stop线程。这样你用两个Button可以控制一个变量。 以前學java還沒接觸過多線程呢 在GUI线程里循环的时候会导致GUI假死,因为线程陷在循环里无法对其他GUI操作做出反应。这种情况应该用线程实现。将进入循环以及退出循环的处理都放到另起的一个线程里运行,这样GUI不会假死。 背景颜色的设置:方法public Color(int r,int g,int b)Color color = new Color(188,100,100);r,g,b,的值在0到255之间。设置背景颜色方法,变量.setForeground(Color.RED); 你可以这么做设两个按钮,button1,button2,static boolean falg =false;while(false){...}因为一开始是错的不会执行,button1为启动按钮,点了就启动,button2为关按钮。点后关闭 if(e.getSource() == button1){x=a; y=b; z=c; isStart = true; }if(e.getSource()==button2){isStart = false;System.exit(0);}repaint();放在sleep下面。如果点bt1你就终止线程, 我開始繼承了一個Frame 就不能繼承Thread了 java只能繼承一個父類 怎麼解決呢 界面程序需要界面程序的线程处理方式, 在单击按钮时 传入结束循环的标志即可,+ 167765087 java开发技术交流 who can help me? 楼主这样试试开启两个线程其中一个while(flag){另一个......flag=false; 这种需要启动后循环的肯定需要多线程处理。点击开始按钮启动一个线程让他while循环,当点击结束按钮的时候把while的参数改成false就可以了点击按钮时(new BackThread()).start();class BackThread extends Thread{ public void run(){ while(isStart){ //... } }} package org;public class Test { public static boolean isStart = false; public static void main(String[] args) { System.out.println("service is started..."); isStart = true; Thread t = new BackThread(); t.start(); try { Thread.sleep(10000L); } catch (InterruptedException e) { e.printStackTrace(); } isStart = false; t.stop(); System.out.println("service is end..."); }}class BackThread extends Thread{ public void run(){ while(Test.isStart){ try { Thread.sleep(1000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("service is running..."); } }} 恩恩 要控制按鈕啊 怎麼把在第一個class的按鈕值搞到 第二個class中來呀 第二個class是這樣的 没报错 但是按钮还是按不了class BackThread extends Thread implements ActionListener{ public void run(){ while(szswing.isStart){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public void actionPerformed(ActionEvent e) { if(e.getSource()==szswing.button2){System.exit(1);} }} thread dump 代码求指点 求Java 数字转成英文 需要原创 + 注释 急!!!300分 只运行一个java桌面应用程序的实例的问题 字符串转浮点数,但不包括整数,可以不考虑数值范围 买了本书,《Java程序设计教程(Java how to program)第五版》 如何让一个JFrame最小化为系统托盘? 如何用java实现通过串口发短信 发电子邮件一定要用Javamail才能发吗? 请问在applet中如何实现滚动条? 关于java线程 取消任务 JAVA SE GridBagLayout 问题
就是用一個button让他退出循环 可是点不了啊 是循环导致的 怎么解决呀
static boolean isStart = true;
while(isStart){
//...
}
然后点击按钮的时候把isStart设置为false
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){x=a; y=b; z=c; isStart = true; }
if(e.getSource()==button2){isStart = false;System.exit(0);}
repaint();
}
public void paint(Graphics g)
{super.paint(g);
while(isStart){
try{
Thread.sleep(1000);
}catch(InterruptedException d){return;}
背景颜色的设置:方法public Color(int r,int g,int b)
Color color = new Color(188,100,100);
r,g,b,的值在0到255之间。
设置背景颜色方法,变量.setForeground(Color.RED);
while(false){
...
}因为一开始是错的不会执行,button1为启动按钮,点了就启动,button2为关按钮。点后关闭
if(e.getSource()==button2){isStart = false;System.exit(0);}
repaint();
放在sleep下面。
如果点bt1你就终止线程,
其中一个
while(flag){另一个
......
flag=false;
(new BackThread()).start();class BackThread extends Thread{
public void run(){
while(isStart){
//...
}
}}
package org;public class Test {
public static boolean isStart = false;
public static void main(String[] args) {
System.out.println("service is started...");
isStart = true;
Thread t = new BackThread();
t.start();
try {
Thread.sleep(10000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
isStart = false;
t.stop();
System.out.println("service is end...");
}
}class BackThread extends Thread{
public void run(){
while(Test.isStart){
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("service is running...");
}
}
}
class BackThread extends Thread implements ActionListener{
public void run(){
while(szswing.isStart){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==szswing.button2){System.exit(1);}
}}