Java 如何将 JPanel 添加到 JFrame?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19916325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a JPanel to a JFrame?
提问by user2980816
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. Below is the code for the game grid. Thanks!
我正在创建一个雷区游戏。我需要在网格下方各自独立的 JPanel 中添加两个按钮 Clear 和 Done,但不知道如何操作。下面是游戏网格的代码。谢谢!
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 camickr
By default a JFrame uses a BorderLayout.
默认情况下,JFrame 使用 BorderLayout。
So currently your MineField class is added to the CENTER of the border layout.
所以目前你的 MineField 类被添加到边框布局的中心。
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 BorderLayoutfor more information and examples to better understand how layout managers work.
阅读 Swing 教程中关于如何使用 BorderLayout 的部分以获取更多信息和示例,以更好地了解布局管理器的工作原理。
回答by Tdorno
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
或者正如昆昆克斯所说
JFrame.add(Component c); //component to jframe
回答by BilalDja
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
希望有帮助,萨拉姆