actionPerformed 仅在计时器启动时运行
actionPerformed only runs when a Timer starts
Sup,希望你一切都好。出于某种原因,在我的 java 代码中,当我按下一个键时(虽然它有效),我的 ActionListener 要求我保持 Timer 启动,否则绘制矩形并显示 JLabel 简单的条件不会 运行因为actionListener不能调用它。
这是工作代码。
package Game;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import java.awt.Image;
import java.awt.Point;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Rectangle;
import javax.swing.Timer;
public class Riven extends JPanel implements ActionListener
{
private keyBindings kb;
private int C_ImageID = 0;
private Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); //Get screen resolution
private Image C_Image;
JLabel mouseX = new JLabel();
private final int DELAY = 10;
private Timer timer;
public Riven()
{
initRiven();
}
private void initRiven()
{
addKeyListener(new TAdapter());
setBackground(Color.black);
loadImage();
setFocusable(true);
kb = new keyBindings(); //set keyBindings
//Configuring the Debug JLabel
mouseX.setFont(new java.awt.Font("Microsoft YaHei UI", Font.PLAIN, 16)); //Set configurations such as Font, and size
mouseX.setText("Test");
mouseX.setOpaque(false);
mouseX.setForeground(Color.white);
add(mouseX);
setLayout(null);
mouseX.setBounds(new Rectangle(new Point(200, 300), mouseX.getPreferredSize()));
mouseX.setLocation(mouseX.WIDTH + 10, 10);
mouseX.setVisible(false);
timer = new Timer(DELAY, this);
timer.start();
}
private void loadImage()
{
String imagedir = showImage.importImage(C_ImageID, size);
ImageIcon ii = new ImageIcon(imagedir);
C_Image = ii.getImage();
}
//Paints
@Override
public void paintComponent(Graphics g)
{
g.drawImage(C_Image, 0, 0, null); //Draw the front frame
paintDBG(g);
}
private void paintDBG(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
if (kb.getDBG()) //If the debug is set to ON
{
//set shape color
g2d.setPaint(Color.black);
//drawing the rectangle
g2d.fillRect(0, 0, size.width / 10 , size.height / 10);
mouseX.setVisible(true);
}
else
{
g2d.dispose();
mouseX.setVisible(false);
}
}
@Override
public void actionPerformed(ActionEvent e)
{
repaint();
}
//Key Bindings
private class TAdapter extends KeyAdapter
{
@Override
public void keyPressed(KeyEvent e)
{
System.out.print("Entrei");
kb.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e)
{
System.out.print("Sai");
kb.keyReleased(e);
}
}
}
这就是导致 actionEvent 不执行的原因
package Game;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import java.awt.Image;
import java.awt.Point;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Rectangle;
import javax.swing.Timer;
public class Riven extends JPanel implements ActionListener
{
private keyBindings kb;
private int C_ImageID = 0;
private Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); //Get screen resolution
private Image C_Image;
JLabel mouseX = new JLabel();
private final int DELAY = 10;
private Timer timer;
public Riven()
{
initRiven();
}
private void initRiven()
{
addKeyListener(new TAdapter());
setBackground(Color.black);
loadImage();
setFocusable(true);
kb = new keyBindings(); //set keyBindings
//Configuring the Debug JLabel
mouseX.setFont(new java.awt.Font("Microsoft YaHei UI", Font.PLAIN, 16)); //Set configurations such as Font, and size
mouseX.setText("Test");
mouseX.setOpaque(false);
mouseX.setForeground(Color.white);
add(mouseX);
setLayout(null);
mouseX.setBounds(new Rectangle(new Point(200, 300), mouseX.getPreferredSize()));
mouseX.setLocation(mouseX.WIDTH + 10, 10);
mouseX.setVisible(false);
//timer = new Timer(DELAY, this);
//timer.start();
}
private void loadImage()
{
String imagedir = showImage.importImage(C_ImageID, size);
ImageIcon ii = new ImageIcon(imagedir);
C_Image = ii.getImage();
}
//Paints
@Override
public void paintComponent(Graphics g)
{
g.drawImage(C_Image, 0, 0, null); //Draw the front frame
paintDBG(g);
}
private void paintDBG(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
if (kb.getDBG()) //If the debug is set to ON
{
//set shape color
g2d.setPaint(Color.black);
//drawing the rectangle
g2d.fillRect(0, 0, size.width / 10 , size.height / 10);
mouseX.setVisible(true);
}
else
{
g2d.dispose();
mouseX.setVisible(false);
}
}
@Override
public void actionPerformed(ActionEvent e)
{
repaint();
}
//Key Bindings
private class TAdapter extends KeyAdapter
{
@Override
public void keyPressed(KeyEvent e)
{
System.out.print("Entrei");
kb.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e)
{
System.out.print("Sai");
kb.keyReleased(e);
}
}
}
您似乎在问为什么除非您打开 Swing 计时器,否则永远不会调用 ActionListener 中的代码。
原因是,Swing 库是一个事件驱动的 GUI 库,并且该库具有侦听器 类,当正确连接到您的代码中时,具有响应事件而调用的回调方法在程序 运行 时发生的事件,例如仅在发生按键时调用的 KeyListeners。
ActionListeners也是如此。它们的代码仅在侦听器附加到事件生成器时被调用,并且该生成器生成事件。对于 ActionListener,事件生成器可以是 JButton(通过 addActionListener
),然后在按下 JButton 时调用侦听器代码,或者它可以附加到 Swing 定时器,当定时器启动时,它会生成每 timer-delay 毫秒重复一次的事件,直到计时器停止。
您的代码的其他问题:
- 始终在您的重写中调用 super 的 paintComponent 方法。否则不会完成内务处理绘画,例如清除脏像素
- 永远不要删除 JVM 提供给您的 Graphics 对象,就像您正在做的那样,因为这会破坏绘画链。只删除您自己创建的一个。
- 您的 paintComponent 方法做的比它应该做的更多,因为它调用
paintDBG(...)
方法来更改 JLabel 的可见性状态。该状态更改应进入计时器的 ActionListener 而不是绘画方法。
Sup,希望你一切都好。出于某种原因,在我的 java 代码中,当我按下一个键时(虽然它有效),我的 ActionListener 要求我保持 Timer 启动,否则绘制矩形并显示 JLabel 简单的条件不会 运行因为actionListener不能调用它。
这是工作代码。
package Game;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import java.awt.Image;
import java.awt.Point;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Rectangle;
import javax.swing.Timer;
public class Riven extends JPanel implements ActionListener
{
private keyBindings kb;
private int C_ImageID = 0;
private Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); //Get screen resolution
private Image C_Image;
JLabel mouseX = new JLabel();
private final int DELAY = 10;
private Timer timer;
public Riven()
{
initRiven();
}
private void initRiven()
{
addKeyListener(new TAdapter());
setBackground(Color.black);
loadImage();
setFocusable(true);
kb = new keyBindings(); //set keyBindings
//Configuring the Debug JLabel
mouseX.setFont(new java.awt.Font("Microsoft YaHei UI", Font.PLAIN, 16)); //Set configurations such as Font, and size
mouseX.setText("Test");
mouseX.setOpaque(false);
mouseX.setForeground(Color.white);
add(mouseX);
setLayout(null);
mouseX.setBounds(new Rectangle(new Point(200, 300), mouseX.getPreferredSize()));
mouseX.setLocation(mouseX.WIDTH + 10, 10);
mouseX.setVisible(false);
timer = new Timer(DELAY, this);
timer.start();
}
private void loadImage()
{
String imagedir = showImage.importImage(C_ImageID, size);
ImageIcon ii = new ImageIcon(imagedir);
C_Image = ii.getImage();
}
//Paints
@Override
public void paintComponent(Graphics g)
{
g.drawImage(C_Image, 0, 0, null); //Draw the front frame
paintDBG(g);
}
private void paintDBG(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
if (kb.getDBG()) //If the debug is set to ON
{
//set shape color
g2d.setPaint(Color.black);
//drawing the rectangle
g2d.fillRect(0, 0, size.width / 10 , size.height / 10);
mouseX.setVisible(true);
}
else
{
g2d.dispose();
mouseX.setVisible(false);
}
}
@Override
public void actionPerformed(ActionEvent e)
{
repaint();
}
//Key Bindings
private class TAdapter extends KeyAdapter
{
@Override
public void keyPressed(KeyEvent e)
{
System.out.print("Entrei");
kb.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e)
{
System.out.print("Sai");
kb.keyReleased(e);
}
}
}
这就是导致 actionEvent 不执行的原因
package Game;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import java.awt.Image;
import java.awt.Point;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Rectangle;
import javax.swing.Timer;
public class Riven extends JPanel implements ActionListener
{
private keyBindings kb;
private int C_ImageID = 0;
private Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); //Get screen resolution
private Image C_Image;
JLabel mouseX = new JLabel();
private final int DELAY = 10;
private Timer timer;
public Riven()
{
initRiven();
}
private void initRiven()
{
addKeyListener(new TAdapter());
setBackground(Color.black);
loadImage();
setFocusable(true);
kb = new keyBindings(); //set keyBindings
//Configuring the Debug JLabel
mouseX.setFont(new java.awt.Font("Microsoft YaHei UI", Font.PLAIN, 16)); //Set configurations such as Font, and size
mouseX.setText("Test");
mouseX.setOpaque(false);
mouseX.setForeground(Color.white);
add(mouseX);
setLayout(null);
mouseX.setBounds(new Rectangle(new Point(200, 300), mouseX.getPreferredSize()));
mouseX.setLocation(mouseX.WIDTH + 10, 10);
mouseX.setVisible(false);
//timer = new Timer(DELAY, this);
//timer.start();
}
private void loadImage()
{
String imagedir = showImage.importImage(C_ImageID, size);
ImageIcon ii = new ImageIcon(imagedir);
C_Image = ii.getImage();
}
//Paints
@Override
public void paintComponent(Graphics g)
{
g.drawImage(C_Image, 0, 0, null); //Draw the front frame
paintDBG(g);
}
private void paintDBG(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
if (kb.getDBG()) //If the debug is set to ON
{
//set shape color
g2d.setPaint(Color.black);
//drawing the rectangle
g2d.fillRect(0, 0, size.width / 10 , size.height / 10);
mouseX.setVisible(true);
}
else
{
g2d.dispose();
mouseX.setVisible(false);
}
}
@Override
public void actionPerformed(ActionEvent e)
{
repaint();
}
//Key Bindings
private class TAdapter extends KeyAdapter
{
@Override
public void keyPressed(KeyEvent e)
{
System.out.print("Entrei");
kb.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e)
{
System.out.print("Sai");
kb.keyReleased(e);
}
}
}
您似乎在问为什么除非您打开 Swing 计时器,否则永远不会调用 ActionListener 中的代码。
原因是,Swing 库是一个事件驱动的 GUI 库,并且该库具有侦听器 类,当正确连接到您的代码中时,具有响应事件而调用的回调方法在程序 运行 时发生的事件,例如仅在发生按键时调用的 KeyListeners。
ActionListeners也是如此。它们的代码仅在侦听器附加到事件生成器时被调用,并且该生成器生成事件。对于 ActionListener,事件生成器可以是 JButton(通过 addActionListener
),然后在按下 JButton 时调用侦听器代码,或者它可以附加到 Swing 定时器,当定时器启动时,它会生成每 timer-delay 毫秒重复一次的事件,直到计时器停止。
您的代码的其他问题:
- 始终在您的重写中调用 super 的 paintComponent 方法。否则不会完成内务处理绘画,例如清除脏像素
- 永远不要删除 JVM 提供给您的 Graphics 对象,就像您正在做的那样,因为这会破坏绘画链。只删除您自己创建的一个。
- 您的 paintComponent 方法做的比它应该做的更多,因为它调用
paintDBG(...)
方法来更改 JLabel 的可见性状态。该状态更改应进入计时器的 ActionListener 而不是绘画方法。