gpt4 book ai didi

java - 按下某个键时重新绘制 JPanel。 ( java )

转载 作者:行者123 更新时间:2023-11-29 03:21:34 24 4
gpt4 key购买 nike

我正在使用 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+箭头时扩大选区...

最佳答案

您遇到了 KeyListeners 的一个常见问题——缺少组件焦点。要使 KeyListener 起作用,被监听的组件必须既可聚焦又具有用户焦点,这两者对于您的应用程序都是不正确的。

一个快速解决方法是通过 setFocusable(true) 让您的 JPanel 可聚焦,然后通过 requestFocusInWindow() 获得焦点。

public DrawingPanel() {
addKeyListener(this);
setFocusable(true);
requestFocusInWindow();
}

但是一旦将另一个组件(例如 JButton 或文本组件)添加到您的 GUI 并窃取了焦点,这就会失败。更好的长期解决方案是使用键绑定(bind)。


编辑

请查看the Key Bindings Tutorial .此外,如果您在该网站上搜索我的带有键绑定(bind)的帖子,您会看到一些不错的示例。 For example .

关于java - 按下某个键时重新绘制 JPanel。 ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23325568/

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