【问题标题】:Adding toolbar to GUI in JAVA在 JAVA 中将工具栏添加到 GUI
【发布时间】:2016-04-19 15:07:48
【问题描述】:

我正在研究国际象棋游戏。我有 JFrame 我有容器,其中包括 JLabel 字段的二维数组。现在我想在我的窗口中添加一个工具栏,但是当我想这样做时,我的棋盘就会崩溃。这是一张我的带有棋盘的窗口看起来像Window 的图片。我想在窗口顶部添加工具栏,例如我可以在其中保存/加载游戏...等 另外我想在右侧添加一些面板,我可以在其中看到以前的动作。我想知道如何在不破坏棋盘的情况下做到这一点。 这是我的代码:

public class GUI extends JFrame implements Serializable{
 public void initGUI() {
    setTitle("Chess game");
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    ImageIcon img = new ImageIcon("pieceImages/whiteKing.png");
    contentPane = getContentPane();
    GridLayout gridLayout = new GridLayout(8, 8);
    contentPane.setLayout(gridLayout);
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            labels[i][j]=new JLabel(game.getBoard()[i][j].getImagePath());
            contentPane.add(labels[i][j]);
        }
    }
    setIconImage(img.getImage());
    setResizable(false);
    setSize(600, 600);
    setLocationRelativeTo(null);
    setVisible(true);
}

我创建了一些 JPanel 的建议:

public class Toolbar extends JPanel implements Serializable {
   public Toolbar(){
       JButton newGameBtn = new JButton("New game");
       JButton regretBtn = new JButton("Regret move");
       add(newGameBtn);
       add(regretBtn);
   }
}

【问题讨论】:

  • 你能包含一个完整的SSCCE吗?您没有显示将工具栏添加到应用程序的代码。

标签: java user-interface jframe containers


【解决方案1】:

您可以将BorderLayout 设置为内容窗格,将工具栏添加到北边,将另一个包含标签的面板添加到中心。

    setTitle("Chess game");
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    ImageIcon img = new ImageIcon("pieceImages/whiteKing.png");
    contentPane = getContentPane();

    contentPane.setLayout(new BorderLayout());

    Toolbar toolbar = new Toolbar();

    JPanel labelsPanel = new JPanel();

    GridLayout gridLayout = new GridLayout(8, 8);
    labelsPanel.setLayout(gridLayout);
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            labels[i][j]=new JLabel(game.getBoard()[i][j].getImagePath());
            labelsPanel.add(labels[i][j]);
        }
    }

    contentPane.add(toolbar, BorderLayout.NORTH);
    contentPane.add(labelsPanel, BorderLayout.CENTER);

    setIconImage(img.getImage());
    setResizable(false);
    setSize(600, 600);
    setLocationRelativeTo(null);
    setVisible(true);

另请注意,您可以使用 JToolBar ,请参阅 How to Use Tool Bars

【讨论】:

    【解决方案2】:

    您可以使用两个面板,一个用于工具栏,另一个面板作为主游戏面板。

    因此,在您的 GUI 类中,将布局设置为 BorderLayout 并添加带有 NORTH 约束的工具栏面板,并添加带有 CENTER 约束的游戏面板。比如:

    JFrame frame = new JFrame();
                frame.setTitle("Chess game");
                frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                Container contentPane = frame.getContentPane();
                contentPane.setLayout(new BorderLayout());
                JPanel toolbar = new JPanel(); // stablish any layout...
                toolbar.add(new JButton("Save"));
                // add the rest of the buttons...
    
                JPanel game = new JPanel(); // this is your current contentPane
                GridLayout gridLayout = new GridLayout(8, 8);
                game.setLayout(gridLayout);
                game.add(new JButton("Any Component"));
                game.add(new JButton("Any other Component"));
    
                contentPane.add(toolbar, BorderLayout.NORTH);
                contentPane.add(game, BorderLayout.CENTER);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多