我正在使用Java中的Swing应用程序,当按下某个键时我无法更新我的JPanel ...
这是JFrame
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class PixelDraw {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
//System.out.println("Created GUI on EDT? "+
//SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Swing Paint Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new DrawingPanel());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
这是我的小组:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class DrawingPanel extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;
private int scale = 5;
private int bits = 8 * scale;
private int cols = 32;
//Selection rectangle:
private int sX = 0;
private int sY = 0;
private int sWidth = 1;
private int sHeight = 1;
private Color BackFill = new Color(140, 140, 140);
private Color BackBorder = new Color(70, 70, 70);
public DrawingPanel() {
//setBorder(BorderFactory.createLineBorder(Color.red));
addKeyListener(this);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
//On Click:
}
});
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
//On Dragg:
}
});
}
public void keyPressed(KeyEvent evt) {
int keyCode = evt.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
sY = (sY == 0) ? sY : sY-1;
if (evt.isControlDown()) {
sWidth = (sY == 0) ? sWidth : sWidth+1;
}
}
else if (keyCode == KeyEvent.VK_RIGHT) {
sY = (sY == cols) ? sY : sY+1;
if (evt.isControlDown()) {
sWidth = (sY == cols) ? sWidth : sWidth+1;
}
}
else if (keyCode == KeyEvent.VK_UP) {
sX = (sX == 0) ? sX : sX-1;
if (evt.isControlDown()) {
sWidth = (sY == 0) ? sWidth : sHeight+1;
}
}
else if (keyCode == KeyEvent.VK_DOWN){
sX = (sX == cols) ? sX : sX+1;
if (evt.isControlDown()) {
sWidth = (sY == cols) ? sWidth : sHeight+1;
}
}
this.repaint();
}
public void keyReleased(KeyEvent evt) {
}
public void keyTyped(KeyEvent evt) {
}
public Dimension getPreferredSize() {
return new Dimension(800, 800);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//paint Background
for(int x = 0; x <= (cols-1) * bits; x += bits) {
for(int y = 0; y <= (cols-1) * bits; y += bits){
g.setColor(BackBorder);
g.fillRect(x, y, bits, bits);
g.setColor(BackFill);
g.fillRect(x+(1*scale), y+(1*scale), bits-(1*scale), bits-(1*scale));
}
}
//paint selection
g.setColor(Color.red);
g.drawRect(sX*bits, sY*bits, (sWidth*bits)+(scale-1), (sHeight*bits)+(scale-1));
}
}
那么,当按下箭头时它应该做的是移动红色矩形(如选择)...或者当按下ctrl +箭头时增大选择...
答案 0 :(得分:4)
您遇到了KeyListeners的常见问题 - 缺乏组件焦点。要使KeyListener起作用,被侦听的组件必须既可以聚焦又具有用户焦点,对于您的应用程序都不是这样。
快速解决方法是让您的JPanel通过setFocusable(true)
获得焦点,然后通过requestFocusInWindow()
获得焦点。
public DrawingPanel() {
addKeyListener(this);
setFocusable(true);
requestFocusInWindow();
}
但是,只要将另一个组件(例如JButton或文本组件)添加到GUI并窃取焦点,这将失败。更好的长期解决方案是使用密钥绑定。
修改
请查看the Key Bindings Tutorial。此外,如果您使用键绑定搜索此站点以查找我的帖子,您将看到一些正常的示例。 For example