Java 按键时使用定时器
在过去一周的搜索中,还没有找到任何有用的方法来解决这个问题。当用户按下backspace键时,将保存游戏。我设置它,它在顶部显示一个小框,上面写着“保存…”,我希望它在上面停留大约2秒钟。我可以让它在按下按钮时显示此代码:Java 按键时使用定时器,java,swing,timer,action,Java,Swing,Timer,Action,在过去一周的搜索中,还没有找到任何有用的方法来解决这个问题。当用户按下backspace键时,将保存游戏。我设置它,它在顶部显示一个小框,上面写着“保存…”,我希望它在上面停留大约2秒钟。我可以让它在按下按钮时显示此代码: if (key.save) { font = new Font(null, 0, 16); g.setFont(font); g.setColor(Color.DARK_GRAY); g.fillRect(getWidth() / 2 - 40
if (key.save) {
font = new Font(null, 0, 16);
g.setFont(font);
g.setColor(Color.DARK_GRAY);
g.fillRect(getWidth() / 2 - 40, -1, 80, 35);
g.setColor(Color.BLACK);
g.drawRect(getWidth() / 2 - 40, -1, 80, 35);
g.setColor(Color.white);
g.drawString("Saving..", getWidth() / 2 - 30, 22);
}
但此代码不起作用,它甚至不会在顶部显示框:
if (key.save) {
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font font = new Font(null, 0, 16);
g.setFont(font);
g.setColor(Color.DARK_GRAY);
g.fillRect(getWidth() / 2 - 40, -1, 80, 35);
g.setColor(Color.BLACK);
g.drawRect(getWidth() / 2 - 40, -1, 80, 35);
g.setColor(Color.white);
g.drawString("Saving..", getWidth() / 2 - 30, 22);
}
};
timer = new Timer(0, action);
timer.start();
}
使用伪代码编写,您可以使用以下几个部分构建解决方案:
- 引入一个字段
,该字段设置为saveMessageIsVisible
false
- 内部的密钥处理方法
- 克隆游戏状态
- 启动一个新线程并保存状态
- 在此线程结束时,将
设置为true(并可选择执行失效屏幕)saveMessageIsVisible
- 同时启动一个持续时间为2秒的计时器,然后再次将字段设置为false
- 在绘制中,对字段进行例行检查并绘制保存框
ExecutorService service = Executors.newSingleThreadExecutor();
try {
Runnable r = new Runnable() {
@Override
public void run() {
// task
}
};
Future<?> f = service.submit(r);
f.get(2, TimeUnit.MINUTES); // attempt the task for two minutes
} catch (Exception e) {
//Handele Exception
} finally {
service.shutdown();
}
ExecutorService service=Executors.newSingleThreadExecutor();
试一试{
Runnable r=新的Runnable(){
@凌驾
公开募捐{
//任务
}
};
未来f=服务提交(r);
f、 获取(2,TimeUnit.MINUTES);//尝试执行该任务两分钟
}捕获(例外e){
//韩德尔例外
}最后{
service.shutdown();
}
这是使用可渲染工件列表的基本概念示例,这些工件可以随时间更新
其基本思想是有一个中央绘制循环,负责根据要渲染的内容更新UI的当前状态。这意味着对UI的任何更改都必须经过这个中心循环
这个例子只使用了Swing,但是这个概念应该很容易翻译
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class QuickPaint01 {
public static void main(String[] args) {
new QuickPaint01();
}
public QuickPaint01() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel implements Environment {
private List<Drawable> drawables;
public TestPane() {
drawables = new ArrayList<>(25);
Timer update = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Drawable[] draws = drawables.toArray(new Drawable[drawables.size()]);
for (Drawable drawable : draws) {
if (drawable instanceof Moveable) {
((Moveable)drawable).update(TestPane.this);
}
}
repaint();
}
});
update.setCoalesce(true);
update.setRepeats(true);
update.start();
drawables.add(new Ball());
}
@Override
public void add(Drawable drawable) {
drawables.add(drawable);
}
@Override
public void remove(Drawable drawable) {
drawables.remove(drawable);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Drawable drawable : drawables) {
drawable.draw(this, g2d);
}
g2d.dispose();
}
}
public interface Environment {
public Dimension getSize();
public void add(Drawable drawable);
public void remove(Drawable drawable);
}
public interface Drawable {
public void draw(Environment env, Graphics2D g);
}
public interface Moveable extends Drawable {
public void update(Environment env);
}
public class Ball implements Moveable {
private int radius = 20;
private int x = 0;
private int y = 0;
private int xDelta = 4;
private Shape shape;
public Ball() {
shape = new Ellipse2D.Float(0, 0, radius, radius);
}
@Override
public void update(Environment env) {
x += xDelta;
if (x + radius > env.getSize().width) {
x = env.getSize().width - radius;
xDelta *= -1;
env.add(new Message(env, "<< Bounce", 1));
} else if (x < 0) {
x = 0;
xDelta *= -1;
env.add(new Message(env, "Bounce >> ", 1));
}
y = (env.getSize().height - radius) / 2;
}
@Override
public void draw(Environment env, Graphics2D g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.translate(x, y);
g2d.setColor(Color.BLUE);
g2d.fill(shape);
g2d.dispose();
}
}
public class Message implements Drawable {
private Environment environment;
private String message;
private int delay;
public Message(Environment environment, String message, int delay) {
this.environment = environment;
this.message = message;
this.delay = delay;
Timer timer = new Timer(delay * 1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Message.this.environment.remove(Message.this);
}
});
timer.start();
}
@Override
public void draw(Environment env, Graphics2D g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
FontMetrics fm = g2d.getFontMetrics();
g2d.drawString(message, env.getSize().width - fm.stringWidth(message), 0 + fm.getAscent());
g2d.dispose();
}
}
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.FontMetrics;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.Shape;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.geom.Ellipse2D;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.Timer;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类QuickPaint01{
公共静态void main(字符串[]args){
新的QuickPaint01();
}
公共图书馆01(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel实现环境{
私人名单提款;
公共测试窗格(){
可提取数据=新阵列列表(25);
计时器更新=新计时器(40,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
Drawable[]draws=drawables.toArray(新的Drawable[drawables.size()]);
用于(可绘制:绘制){
if(可移动的可绘制实例){
((可移动)可绘制)。更新(TestPane.this);
}
}
重新油漆();
}
});
update.setCoalesce(true);
update.setRepeats(true);
update.start();
添加(新球());
}
@凌驾
公共空白添加(可提取){
可提取。添加(可提取);
}
@凌驾
公共空间移除(可提取){
可抽式。移除(可抽式);
}
@凌驾
公共维度getPreferredSize(){
返回新维度(200200);
}
@凌驾
受保护组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g.create();
用于(可提取:可提取){
可绘制。绘制(本,g2d);
}
g2d.dispose();
}
}
公共接口环境{
公共维度getSize();
公共作废增加(可提取);
公共空间移除(可提取);
}
公共接口可绘制{
公共空间图纸(环境环境、图形2D g);
}
公共接口可移动扩展可绘制{
公共空间更新(环境环境);
}
公共类球具可移动{
私有整数半径=20;
私有整数x=0;
私有整数y=0;
私有int xDelta=4;
私人造型;
公共舞会{
形状=新的椭圆2d.浮动(0,0,半径,半径);
}
@凌驾
公共空间更新(环境环境){
x+=xDelta;
如果(x+radius>env.getSize().width){
x=env.getSize().width-radius;
xDelta*=-1;
环境添加(新消息(env,“>”,1));
}
y=(env.getSize().height-radius)/2;
}
@凌驾
公共空间绘制(环境环境、图形2D g){