Java 尝试使用GridLayout在JPanel上移动JLabel
我试图让一些jLabel在按下GridLayout键的JPanel上彼此交换位置 到目前为止:Java 尝试使用GridLayout在JPanel上移动JLabel,java,swing,loops,jlabel,Java,Swing,Loops,Jlabel,我试图让一些jLabel在按下GridLayout键的JPanel上彼此交换位置 到目前为止: 1.我只能让他们对鼠标点击做出反应。 2.他们只能在其上方和左侧的位置进行切换 注意: 1.如果我设置movePlayer(1),它会工作。并从网格(8,7)切换一个带有玩家图像的JLabel,其中一个JLabel上面有一个地板图像。然而,我的问题是,当我设置movePlayer(2)时,它会给我和索引bouds异常。 2.这只是我认为与该问题有关的守则的一部分: JPanel myCentreP
1.我只能让他们对鼠标点击做出反应。
2.他们只能在其上方和左侧的位置进行切换 注意:
1.如果我设置movePlayer(1),它会工作。并从网格(8,7)切换一个带有玩家图像的JLabel,其中一个JLabel上面有一个地板图像。然而,我的问题是,当我设置movePlayer(2)时,它会给我和索引bouds异常。
2.这只是我认为与该问题有关的守则的一部分:
JPanel myCentrePanel = new JPanel();
MapElement[][] myMap = new MapElement[8][7];
public TileSwitching(){
super("TileSwitching");
setSize(400,400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
myCentrePanel.setLayout(new GridLayout(8,7));
myCentrePanel.setPreferredSize(new Dimension(5,5));
add(myCentrePanel);
loadMap(level1);
drawMap();
myCentrePanel.setFocusable(true);
myCentrePanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
movePlayer(1);
myCentrePanel.removeAll();
drawMap();
myCentrePanel.revalidate();
}
});
myCentrePanel.setBackground(Color.CYAN);
}
public void movePlayer(int dir){
if(dir == 1){
for(int i = 0; i<8; i++){
for(int j= 0; j<7; j++){
if (myMap[i][j].getClass() == wk.getClass()){
myMap[i-1][j] = new Player();
myMap[i][j] = new Floor();
}
else{
myMap[i][j] = myMap[i][j];
}
}}
if(dir == 2){
for(int i = 0; i<8; i++){
for(int j= 0; j<7; j++){
if (myMap[i][j].getClass() == wk.getClass()){
myMap[i+1][j] = new Player();
myMap[i][j] = new Floor();
}
else{
myMap[i][j] = myMap[i][j];
}
}}
}
public static void main(String[] args) {
TileSwitching tl = new TileSwitching();
TileSwitching.setVisible(true);
}
JPanel myCentrePanel=newjpanel();
MapElement[][]myMap=新的MapElement[8][7];
公用瓷砖切换(){
超级(“瓷砖切换”);
设置大小(400400);
可设置大小(假);
setDefaultCloseOperation(关闭时退出);
myCentrePanel.setLayout(新网格布局(8,7));
myCentrePanel.setPreferredSize(新尺寸(5,5));
添加(myCentrePanel);
负荷图(1级);
drawMap();
myCentrePanel.setFocusable(真);
myCentrePanel.addMouseListener(新的MouseAdapter(){
@凌驾
公共无效mouseClicked(MouseEvent e){
移动播放器(1);
myCentrePanel.removeAll();
drawMap();
myCentrePanel.revalidate();
}
});
菌丝体侧基(颜色:青色);
}
公共无效移动播放器(内部目录){
if(dir==1){
对于(int i=0;i我不确定是否完全理解您的问题,但是,您可以使用一些技巧来更改组件的渲染顺序,例如,您可以更改componentZOrder
,这将更改组件的布局和渲染顺序
如果您想包括键盘交互,我强烈建议使用,例如
导入java.awt.Color;
导入java.awt.EventQueue;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.KeyEvent;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.AbstractAction;
导入javax.swing.Action;
导入javax.swing.ActionMap;
导入javax.swing.InputMap;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.KeyStroke;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
导入javax.swing.border.CompoundBorder;
导入javax.swing.border.EmptyBorder;
导入javax.swing.border.LineBorder;
公开课考试{
公共静态void main(字符串[]args){
新测试();
}
公开考试(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
私人JLabel播放器;
私人名单瓷砖;
公共测试窗格(){
setLayout(新的GridLayout(3,3));
player=makeLabel(“P”);
添加(玩家);
tiles=新阵列列表(8);
对于(int-index=0;index<8;index++){
JLabel tile=makeLabel(“”);
瓷砖。添加(瓷砖);
添加(瓷砖);
}
addKeyBinding(“left”,KeyEvent.VK_left,newmoveAction(-1,0));
addKeyBinding(“right”,KeyEvent.VK_right,newmoveAction(1,0));
addKeyBinding(“up”,KeyEvent.VK_up,newmoveAction(0,-1));
addKeyBinding(“down”,KeyEvent.VK_down,newmoveAction(0,1));
}
受保护的void addKeyBinding(字符串名称、int-keyCode、操作){
InputMap InputMap=getInputMap(在聚焦窗口中时);
ActionMap ActionMap=getActionMap();
inputMap.put(KeyStroke.getKeyStroke(keyCode,0),name);
actionMap.put(名称、动作);
}
受保护的JLabel makeLabel(字符串文本){
JLabel标签=新的JLabel(文本);
标签.订单(新的复合边框)(
新线条边框(颜色为灰色),
新订单(4,4,4,4));
退货标签;
}
公共类MoveAction扩展了AbstractAction{
私人最终int xDelta,yDelta;
公共行动(int xDelta、int yDelta){
this.xDelta=xDelta;
this.yDelta=yDelta;
}
@凌驾
已执行的公共无效操作(操作事件e){
int index=getComponentZOrder(播放器);
指数+=xDelta;
索引+=(yDelta*3);
如果(指数<0){
指数=0;
}else if(索引>=getComponentCount()){
index=getComponentCount()-1;
}
setComponentZOrder(播放器,索引);
重新验证();
重新油漆();
}
}
}
}
现在,<代码>动作< /代码>我使用垂直和水平变化的句柄,您可能需要考虑使用两个或更多个,这可以更好地约束移动,因此如果用户试图移动超出行的开始或结束,它将限制它们,如它现在所代表的,它允许组件向上或向下流动到下一行
哟
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel player;
private List<JLabel> tiles;
public TestPane() {
setLayout(new GridLayout(3, 3));
player = makeLabel("P");
add(player);
tiles = new ArrayList<>(8);
for (int index = 0; index < 8; index++) {
JLabel tile = makeLabel(" ");
tiles.add(tile);
add(tile);
}
addKeyBinding("left", KeyEvent.VK_LEFT, new MoveAction(-1, 0));
addKeyBinding("right", KeyEvent.VK_RIGHT, new MoveAction(1, 0));
addKeyBinding("up", KeyEvent.VK_UP, new MoveAction(0, -1));
addKeyBinding("down", KeyEvent.VK_DOWN, new MoveAction(0, 1));
}
protected void addKeyBinding(String name, int keyCode, Action action) {
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();
inputMap.put(KeyStroke.getKeyStroke(keyCode, 0), name);
actionMap.put(name, action);
}
protected JLabel makeLabel(String text) {
JLabel label = new JLabel(text);
label.setBorder(new CompoundBorder(
new LineBorder(Color.GRAY),
new EmptyBorder(4, 4, 4, 4)));
return label;
}
public class MoveAction extends AbstractAction {
private final int xDelta, yDelta;
public MoveAction(int xDelta, int yDelta) {
this.xDelta = xDelta;
this.yDelta = yDelta;
}
@Override
public void actionPerformed(ActionEvent e) {
int index = getComponentZOrder(player);
index += xDelta;
index += (yDelta * 3);
if (index < 0) {
index = 0;
} else if (index >= getComponentCount()) {
index = getComponentCount() - 1;
}
setComponentZOrder(player, index);
revalidate();
repaint();
}
}
}
}