java出了点小问题 帮忙看一下

importjava.awt.Container;importjava.awt.Graphics;importjavax.swing.JFrame;importjavax... import java.awt.Container;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class ring implements Runnable{
jpenl j = new jpenl();
void buildUI(Container container){

container.add(j);
}
/**
* @param args
*/

static int time=0;
static String mes="time:";

public static void main(String[] args) {
// TODO Auto-generated method stub

JFrame f=new JFrame("ring");

ring controller = new ring();
controller.buildUI(f.getContentPane());
Thread t=new Thread();
f.setSize(500, 500);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
t.start();
String result = JOptionPane.showInputDialog("要休息的分钟数");

try {
time=Integer.parseInt(result)*60;
if(time>0&&time<99999){
mes="time:"+(time / 3600) + ":"
+ (time / 60 - time / 3600 * 60) + ":"
+ (time - time / 60 * 60);
//f.repaint();
}

} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(f, "wrong");
}

}

@Override
public void run() {
// TODO Auto-generated method stub
if(time<=0){
return;
}
try {
Thread.sleep(1000);
time--;
} catch (Exception e) {
// TODO: handle exception
}
mes="time:"+(time / 3600) + ":"
+ (time / 60 - time / 3600 * 60) + ":"
+ (time - time / 60 * 60);
System.out.println(mes);
j.repaint();
}
@SuppressWarnings("serial")
class jpenl extends JPanel{
public jpenl(){

}
public void paint(Graphics g){
super.paint(g);
g.drawString(mes+time, 20, 20);
j.repaint();
}
}
}
不连续显示时间 出什么问题了。。。。
展开
 我来答
bearverygood
2011-09-16 · 超过21用户采纳过TA的回答
知道答主
回答量:48
采纳率:0%
帮助的人:59.3万
展开全部
import java.awt.Container;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Ring implements Runnable{
jpenl j = new jpenl();
void buildUI(Container container){
container.add(j);
}
static int time;
static String mes="剩下秒数:";

public static void main(String[] args) {
JFrame f=new JFrame("ring");
Ring controller = new Ring();
controller.buildUI(f.getContentPane());
Thread t=new Thread(controller);//要把controller作为参数传进Thread里,那样t.start()才会调用到你下面重写的run
f.setSize(500, 500);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
String result = JOptionPane.showInputDialog("要休息的分钟数");
try {
time=Integer.parseInt(result)*60;//下面好像有点逻辑错误,既然是分钟数为什么会除以3600的?我先注释掉,然后我再写个简单的倒计时给你作参考吧
/*if(time>0&&time<99999){
mes="time:"+(time / 3600) + ":"
+ (time / 60 - time / 3600 * 60) + ":"
+ (time - time / 60 * 60);
}*/
t.start();//start应该在这里,这样result才会有值
} catch (Exception e) {
JOptionPane.showMessageDialog(f, "wrong");
}
}
@Override
public void run() {
while(time>=0){//这里应该用循环,不然只会停顿1秒
try {
Thread.sleep(1000);
time--;
} catch (Exception e) {
e.printStackTrace();
}
/*mes="time:"+(time / 3600) + ":"
+ (time / 60 - time / 3600 * 60) + ":"
+ (time - time / 60 * 60);*/
mes="剩下秒数:";
j.repaint();

}
}
@SuppressWarnings("serial")
class jpenl extends JPanel{
public void paint(Graphics g){
super.paint(g);
g.drawString(mes+time, 20, 20);
//j.repaint();//这里不要repaint,否则会陷入死循环
}
}
}

//希望能帮到你哦,楼主
loweryou
2011-09-16 · TA获得超过288个赞
知道小有建树答主
回答量:195
采纳率:75%
帮助的人:134万
展开全部
对线程的操作和swing重画机制不了解!线程t都没有把runnable对象传进去。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
ymiqplgao
2011-09-17 · TA获得超过646个赞
知道小有建树答主
回答量:512
采纳率:0%
帮助的人:393万
展开全部
package ring;

import java.awt.Container;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Ring_old implements Runnable {
static int time = 0;
static String mes = "time:";
MyPanel mp = new MyPanel();

void buildUI(Container container) {
System.out.println("buildUI()...");
container.add(mp);
}

@Override
public void run() {
System.out.println("begin run()...");
while (time > 0 && time < 99999) {
try {
Thread.sleep(200);
time--;
} catch (Exception e) {
}
mes = ComputeTime.showTime(time);
System.out.println(mes);
mp.repaint();
}
System.out.println("...end run()");
return;
}

@SuppressWarnings("serial")
class MyPanel extends JPanel {
public MyPanel() {
System.out.println("Jpenl实例生成...");
}

// 回调方法
public void paint(Graphics g) {
System.out.println("paint()...");
super.paint(g);
g.drawString(mes + time, 20, 20);
}
}

public static void main(String[] args) {
JFrame jf = new JFrame("ring");
jf.setSize(500, 500);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setResizable(false);

Ring_old ro = new Ring_old();
ro.buildUI(jf.getContentPane());

String result = JOptionPane.showInputDialog("要休息的分钟数");
time = Integer.parseInt(result) * 60;
mes = ComputeTime.showTime(time);

ro.run();
}
}
////////////////////////
package ring;

public class ComputeTime {
public static String showTime(int seconds) {
int hour = seconds / 3600;
int minute = (seconds - hour * 3600) / 60;
int second = seconds - hour * 3600 - minute * 60;
return "time(hh:mm ss) = " + hour + ":" + minute + ":" + second
+ " ";
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式