Java 使用GridBagLayout使用按钮填充框架
我试图建立一个匹配的游戏,每个按钮上都有图标。虽然,这还没有接近完成,但我在用按钮填充面板时遇到了一个问题 通过这个代码,我得到了一个灰色的框架。如果我在“//execution”下注释掉我使用的3种方法,面板将全部为黑色(这就是我测试按钮是否填充空间的方式) 由于某些原因,n my按钮没有填充到面板上。Java 使用GridBagLayout使用按钮填充框架,java,swing,jframe,jpanel,jbutton,Java,Swing,Jframe,Jpanel,Jbutton,我试图建立一个匹配的游戏,每个按钮上都有图标。虽然,这还没有接近完成,但我在用按钮填充面板时遇到了一个问题 通过这个代码,我得到了一个灰色的框架。如果我在“//execution”下注释掉我使用的3种方法,面板将全部为黑色(这就是我测试按钮是否填充空间的方式) 由于某些原因,n my按钮没有填充到面板上。 我需要一些帮助!!!我哪里做错了 import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.Grid
我需要一些帮助!!!我哪里做错了
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MemoryMainFrame extends JFrame implements ActionListener {
JButton button[] = new JButton[16];
private static final int SIXTEEN_BUTTONS = 16;
JPanel mainPanel = new JPanel();
double dim = Math.sqrt(SIXTEEN_BUTTONS);
int numOfColumns = (int) (dim);
int numOfRows = (int) (dim);
public static void main(String[] args) {
new MemoryMainFrame();
}
public MemoryMainFrame() {
this.setTitle("MemoryGame!!!");
this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
mainPanel.setBackground(Color.BLACK);
mainPanel.setLayout(new GridBagLayout());
// execution
FillButtonArray(SIXTEEN_BUTTONS);
AddButtonListener(SIXTEEN_BUTTONS);
ButtonToPanel(SIXTEEN_BUTTONS);
this.add(mainPanel);
}
public void FillButtonArray(int numOfButtons) {
int i = 0;
while (i < numOfButtons) {
button[i] = new JButton("asdf");
}
}
public void AddButtonListener(int numOfButtons) {
int i = 0;
while (i < numOfButtons) {
button[i].addActionListener(this);
}
}
public void ButtonToPanel(int numOfButtons) {
int n = 0;
GridBagConstraints gbc = new GridBagConstraints();
for (int i = 0; i < numOfColumns; i++) {
for (int j = 0; j < numOfRows; j++) {
gbc.gridx = i;
gbc.gridy = j;
n++;
button[n].setBorder(BorderFactory.createLineBorder(
Color.DARK_GRAY, 2));
mainPanel.add(button[n]);
}
}
}
public void actionPerformed(ActionEvent arg0) {
JFrame j = new JFrame();
j.setSize(300, 300);
j.setVisible(true);
}
}
导入java.awt.Color;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.BorderFactory;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类MemoryInframe扩展JFrame实现ActionListener{
JButton按钮[]=新JButton[16];
专用静态最终int十六个按钮=16;
JPanel mainPanel=新的JPanel();
双尺寸=数学sqrt(十六个按钮);
int numOfColumns=(int)(dim);
int numorrows=(int)(dim);
公共静态void main(字符串[]args){
新的MemoryMainFrame();
}
公共存储器名称(){
这个.setTitle(“MemoryGame!!!”);
这个。设置大小(400400);
此.setLocationRelativeTo(空);
此.setDefaultCloseOperation(关闭时退出);
此.setVisible(true);
主面板。立根背景(颜色。黑色);
setLayout(新的GridBagLayout());
//执行
FillButtonArray(十六个按钮);
AddButtonListener(十六个按钮);
按钮面板(十六个按钮);
添加(主面板);
}
公共void FillButtonArray(int numOfButtons){
int i=0;
while(i
我使用“asdf”作为测试,看看按钮是否也能正常工作
此外,执行的操作也是一项测试。这部分代码是不相关的 您正在创建GridBagConstraints,但没有使用它们 更改此项:
mainPanel.add(button[n]);
为此:
// passes both the component and the GBC into the container
mainPanel.add(button[n], gbc);
编辑
这里还有一个永无止境的循环:
while (i < numOfButtons) {
button[i] = new JButton("asdf");
}
您正在创建GridBagConstraints,但没有使用它们 更改此项:
mainPanel.add(button[n]);
为此:
// passes both the component and the GBC into the container
mainPanel.add(button[n], gbc);
编辑
这里还有一个永无止境的循环:
while (i < numOfButtons) {
button[i] = new JButton("asdf");
}
我不得不承认。我真傻,但这不是问题所在。我一直在GridLayout()和GridBagLayout()之间切换,但两者都无法正常工作。我忘了添加约束条件。但是好球!谢谢这是一个很好的展示方式!我喜欢你只在contentPane()中添加内容的方式。我必须承认。我真傻,但这不是问题所在。我一直在GridLayout()和GridBagLayout()之间切换,但两者都无法正常工作。我忘了添加约束条件。但是好球!谢谢这是一个很好的展示方式!我喜欢您添加到contentPane()的方式。
public void actionPerformed(ActionEvent arg0){JFrame j=new JFrame();
,这表明应用程序正在使用多个帧。请参见“this.setVisible(true);…this.add(mainPanel);
”应该是“。this.add(mainPanel);this.pack();this.setVisible(true)这可能是问题的根源。@AndrewThompson:确实如此。他还有一个永无止境的while循环。public void actionPerformed(ActionEvent arg0){JFrame j=new JFrame();
这表明应用程序正在使用多个帧。请参阅“this.setVisible(true);…this.add(主面板);
”应该是“.this.add(主面板);this.pack();this.setVisible(true);
”。这可能是问题的根源。@AndrewThompson:的确如此。他还有一个永无止境的while循环。