gpt4 book ai didi

java - 使用 GridBagLayout 用按钮填充框架

转载 作者:行者123 更新时间:2023-12-01 07:55:12 24 4
gpt4 key购买 nike

我正在尝试构建一个匹配游戏,每个按钮都附加有图标。虽然这还没有接近完成,但我在用按钮填充面板时遇到了问题。

使用此代码,我得到一个灰色框架。如果我注释掉“//执行”下使用的 3 种方法,面板将全黑(这就是我测试按钮是否填充空间的方式。)

出于某种原因,我的按钮没有填充到面板上。
我需要一些帮助!!!我哪里出错了?

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);
}
}

我使用“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");
}

对于 AddButtonListener(...) 方法也是如此。

您需要使用 for 循环或更改循环内的 i 来解决此问题。

另外,根据 Andrew Thompson 的评论,您在添加所有组件之前将 JFrame 设置为可见。

此外,使用 Math.sqrt 然后将结果转换为 int 是非常危险的,并且有可能获得意外结果。如果需要,只需将边长声明为 8 并计算 int 的平方即可。

有关 GridBagLayout 的示例,请查看:

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial") // avoid extending JFrame if possible
public class MemoryMainPanel extends JPanel {
private static final int ROWS = 8;
private static final Color BACKGROUND = Color.black;
private static final int I_GAP = 5;
private static final Insets BTN_INSETS = new Insets(I_GAP, I_GAP, I_GAP, I_GAP);
private JButton[][] buttons = new JButton[ROWS][ROWS];


public MemoryMainPanel() {
setBackground(BACKGROUND);
setLayout(new GridBagLayout());
for (int row = 0; row < buttons.length; row++) {
for (int col = 0; col < buttons[row].length; col++) {
JButton btn = new JButton(new ButtonAction(row, col));
add(btn, createGbc(row, col));
buttons[row][col] = btn;
}
}
}

private GridBagConstraints createGbc(int y, int x) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.insets = BTN_INSETS;
return gbc;
}

private class ButtonAction extends AbstractAction {
private int row;
private int col;

public ButtonAction(int row, int col) {
super("asdf");
this.row = row;
this.col = col;
}

@Override
public void actionPerformed(ActionEvent e) {
String text = String.format("Column, Row: [%d, %d]", col + 1, row + 1);
Component parentComponent = MemoryMainPanel.this;
String message = text;
String title = "Button Pressed";
int messageType = JOptionPane.PLAIN_MESSAGE;
Icon icon = null;
JOptionPane.showMessageDialog(parentComponent, message, title, messageType, icon);
}
}

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

JFrame frame = new JFrame("MemoryMainPanel");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

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

关于java - 使用 GridBagLayout 用按钮填充框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31281516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com