我最近开始使用 java GUI - 最合适的是 Swing。
现在我遇到了这个问题,我无法解决。我想处理一个扩展JPanel
和实现ActionListener
(某些东西)的游戏板,如下所示:
+----------------+
| Panel1 | Board |
|________| |
| Panel2 | |
| | |
+----------------+
但我得到了这个:
+----------------+
| Panel1 | Board |
|________|_______|
| Panel2 | EMPTY |
| | |
+----------------+
我首先尝试使用内部GridLayout
带有 ed 面板的主窗格来执行此操作,BoxLayout
但这不起作用。然后我发现GroupLayout
,这导致我遇到上述情况,并使我在董事会课程中失去了听众的控制/注意力。
[编辑] 我还尝试更改板的最小和首选尺寸,也没有奏效。
谁能告诉我为什么会这样?
恢复:
- 为什么组布局对 Board 面板执行此操作?无论如何我可以修复它吗?
- 因为在 Board 类里面我做了 setFocusable(true); 为什么它不能在组布局内获得动作/事件?(没有它也可以正常工作)
这是代码:
游戏课
...
public ConstructorOfTheClassThatExtendsJFrame() {
statusbar = new JLabel(" 0");
panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.black));
this.getContentPane().add(panel);
Board board = new Board(this);
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);
//Specify automatic gap insertion:
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
//PANEL 1
col1 = new JPanel();
col1.setBorder(BorderFactory.createLineBorder(Color.black));
//PANEL 3
col3 = new JPanel();
col3.add(statusbar);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(col1)
.addComponent(col3))
.addComponent(board)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(col1)
.addComponent(board))
.addComponent(col3)
);
setSize(400, 400);
setResizable(false);
setTitle("GameOn");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
和我的董事会课程(来自ZetCode)
public Board(Game parent) {
setFocusable(true);
curPiece = new Shape();
timer = new Timer(400, this);
timer.start();
statusbar = parent.getStatusBar();
board = new Tetrominoes[BoardWidth * BoardHeight];
addKeyListener(new TAdapter());
clearBoard();
}
...
class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
if (!isStarted || curPiece.getShape() == Tetrominoes.NoShape) {
return;
}
int keycode = e.getKeyCode();
if (keycode == 'p' || keycode == 'P') {
pause();
return;
}
if (isPaused)
return;
switch (keycode) {
case KeyEvent.VK_LEFT:
tryMove(curPiece, curX - 1, curY);
break;
case KeyEvent.VK_RIGHT:
tryMove(curPiece, curX + 1, curY);
break;
case KeyEvent.VK_DOWN:
//tryMove(curPiece, curX, curY - 1);
oneLineDown();
break;
case KeyEvent.VK_UP:
tryMove(curPiece.rotateRight(), curX, curY);
break;
case KeyEvent.VK_SPACE:
dropDown();
break;
case 'd':
oneLineDown();
break;
case 'D':
oneLineDown();
break;
case KeyEvent.VK_SHIFT:
newPiece();
break;
}
}
[编辑] 经过一些建议,这里是关键事件工作但对齐仍然错误的代码的 sscce 版本:
游戏类(这个案例是 hello world 但仍然是)
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
public class HelloWorldSwing{
private static void createAndShowGUI(){
//Create frame
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.black));
frame.getContentPane().add(panel);
//Create the new group layout
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);
//We specify automatic gap insertion:
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
//PANEL 1
JPanel col1 = new JPanel();
col1.setBorder(BorderFactory.createLineBorder(Color.black));
//PANEL 2
JLabel label2 = new JLabel("Col2");
Board board = new Board(label2, new BorderLayout());
//PANEL 3
JPanel col3 = new JPanel();
JLabel label = new JLabel("Col1");
JLabel label3 = new JLabel("Col3");
col1.add(label);
board.add(label2,BorderLayout.PAGE_END);
col3.add(label3);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(col1)
.addComponent(col3))
.addComponent(board)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(col1)
.addComponent(board))
.addComponent(col3)
);
frame.setSize(300, 400);
frame.setVisible(true);
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
和董事会类:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Board extends JPanel implements ActionListener {
JLabel blabel;
public Board(JLabel label, BorderLayout b) {
super(b);
setFocusable(true);
blabel = label;
addKeyListener(new TAdapter());
}
public void actionPerformed(ActionEvent e) {
}
class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
switch (keycode) {
case KeyEvent.VK_LEFT:
blabel.setText("Left");
break;
case KeyEvent.VK_RIGHT:
blabel.setText("Right");
break;
case KeyEvent.VK_DOWN:
blabel.setText("Down");
break;
}
}
}
}
谢谢你的时间!