我想知道是否有任何簡單的方法可以在5秒後從JLabel中刪除內容(文本)。那可能嗎?因爲我在JFrame中有一個JLabel,並且它顯示了我正在編寫的程序中的一些內部錯誤,我希望JLabel將該消息顯示幾秒鐘,然後空白。有任何想法嗎?5秒後從JLabel中刪除文本?
0
A
回答
0
有一個簡單的解決方案。
JLabel label = new JLabel("error text");
Thread.sleep(5000);
label.setText("");
希望這有助於!
編輯:如果你不想讓程序凍結5秒,你將不得不把它放在Runnable裏面。
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。 –
相關問題
- 1. 5秒鐘後刪除SKSpriteNode
- 2. 10秒後移除JLabel
- 3. 5秒後刪除新的div
- 4. 從JLayeredPane刪除JLabel
- 5. 從JLabel中刪除MouseListener()
- 6. 從Wav文件中刪除第一個.5秒
- 7. 5秒後清除innerHTML
- 8. 從jquery腳本中刪除秒
- 9. 從javascript中的變量中刪除5秒
- 10. 從toLocaleTimeString中刪除秒
- 11. 從JLabel中選擇文本?
- 12. 如何在30秒後刪除文件?
- 13. 3秒後刪除類
- 14. 10秒後刪除div
- 15. JLabel文本在不到一秒鐘後消失
- 16. Postgres - 從時間戳文本中刪除毫秒
- 17. 從文本文件中刪除文本
- 18. 從文本文件中刪除文本
- 19. 5秒後刪除內容窗體標記
- 20. jQuery在5秒後隱藏/刪除DIV內容
- 21. 如何在5秒後刪除動態添加的元素?
- 22. 從PDF中刪除文本
- 23. 從文本中刪除
- 24. 從CSS中刪除文本
- 25. 從文本框中刪除文本
- 26. 如何從「/」文本中刪除文本?
- 27. 從複製文本中刪除文本
- 28. 在1秒後在php中刪除
- 29. 從OffsetDateTime刪除分,秒和納秒
- 30. 從JPanel完全刪除JLabel ... not setVisible(False)
我建議使用計時器。 –