1

我最近开始使用 java GUI - 最合适的是 Swing。

现在我遇到了这个问题,我无法解决。我想处理一个扩展JPanel和实现ActionListener(某些东西)的游戏板,如下所示:

+----------------+
| Panel1 | Board |
|________|       |
| Panel2 |       |
|        |       |
+----------------+

但我得到了这个:

+----------------+
| Panel1 | Board |
|________|_______|
| Panel2 | EMPTY |
|        |       |
+----------------+

我首先尝试使用内部GridLayout带有 ed 面板的主窗格来执行此操作,BoxLayout但这不起作用。然后我发现GroupLayout,这导致我遇到上述情况,并使我在董事会课程中失去了听众的控制/注意力。

[编辑] 我还尝试更改板的最小和首选尺寸,也没有奏效。

谁能告诉我为什么会这样?

恢复:

  1. 为什么组布局对 Board 面板执行此操作?无论如何我可以修复它吗?
  2. 因为在 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;
            }
        }
    }
}

谢谢你的时间!

4

1 回答 1

3

我在您的代码中看到一件事可能是一个问题,并且可以毫不费力地解决:您正在使用 KeyListeners。这通常应该在 Swing GUI 中避免,相反,您应该尝试使用更灵活且不需要绑定组件具有焦点的键绑定

关于您的 GroupLayout 大小问题,我不得不承认对 GroupLayout 的使用非常薄弱,实际上我尽量避免使用它。要考虑的其他布局包括 GridBagLayout 或 MigLayout。

编辑:
但是,我现在已经阅读了 GroupLayout 教程,包括标记为“强制组件可调整大小(允许缩小和增长):”的部分。当您将组件添加到布局时,您似乎必须添加一些参数,在我的代码示例中如下所示:

.addComponent(board2, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

例如,这是我的代码,显示了根据需要布置的组件,还显示了 KeyBindings 和 PropertyChangeListener 的使用。请注意,通过使用 Key Bindings,焦点不再是一个大问题,因为我不必将 JPanel 设置为可聚焦也不给它焦点:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;

/**
 * uses GroupLayout and Key Bindings
 * @link http://stackoverflow.com/questions/14784773/grouplayout-makes-action-listener-loses-focus
 * @author Pete
 *
 */
@SuppressWarnings("serial")
public class HelloWorldSwing2GroupLayout extends JPanel {
   private JLabel[] labels = {new JLabel("A"), new JLabel("B")};
   private Board2 board2 = new Board2();
   private JLabel directionLabel = new JLabel();

   public HelloWorldSwing2GroupLayout() {
      directionLabel.setHorizontalAlignment(SwingConstants.CENTER);
      board2.add(directionLabel);

      board2.addPropertyChangeListener(new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent pcEvt) {
            if (Board2.DIRECTION.equals(pcEvt.getPropertyName())) {
               Direction dir = (Direction)pcEvt.getNewValue();
               if (dir != null) {
                  directionLabel.setText(dir.getText());
               } else {
                  directionLabel.setText("");
               }
            }
         }
      });

      GroupLayout layout = new GroupLayout(this);
      setLayout(layout);

      int lWidth = board2.getPreferredSize().width;
      int lHeight = board2.getPreferredSize().height / 2;
      Dimension preferredSize = new Dimension(lWidth, lHeight);
      for (JLabel label : labels) {
         label.setHorizontalAlignment(SwingConstants.CENTER);
         label.setVerticalAlignment(SwingConstants.CENTER);
         label.setBorder(BorderFactory.createLineBorder(Color.black));

         // please, please forgive me Jeanette! This is for demo purposes only.
         label.setPreferredSize(preferredSize);
      }

      layout.setAutoCreateGaps(true);
      layout.setAutoCreateContainerGaps(true);
      layout.setHorizontalGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                  .addComponent(labels[0], 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                  .addComponent(labels[1], 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(board2, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      );
      layout.setVerticalGroup(layout.createParallelGroup()
            .addGroup(layout.createSequentialGroup()
                  .addComponent(labels[0], 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                  .addComponent(labels[1], 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(board2, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)      
      );
   }

   private static void createAndShowGui() {
      HelloWorldSwing2GroupLayout mainPanel = new HelloWorldSwing2GroupLayout();

      JFrame frame = new JFrame("HelloWorldSwing2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

@SuppressWarnings("serial")
class Board2 extends JPanel {
   private static final int PREF_W = 200;
   private static final int PREF_H = 400;
   public static final String DIRECTION = "direction";
   private Direction direction = null;

   public Board2() {
      setBorder(BorderFactory.createTitledBorder("Board2"));
      InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
      ActionMap actionMap = getActionMap();

      for (Direction dir : Direction.values()) {
         inputMap.put(dir.getKeyStroke(), dir.getText());
         actionMap.put(dir.getText(), new MyArrowBinding(dir));
      }
   }

   private class MyArrowBinding extends AbstractAction {
      private Direction dir;

      public MyArrowBinding(Direction dir) {
         super(dir.getText());
         this.dir = dir;
         putValue(ACTION_COMMAND_KEY, dir);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         setDirection(dir);
      }
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   public void setDirection(Direction direction) {
      Direction oldValue = this.direction;
      Direction newValue = direction;
      this.direction = newValue;

      firePropertyChange(DIRECTION, oldValue, newValue);
   }

   public Direction getDirection() {
      return direction;
   }
}

enum Direction {
   UP("Up", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0)),
   DOWN("Down", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0)),
   LEFT("Left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0)),
   RIGHT("Right", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0));

   Direction(String text, KeyStroke keyStroke) {
      this.text = text;
      this.keyStroke = keyStroke;
   }
   private String text;
   private KeyStroke keyStroke;

   public String getText() {
      return text;
   }

   public KeyStroke getKeyStroke() {
      return keyStroke;
   }

   @Override
   public String toString() {
      return text;
   }
}

看起来像这样:
在此处输入图像描述

我非常喜欢将 PropertyChangeListeners 用于此类事情,因为它可以轻松解耦您的代码。现在 Board2 类不必担心其他类对其方向的任何变化有何反应。它所要做的就是将这个变化广播给任何正在听它的类,它们每个都按照他们认为合适的方式做出响应。

于 2013-02-09T04:28:35.663 回答