Java Applet 无法清除屏幕上的文本

标签 java swing applet

所以我在下面有这段代码,我正在尝试刷新屏幕上的文本以显示当前时间。到目前为止,我已经尝试使用drawRect作为清除的方法,但文本会保留在那里,而不是更新。 super.paint 也不起作用。另外,我是否可以将该新语句合并到 init() 或 Paint() 方法中?

import java.awt.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.awt.*;
import java.awt.font.*;

import javax.swing.*;

import java.awt.geom.*;
public class HelloWorldApplet extends javax.swing.JApplet {
Calendar now = Calendar.getInstance();
int second = now.get(Calendar.SECOND);
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);
String greeting;
String currentTime = ("The time currently is: " + hour + ":" + minute + " and" + second + " seconds");
String currentDate = ("Date: " + month + "/" + day + "/" + year);


public void init() {
    greeting = "Hello World";
     Font myFont = new Font("TimesRoman", Font.BOLD, 20);
     // set the component or graphics object like this:
     setFont(myFont);

}
public void paint(Graphics screen){

        Graphics2D screen2D = (Graphics2D) screen;
        screen2D.drawString(greeting, 20, 50);
        screen2D.drawString(currentTime, 20, 75);
        screen2D.drawString(currentDate, 20, 100);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        //DoNothing. Pretend it didnt happen...
        } 




}
public void setup(Graphics screen) {
    for (int x = 1; x < 2000000000;) {
        x++;
        repaint();

}
}

}

最佳答案

@trashgod 已经解释了最重要的事情,但我会在我的回答中重复它们,以便更完整

<小时/>
  1. 重写paintComponent(),而不是paint()
  2. 创建一个新的 JPanel 类来为您进行绘画
  3. paintComponent() 方法中获取时间,否则它将始终保持不变
  4. 使用计时器而不是 hibernate 事件调度线程
  5. 调用super.paintComponent()来清屏

所以这是固定代码:

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

import javax.swing.JPanel;
import javax.swing.Timer;


public class HelloWorldApplet extends javax.swing.JApplet {

    String greeting;

    Font myFont = new Font("Times New Roman", Font.BOLD, 20);

    public void init() {

        greeting = "Hello World";

        add(new MyPanel());

        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                repaint();

            }       
        };

        Timer timer = new Timer(500, actionListener);
        timer.start();


    }

    class MyPanel extends JPanel {

        public void paintComponent(Graphics screen) {

            super.paintComponent(screen);

            Calendar now = Calendar.getInstance();
            int second = now.get(Calendar.SECOND);
            int hour = now.get(Calendar.HOUR_OF_DAY);
            int minute = now.get(Calendar.MINUTE);
            int month = now.get(Calendar.MONTH) + 1;
            int day = now.get(Calendar.DAY_OF_MONTH);
            int year = now.get(Calendar.YEAR);

            String currentTime = ("The time currently is: " + hour + ":" + minute
                    + " and " + second + " seconds");
            String currentDate = ("Date: " + month + "/" + day + "/" + year);

            setFont(myFont);

            Graphics2D screen2D = (Graphics2D) screen;
            screen2D.drawString(greeting, 20, 50);
            screen2D.drawString(currentTime, 20, 75);
            screen2D.drawString(currentDate, 20, 100);

        }

    }


}

关于Java Applet 无法清除屏幕上的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32379232/

相关文章:

java - 使用 XPATH 从特定范围的重复节点中进行选择

java - 调用 DISPOSE_ON_CLOSE 时启动函数

java - 如何将 e.getValueIsAdjusting 与 JTable 的行和列上的监听器一起使用?

java - 无法在 Eclipse 中关闭小程序

java - 如何解决依赖于 WEB-INF/lib 中已有类的插件所需的 Tomcat Web 应用程序中的 JAR 冲突?

java - 如何在 spring 中将集合对象绑定(bind)到 Controller

java - JTable:检测单元格数据变化

java - log4J 与 java applet 和 java 插件2

java - 如何学习游戏开发?

java - 如何让EntityManager在DAO Factory中正常工作?