使文本在 JPanel 中短暂显示

发布于 2024-10-10 00:17:37 字数 356 浏览 0 评论 0原文

我试图让文本在消失之前短暂出现。它将沿着
的思路 1) 设置颜色为黑色
2) 等待 x 秒
3)设置颜色为背景色

我调用的方法是repaint(),然后调用paintComponent(Graphics Painter)repaint() 仅当我按下空格键时才会被调用。

我想过尝试 repaint();Thread.sleep(1000);repaint(); (我确实捕获了中断异常,只是没有显示),但它只调用 paintComponent 每个空格键一次。

有没有一种简单的方法可以做到这一点,或者这是否有点具有挑战性?

I am trying to make text appear briefly before it disappears. It would be along the lines of
1) Set color to black
2) wait x amount of seconds
3) set color to background color

The method I call is repaint(), which then calls paintComponent(Graphics painter). repaint() is called only if I press the space-bar.

I thought of trying repaint();Thread.sleep(1000);repaint(); (I do catch the Interrupt exception, just not shown), but it only calls paintComponent once per space-bar .

Is there an easy way to do this or is this something that is a bit challenging?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聆听风音 2024-10-17 00:17:37

我将使用 Swing Timer 来安排文本的重新绘制。

另外,我只使用 JLabel 来显示文本。然后,您只需使用 setText(...) 方法根据需要更改文本,组件就会重新绘制自身。

I would use a Swing Timer to schedule the repainting of the text.

Also, I would just use a JLabel to display the text. Then you just use the setText(...) method to change the text as you wish and the component will repaint itself.

同展鸳鸯锦 2024-10-17 00:17:37

您需要重写面板中的paint方法并使其实现Runnable,以便您可以在几秒钟后关闭文本。这是一些示例代码:

import java.awt.Color;
import java.awt.Graphics;
import java.io.Exception;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;   


class MyPanel extends JPanel implements Runnable{

 private final static String TEXT = "HELLO WORLD";
 private boolean on = true;

 @Override
 public void paint(Graphics g) {
  super.paint(g);

  if(on){
   g.drawString(TEXT, 20, 20);
  }

 }

 @Override
 public void run() {
  for(int i = 0 ; i< 2 ; i++){
   paintImmediately(0, 0, getWidth(), getHeight());
   try {
    Thread.sleep(5000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   on = false;
  }
 }
}

public class App {

 public static void main(String[] args) throws Exception {

  JFrame f = new JFrame();
  final MyPanel p =  new MyPanel();
  f.add(p);
  f.setSize(100,100);
  f.setVisible(true);
  Thread t = new Thread(p);
  t.start();
 }
}

You need to override the paint method in your panel and make it implement Runnable so that you can turn off the text after a few seconds. Here is some sample code:

import java.awt.Color;
import java.awt.Graphics;
import java.io.Exception;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;   


class MyPanel extends JPanel implements Runnable{

 private final static String TEXT = "HELLO WORLD";
 private boolean on = true;

 @Override
 public void paint(Graphics g) {
  super.paint(g);

  if(on){
   g.drawString(TEXT, 20, 20);
  }

 }

 @Override
 public void run() {
  for(int i = 0 ; i< 2 ; i++){
   paintImmediately(0, 0, getWidth(), getHeight());
   try {
    Thread.sleep(5000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   on = false;
  }
 }
}

public class App {

 public static void main(String[] args) throws Exception {

  JFrame f = new JFrame();
  final MyPanel p =  new MyPanel();
  f.add(p);
  f.setSize(100,100);
  f.setVisible(true);
  Thread t = new Thread(p);
  t.start();
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文