java – getContentPane().添加无效?
作者:互联网
我正在做一个我的任务,它的一部分需要我在JFrame中的网格布局中放置一个红色块.之后,用户应该能够通过箭头键更改该红色块的位置.
到目前为止,我已经能够将红色块添加到网格布局中.问题是我无法移动它.
这是我的Main.java.它在下面的Panel.java中调用JFrame:
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main {
public static void main( String[] args ) {
Panel p = new Panel();
p.setSize(870, 780);
p.setVisible(true);
p.setResizable(true);
p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//END main
}//END Main
Panel.java.这是具有KeyListener的代码来移动红色块.但它不起作用.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Panel extends JFrame implements Runnable{
static final int GWIDTH = 200,
GHEIGHT = 200;
static final Dimension gameDim = new Dimension(GWIDTH, GHEIGHT);
Icon bg = new ImageIcon( getClass().getResource("empty.jpg") );
Icon red = new ImageIcon( getClass().getResource("actor.jpg") );
Icon blue = new ImageIcon( getClass().getResource("blue.jpg") );
JLabel[] labels = new JLabel[48];
private int redPoint;
//Player navigation
private int i, j;
public Panel() {
i = 1;
j = 1;
setPreferredSize(gameDim);
setFocusable(true);
requestFocus();
setTitle("LabWork 10");
setLayout(new GridLayout(6, 8, 10, 10));
redPoint = (i*8)+j;
for( int i=0 ; i<48 ; i++)
labels[i] = new JLabel(bg);
labels[redPoint] = new JLabel(red);
for( int i=0 ; i<48 ; i++ )
getContentPane().add(labels[i]);
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if( e.getKeyCode() == KeyEvent.VK_LEFT ) {
j--;
labels[redPoint] = new JLabel(bg);
getContentPane().add(labels[redPoint]);
redPoint = (i*8)+j;
labels[redPoint] = new JLabel(red);
getContentPane().add(labels[redPoint]);
}
if( e.getKeyCode() == KeyEvent.VK_RIGHT ) {
j++;
labels[redPoint] = new JLabel(bg);
getContentPane().add(labels[redPoint]);
redPoint = (i*8)+j;
labels[redPoint] = new JLabel(red);
getContentPane().add(labels[redPoint]);
}
if( e.getKeyCode() == KeyEvent.VK_UP ){
i--;
labels[redPoint] = new JLabel(bg);
getContentPane().add(labels[redPoint]);
redPoint = (i*8)+j;
labels[redPoint] = new JLabel(red);
getContentPane().add(labels[redPoint]);
}
if( e.getKeyCode() == KeyEvent.VK_DOWN ){
i++;
labels[redPoint] = new JLabel(bg);
getContentPane().add(labels[redPoint]);
redPoint = (i*8)+j;
labels[redPoint] = new JLabel(red);
getContentPane().add(labels[redPoint]);
}
}//end keyPressed
});//end addKeyListener
}//END panel
@Override
public void run() {
}
}
KeyListener中的代码无法重新定位红色块的原因是什么?
编辑1:
这是带有工作移动图块的代码.也防止走出界限.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Panel extends JFrame implements Runnable{
static final int GWIDTH = 200,
GHEIGHT = 200;
static final Dimension gameDim = new Dimension(GWIDTH, GHEIGHT);
Icon bg = new ImageIcon( getClass().getResource("empty.jpg") );
Icon red = new ImageIcon( getClass().getResource("actor.jpg") );
Icon blue = new ImageIcon( getClass().getResource("blue.jpg") );
JLabel[] labels = new JLabel[48];
private int redPoint;
//Player navigation
private int i, j;
public Panel() {
i = 1;
j = 1;
setSize(870, 780);
setVisible(true);
setResizable(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(gameDim);
setFocusable(true);
requestFocus();
setTitle("LabWork 10");
setLayout(new GridLayout(6, 8, 10, 10));
setFocusable(true);
requestFocusInWindow();
redPoint = (i*8)+j;
for( int i=0 ; i<48 ; i++)
labels[i] = new JLabel(bg);
labels[redPoint] = new JLabel(red);
for( int i=0 ; i<48 ; i++ )
getContentPane().add(labels[i]);
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT)
j--;
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
j++;
if (e.getKeyCode() == KeyEvent.VK_UP)
i--;
if (e.getKeyCode() == KeyEvent.VK_DOWN)
i++;
run();
//for preventing going out of bounds
int oldRedPoint = redPoint;
int old_i = i;
int old_j = j;
redPoint = (i * 8) + j;
if( redPoint > 47 || redPoint < 0 ) {
redPoint = oldRedPoint;
i = old_i;
j = old_j;
}
labels[oldRedPoint].setIcon(bg);
labels[redPoint].setIcon(red);
}
});//end addKeyListener
}//END panel
@Override
public void run() {
try {
} catch (Exception e) {
// TODO: handle exception
}
}
}
解决方法:
您的KeyListener问题可能是焦点之一,因为KeyListeners仅适用于具有焦点的组件.一种解决方案是通过setFocusable(…)方法使您可以调用组件可聚焦,然后在渲染后将其设置为requestFocusInWindow().
另一个更优雅的解决方案是使用Key Bindings而不是KeyListener,并绑定到适当的InputMap,即用于JComponent.WHEN_IN_FOCUSED_WINDOW的InputMap.那么你就不必担心焦点问题了.
关于如何在这个网站上使用Key Bindings你可以搜索的很多很好的例子,还有一些不是很好的例子,我已经写过了.
编辑
这不是一个焦点问题,我纠正了.进一步分析……
编辑2
我认为您的问题是,当您不应该这样做时,您正尝试将新的JLabel添加到contentPane.相反,你应该在现有的JLabel中交换ImageIcons,因为这就是你想要发生的事情 – 要移动的图像,而不是JLabel.
编辑3
是的,就是这样.你需要简化一些事情.例如:
@Override
public void keyPressed(KeyEvent e) {
int oldRedPoint = redPoint;
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
j--;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
j++;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
i--;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
i++;
}
redPoint = (i * 8) + j;
labels[oldRedPoint].setIcon(bg);
labels[redPoint].setIcon(red);
}
虽然我必须声明,为了记录,我更喜欢Key Bindings给关键的听众.
编辑4
此外,您最好放入代码以防止您的redPoint低于0或高于labels.length.
此外,如果您要问一个类似的问题,感谢发布相关代码,但请发布不依赖于外部资源(如图像)的代码.我必须为您的代码创建自己的图像才能工作:
private Icon createIcon(Color color) {
BufferedImage img = new BufferedImage(IMG_W, IMG_H,
BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.setColor(color);
g.fillRect(0, 0, IMG_W, IMG_H);
g.dispose();
return new ImageIcon(img);
}
标签:java,swing,contentpane,keylistener 来源: https://codeday.me/bug/20190621/1250120.html