2013-02-03 39 views
0

我想知道是否有任何簡單的方法可以在5秒後從JLabel中刪除內容(文本)。那可能嗎?因爲我在JFrame中有一個JLabel,並且它顯示了我正在編寫的程序中的一些內部錯誤,我希望JLabel將該消息顯示幾秒鐘,然後空白。有任何想法嗎?5秒後從JLabel中刪除文本?

+2

我建議使用計時器。 –

回答

0

有一個簡單的解決方案。

JLabel label = new JLabel("error text");
Thread.sleep(5000);
label.setText("");

希望這有助於!

編輯:如果你不想讓程序凍結5秒,你將不得不把它放在Runnable裏面。

+3

這不會凍結整個程序5秒嗎? –

+0

+1簡單 –

+0

是的,但你可以把它放在一個可運行的,然後它不會凍結。 – acer

7

最簡單的解決方案是使用擺動計時器。它將防止凍結GUI並確保正確的線程訪問(即,在EDT上執行UI修改)。

小演示的例子:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingUtilities; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestLabelDelete { 
    private JFrame frame; 
    private JLabel label; 

    protected void initUI() { 
     frame = new JFrame(TestLabelDelete.class.getSimpleName()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     label = new JLabel("Some text to delete in 5 seconds"); 
     frame.add(label); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     Timer t = new Timer(5000, new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       label.setText(null); 
      } 
     }); 
     t.setRepeats(false); 
     t.start(); 
    } 

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, 
      UnsupportedLookAndFeelException { 
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       TestLabelDelete testLogin = new TestLabelDelete(); 
       testLogin.initUI(); 
      } 

     }); 
    } 

} 
4

使用Timer。請看我的例子。

import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.Timer; 

public class SourceCodeProgram { 

    private static void createAndShowGUI() { 
     // Make sure we have nice window decorations. 
     JFrame.setDefaultLookAndFeelDecorated(true); 

     // Create and set up the window. 
     JFrame frame = new JFrame("HelloWorldSwing"); 
     frame.setMinimumSize(new Dimension(200, 300)); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // Add the ubiquitous "Hello World" label. 
     final JLabel label = new JLabel("Hello World"); 
     frame.getContentPane().add(label); 

     // Display the window. 
     frame.pack(); 
     frame.setVisible(true); 

     Timer timer = new Timer(5000, new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       // Clear text or whatever you want 
       label.setText("New text"); 
      } 
     }); 
     // start Tick-Tack 
     timer.start(); 
    } 

    public static void main(String[] args) { 
     // Schedule a job for the event-dispatching thread: 
     // creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

或者你可以編寫一個單獨的類,它可以清理標籤。

new JLabelCleaner(5, label).startCountdownFromNow(); 

另見:現在

class JLabelCleaner { 

    private JLabel label; 
    private int waitSeconds; 

    public JLabelCleaner(int waitSeconds, JLabel label) { 
     this.label = label; 
     this.waitSeconds = waitSeconds; 
    } 

    public void startCountdownFromNow() { 
     Timer timer = new Timer(waitSeconds * 1000, new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       label.setText(""); 
      } 
     }); 
     timer.start(); 
    } 
} 

,只要你需要它以這種方式,你可以用它

-1

這是很容易做到的。 ..只需創建一個新線程並編寫代碼來清除標籤上的文本,並使該線程休眠5秒,然後啓動它。

import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

public class LabelThread { 
    private JLabel textLabel; 

    public LabelThread() { 
     try { 
      JFrame frame = new JFrame("Label"); 
      frame.setSize(500, 500); 

      textLabel = new JLabel("Hiiii.... Kill me"); 
      frame.setContentPane(textLabel); 

      frame.setVisible(true); 

      MyThread thread = new MyThread(); 
      MyThread.sleep(5000); 
      thread.start(); 
     } catch (InterruptedException ex) { 

     } 
    } 
    class MyThread extends Thread{ 

     @Override 
     public void run() { 
      System.out.print("Running thread"); 
      textLabel.setText(""); 
     } 

} 
public static void main(String args[]){ 
    LabelThread labelThread = new LabelThread(); 
} 
} 
+0

-1。 1)你在主線程而不是EDT啓動你的用戶界面2)它是睡眠的主線程,而不是'MyThread'。 「睡眠」是一個靜態調用。 3)您正在調用EDT之外的'setText',這違反了Swing Thread。 –