用长时间运行的任务结果反复更新JLabel

我正在编写一个不断ping服务器的程序。 我写了代码来检查一次,然后把这个ping放在一个JLabel并把它放在一个名为setPing()的方法中。

这是我的代码

 private void formWindowOpened(java.awt.event.WindowEvent evt) { setPing(); } 

这工作,但只做了一次,所以我做了:

 private void formWindowOpened(java.awt.event.WindowEvent evt) { for(;;){ setPing(); } } 

但这并不是第一次。

我没有把setPing方法,因为它太长,所以这里是:

 public String setPing(){ Runtime runtime = Runtime.getRuntime(); try{ Process process = runtime.exec("ping lol.garena.com"); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { int i = 0; i = line.indexOf("Average"); if(i > 0){ String finalPing = ""; line.toCharArray(); try { finalPing = ""; for(int x = i; x < i + 17; x++) { finalPing = finalPing + (line.charAt(x)); } }catch(IndexOutOfBoundsException e) { try { finalPing = ""; for(int x = i; x < i + 16; x++) { finalPing = finalPing + (line.charAt(x)); } }catch(IndexOutOfBoundsException f) { try { finalPing = ""; for(int x = i; x < i + 15; x++) { finalPing = finalPing + (line.charAt(x)); } }catch(IndexOutOfBoundsException g){} } } String final1Ping = finalPing.replaceAll("[^0-9]", ""); return final1Ping; } } }catch(IOException e){ } return ""; } 

更新万一这是很重要的,我使用netbeans。 我创build了一个窗体,并将这个代码放在formWindowOpened evt中,而不是在main中调用它:

 private void formWindowOpened(java.awt.event.WindowEvent evt) { ActionListener timerListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new PingWorker().execute(); } }; Timer timer = new Timer(1000, timerListener); timer.start(); jLabel1.setText(label.getText()); timer.stop(); // TODO add your handling code here: } class PingWorker extends SwingWorker { int time; @Override protected Object doInBackground() throws Exception { time = pingTime("lol.garena.com"); return new Integer(time); } @Override protected void done() { label.setText("" + time); } }; public JComponent getUI() { return label; } public static int pingTime(String hostnameOrIP) { Socket socket = null; long start = System.currentTimeMillis(); try { socket = new Socket(hostnameOrIP, 80); } catch (IOException ex) { ex.printStackTrace(); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { } } } long end = System.currentTimeMillis(); return (int) (end - start); } 

使用Swing Timer重复执行任务,并使用SwingWorker执行长时间运行的任务。 EG的两个下面 – 它使用一个Timer来重复执行一个“长时间运行”任务(一个ping)在一个SwingWorker

有关事件调度​​线程的更多详细信息,请参阅Swing中的并发性,并在GUI中执行长时间运行或重复任务。

此代码使用基于Swing的Timer使用从重复任务(随时间重复更新JLabel )调用的SwingWorker组合使用长时间运行的任务(“pinging”服务器)。

 import java.awt.event.*; import javax.swing.*; import java.net.Socket; public class LabelUpdateUsingTimer { static String hostnameOrIP = "stackoverflow.com"; int delay = 5000; JLabel label = new JLabel("0000"); LabelUpdateUsingTimer() { label.setFont(label.getFont().deriveFont(120f)); ActionListener timerListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new PingWorker().execute(); } }; Timer timer = new Timer(delay, timerListener); timer.start(); JOptionPane.showMessageDialog( null, label, hostnameOrIP, JOptionPane.INFORMATION_MESSAGE); timer.stop(); } class PingWorker extends SwingWorker { int time; @Override protected Object doInBackground() throws Exception { time = pingTime(); return new Integer(time); } @Override protected void done() { label.setText("" + time); } }; public static int pingTime() { Socket socket = null; long start = System.currentTimeMillis(); try { socket = new Socket(hostnameOrIP, 80); } catch (Exception weTried) { } finally { if (socket != null) { try { socket.close(); } catch (Exception weTried) {} } } long end = System.currentTimeMillis(); return (int) (end - start); } public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { new LabelUpdateUsingTimer(); } }; SwingUtilities.invokeLater(r); } } 

你可以使用一个线程 。 问题是你正在阻塞主线程,从而阻止你的程序。 为了解决这个问题,请启动后台Thread来重复更新组件。

(注意:你需要更新EDT上的GUI组件,所以使用SwingUtilities.invokeLater )

 (new Thread((new Runnable(){ @Override public void run(){ while(true){ SwingUtilities.invokeLater(new Runnable(){ @Override public void run(){ refToJLabel.setText(Math.random()); } }); } } }))).start(); 

创build一个定期更新你的JLabel的线程。