Java 小程序无法清除屏幕上的文本
Java Applet failing to clear text onscreen
所以我在下面有这段代码,我正在尝试刷新屏幕上的文本以显示当前时间。到目前为止,我已经尝试使用 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 已经解释了最重要的事情,但我会在我的回答中重复它们以便更完整
- 覆盖
paintComponent()
,而不是 paint()
- 创建一个新的 JPanel class 为您绘画
- 在你的
paintComponent()
方法中获取时间,否则它将始终保持不变
- 使用计时器而不是休眠事件调度线程
- 调用
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);
}
}
}
所以我在下面有这段代码,我正在尝试刷新屏幕上的文本以显示当前时间。到目前为止,我已经尝试使用 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 已经解释了最重要的事情,但我会在我的回答中重复它们以便更完整
- 覆盖
paintComponent()
,而不是paint()
- 创建一个新的 JPanel class 为您绘画
- 在你的
paintComponent()
方法中获取时间,否则它将始终保持不变 - 使用计时器而不是休眠事件调度线程
- 调用
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);
}
}
}