gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:24:24 25 4
gpt4 key购买 nike

所以我在下面有这段代码,我正在尝试刷新屏幕上的文本以显示当前时间。到目前为止,我已经尝试使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com