- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试让一些 JLabels 在 JPanel 上彼此交换位置,并按下按键上的 GridLayout。
到目前为止:
1. 我只能让它们对鼠标点击使用react。
2. 只能与上面和左边的交换位置。
注意:
1.如果我设置 movePlayer(1) 它就可以工作。并从网格 (8,7) 切换一个 JLabel,其中包含玩家的图像以及其中一个 JLabel 及其上方楼层的图像。然而我的问题是,当我设置 movePlayer(2) 时,它给了我和索引超出范围的异常。
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);
}
上面代码中未显示的内容:
1. MapElement
是许多子类的父类(继承)。
2.loadMap()
从字符数组中读出字符(例如 a,b,c...),用 MapElement 子类的新实例填充数组 myMap(例如 new Player(),new Floor(),new Tree()
)。
3. drawMap()
添加JLabels
到JPanel
根据类别 myMap[i][j]
4.(这两种方法都是通过双 For 循环 i<8, j<7(来填充
GridLayout(8,7)
.
谢谢,对于糟糕的编码和新 watch 示歉意,希望这是清楚的。欢迎提出更好的移动玩家的方法 (JLabel)
在JPanel
上与 GridLayout
。请记住,主要问题是:为什么 (dir == 1)
工作和(dir == 2)
不是吗?!
最佳答案
我不确定我完全理解你的问题,但是,你可以使用一些技巧来更改组件的渲染顺序,例如,你可以更改 componentZOrder
,将更改组件的布局和渲染顺序。
如果您想包含键盘交互,我强烈建议使用 Key Bindings API ,例如...
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();
}
}
}
}
现在,我使用的 Action
可以处理垂直和水平变化,您可能需要考虑使用两个或多个,这可以更好地限制移动,因此如果用户尝试移动到超出开始或行尾,它会限制它们,就目前而言,它允许组件向上或向下流动到下一行
您可能还想查看How to Use Actions
关于java - 尝试使用 GridLayout 在 JPanel 上移动 JLabels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34585343/
我正在尝试使用 NetBeans 在 Java 中制作类似幻灯片的应用程序。 我有一个 JFrame(主窗口),里面有两个用于导航的按钮(后退和下一步),还有一个 JPanel(mainPanel),
新代码 package test; import javax.swing.*; import java.awt.*; public class TestWindow extends JFrame{ /
我是 Java swing 编码新手。我正在尝试将 JPanel 内容复制到一个新的 JPanel,它使用原始 JPanel 的内容来显示。此外,原始 JPanel 内容随着记录的变化而变化。我尝试了
我正在尝试创建一个带有另外两个 JPanel 的 JPanel,但是,当时,当我要显示主要内容时JPanel,它只显示主Jpanel上的第一个Jpanel。 通过以下示例,您将更好地理解这个问题: 我
我正在尝试学习如何使用编码风格在 Java 中完成 GUI 的工作这就是我写的: import java.awt.Container; import java.awt.Panel; import ja
我从 Oracle 教程中得到了一个 JPanel 的例子我看到它使用默认方法关闭窗口 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 我想
我在使用 Java JPanel 时遇到问题。我想将 2 个具有不同布局的 JPanel 放入一个也有布局的 JPanel 中。有可能让它发挥作用吗? BibP() setLayout(new
我试图让一个 JPanel 出现在另一个 JPanel 中。目前,JPanel 位于外部 JFrame 中,并与我的其他 JFrame 一起加载。我希望 JPanel 位于另一个 JPanel 内部,
是否可以在 Java 的一个方法中在一个面板中包含一个面板? 就像,如果我在名为 CreatePanel 的方法中创建了一个 JPanel,我可以在其下面添加另一个吗?我正在尝试在一种方法中添加两个或
我对嵌套的 BoxLayouts 有疑问。 我想构建一个由 2 个子面板组成的 DropDownPanel:顶部的标题和底部的主体。 body 最初是隐藏的。通过单击标题,您可以切换正文的可见性并显示
我需要创建一个在 JFrame 或 JPanel 上显示多个矩形的程序。这是我到目前为止提出的代码: import javax.swing.*; import java.util.Random; im
将站点中的一些图像保存到 ArrayList 后,我试图创建一个 jpanel,它将在带有滚动 Pane 的单独 jpanel 中显示所有这些图像,以便我可以向每个图像添加 Action 事件。然
我正在开发一个简单的注册窗口,它会在 Java 应用程序打开时出现。 这是一个 JFrame,里面有一个 JPanel,它有文本字段、标签,另一个面板也包含文本字段和标签。我的问题是外部面板有背景图像
我有这个界面要创建。我有 JScrollPane 的问题: 我声明了一个带有 Gridlayout(8,1,0,2) 的 JPanel,我希望在这个面板中出现 8 行。 一行是一个JPanel,我设置
我已经创建了 JFreeChart 并将其放入图表面板(按照建议)。我还将它添加到 jPanel 中。我正在使用 jFrame。但是运行程序后我的图表不可见。有谁能帮帮我吗? final JFreeC
我在下面附上了我的代码,我的程序的屏幕截图,以及我希望标签看起来像的图形。我需要我的 JPanel textPanel 出现在 LLP 选项卡上的 JButton 下方。我试图将 textPanel
我想从窗口 (JFrame) 中删除旧的 JPanel 并添加一个新的。我该怎么做呢? 我尝试了以下方法: public static void showGUI() { JFrame fram
我刚刚接触 Java,正在为我的大学类(class)开发一个项目。我正在开发一款《百万富翁》游戏,但我陷入困境。 我有一个 JFrame 类,其中有 2 个面板。第一个是由按钮组成的,第二个是我想通过
所以,我正在制作一个绘画程序,我有一个主要的Paint类,它检测鼠标输入和绘画,还有一个Tools类,它是左侧的工具栏,拥有许多工具,例如画笔大小更改和形状更改。因此,我想向 Tools 类添加一个清
我正在尝试制作一个在 JToggleButton 的帮助下激活的弹出面板。我希望在选择 ToggleButton 时将 JPanel 添加到另一个 Jpanel 上,并在取消选择 ToggleButt
我是一名优秀的程序员,十分优秀!