简体   繁体   English

如何将JPanel添加到JFrame?

[英]How to add a JPanel to a JFrame?

I am creating a minefield game. 我正在创建一个雷区游戏。 I need to add two buttons, Clear and Done in their own separate JPanel below the grid and cannot figure out how. 我需要在网格下方的各自独立的JPanel中添加两个按钮,“清除”和“完成”,但无法弄清楚如何做。 Below is the code for the game grid. 下面是游戏网格的代码。 Thanks! 谢谢!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MineField extends JPanel implements ActionListener{

    public static void main(String[] args) {
        MineField g = new MineField();
        JFrame frame = new JFrame("Mine Field");
        frame.add(g);
        frame.setSize(400,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    private JButton squares[][];

    public MineField(){
        this.setSize(400,400);
        this.setLayout(new GridLayout(5,5));
        squares = new JButton[5][5];
        buildButtons();
    }

    int [][] num = new int [5][5];

    private void buildButtons(){
        for(int i=0;i<5;i++){
            for(int j=0;j<5;j++){
                squares[i][j] = new JButton();
                squares[i][j].setSize(400,400);
                this.add(squares[i][j]);
            }
        }
    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
    }

}

By default a JFrame uses a BorderLayout. 默认情况下,JFrame使用BorderLayout。

So currently your MineField class is added to the CENTER of the border layout. 因此,当前将您的MineField类添加到边框布局的CENTER中。

If you want another panel on the frame you can use: 如果要在框架上放置另一个面板,可以使用:

JPanel south = new JPanel();
south.add(clearButton);
south.add(doneButton);
frame.add(south, BorderLayout.SOUTH);

Read the section from the Swing tutorial on How to Use BorderLayout for more information and examples to better understand how layout managers work. 阅读Swing教程中有关如何使用BorderLayout的部分, 获取更多信息和示例,以更好地了解布局管理器的工作方式。

We can add components to each other by using the .add() method. 我们可以使用.add()方法将组件彼此添加。

Two practical usages of this would be: 这有两个实际用法:

mainPanel.add(topPanel); //panel to panel

or as Quincunx said 或如Quincunx所说

JFrame.add(Component c); //component to jframe

You should modify your code a litte bit, well you can add those few lines : 您应该稍微修改一下代码,然后可以添加以下几行:

JPanel thePanel = (JPanel)frame.getContentPane(); // this variable will manage the JFrame content

thePanel.setLayout(new BorderLayout()); // BorderLayout to seperat the Frame on 5 section Center, North, South, Est, West

JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.Right)); // this JPanel made to contain the buttons
btnPanel.add(clearBtn);
btnPanel.add(doneBtn);

thePanel.add(g, BorderLayout.CENTER);
thePanel.add(btnPanel, BorderLayout.SOUTH);

hope that helps, Salam 希望有帮助,萨拉姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM