gpt4 book ai didi

java - 在数组中创建 JButton 时出错

转载 作者:行者123 更新时间:2023-12-01 08:13:35 26 4
gpt4 key购买 nike

我正在编写一个程序,该程序将有一个 9x9 按钮,每个按钮从左上角开始向右移动,从 1 到 81

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MS extends JFrame implements ActionListener {

static JFrame bframe;
static JPanel p;

public MS() {
p = new JPanel(new GridLayout(9,9));
private static JButton[][] jgo = new JButton[9][9];
int count = 1;
for(int row=0; row < 9; row++)
for(int col=0; col < col; col++) {
jgo[row][col] = new JButton("%d",count);
p.add(jgo[row][col]);
count++;
}
}

public static void main(String[] args) {
bframe=new MS(); //CREATE me and
bframe.add(p); //add the JPanel

bframe.setSize(810,810);
bframe.setLocation(0,0); //where my upper left hand corner goes
bframe.setVisible(true); //I start out invisible
bframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //need this for the window manager
}
}

我的错误发生在我的构造函数中,它与我如何制作按钮和设置值有关。

最佳答案

这里是 JButton 类的构造函数列表

JButton()
Creates a button with no set text or icon.
JButton(Action a)
Creates a button where properties are taken from the Action supplied.
JButton(Icon icon)
Creates a button with an icon.
JButton(String text)
Creates a button with text.
JButton(String text, Icon icon)
Creates a button with initial text and an icon.

没有任何一个像您正在使用的那样。

JButton("%d",count); // JButton(String,int); or JButton(format,int);

您可以使用

JButton(""+count)://JButton(String text)

关于java - 在数组中创建 JButton 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15350536/

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