Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java乘法表GUI,请帮我前进_Java_Swing_Jbutton_Actionlistener - Fatal编程技术网

Java乘法表GUI,请帮我前进

Java乘法表GUI,请帮我前进,java,swing,jbutton,actionlistener,Java,Swing,Jbutton,Actionlistener,这里是第一次使用乘法表,所以请耐心听我说……我想创建一个代码,询问用户想要创建乘法表的行数和列数,但我真的不知道从这里开始该怎么做……有人能帮忙吗?我想了解一些细节…比如…为此在这里创建一个for循环 很抱歉,如果我的代码格式不正确…这是我到目前为止所拥有的..它正确地向用户询问一些行和列,并显示这些行和列..我想这样做,一旦用户在某个交叉点单击按钮,就会显示答案 例如:单击的第一个按钮将显示“1*1=1” 导入java.awt.GridLayout; 导入java.awt.event.Acti

这里是第一次使用乘法表,所以请耐心听我说……我想创建一个代码,询问用户想要创建乘法表的行数和列数,但我真的不知道从这里开始该怎么做……有人能帮忙吗?我想了解一些细节…比如…为此在这里创建一个for循环

很抱歉,如果我的代码格式不正确…这是我到目前为止所拥有的..它正确地向用户询问一些行和列,并显示这些行和列..我想这样做,一旦用户在某个交叉点单击按钮,就会显示答案

例如:单击的第一个按钮将显示“1*1=1”

导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.Scanner;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类网格{
私有整数行数;
私有整数计数;
公共平方网格(整数行计数、整数列计数){
this.rowCount=rowCount;
this.colCount=colCount;
}
JFrame框架;
JButton[]]按钮;
JPanel小组;
私有void createAndShowGui(){
theFrame=新JFrame(“网格”);
按钮=新的JButton[rowCount][colCount];
panel=新的JPanel();
panel.setLayout(新的网格布局(rowCount、colCount));
对于(int rowCounter=0;rowCounter
单击按钮时,您可以通过从事件中获取按钮来判断单击了哪个按钮:

JButton buttonClicked = (JButton) event.getSource();
现在需要做的就是在2D按钮数组中找到该按钮的行和列,并计算乘法

PS:您甚至不必使用
event.getSource()
来获取按钮,因为您已经有了引用此按钮的
j
变量

编辑:行和列可从外部变量获得,只需将其设为最终变量:

for(int rowCounter = 0; rowCounter < rowCount; rowCounter++)
    for(int colCounter = 0; colCounter < colCount; colCounter++){
        final JButton j = new JButton("not clicked");
        final int row = rowCounter;
        final int col = colCounter;

        // now you can use row and col inside your listener.
for(int-rowCounter=0;rowCounter
单击按钮时,您可以通过从事件中获取按钮来判断单击了哪个按钮:

JButton buttonClicked = (JButton) event.getSource();
现在需要做的就是在2D按钮数组中找到该按钮的行和列,并计算乘法

PS:您甚至不必使用
event.getSource()
来获取按钮,因为您已经有了引用此按钮的
j
变量

编辑:行和列可从外部变量获得,只需将其设为最终变量:

for(int rowCounter = 0; rowCounter < rowCount; rowCounter++)
    for(int colCounter = 0; colCounter < colCount; colCounter++){
        final JButton j = new JButton("not clicked");
        final int row = rowCounter;
        final int col = colCounter;

        // now you can use row and col inside your listener.
for(int-rowCounter=0;rowCounter
我希望这样,一旦用户在某个十字路口单击按钮,就会显示答案

例如:单击的第一个按钮将显示“1*1=1”

您可以在循环中设置
actionCommand
,循环计数中的乘法已经完成

final JButton j = new JButton("not clicked");
j.setActionCommand((rowCounter + 1) * (colCounter + 1)  + "");
然后使用
actionCommand

if (clicked) {
    j.setText(j.getActionCommand());


更新

不确定你改变了什么,但试试这个

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SquareGrid {

    private int rowCount;
    private int colCount;

    public SquareGrid(int rowCount, int colCount) {
        this.rowCount = rowCount;
        this.colCount = colCount;

    }
    JFrame theFrame;
    JButton[][] buttons;

    JPanel panel;

    private void createAndShowGui() {
        theFrame = new JFrame("grid");
        buttons = new JButton[rowCount][colCount];
        panel = new JPanel();
        panel.setLayout(new GridLayout(rowCount, colCount));

        for (int rowCounter = 0; rowCounter < rowCount; rowCounter++) {
            for (int colCounter = 0; colCounter < colCount; colCounter++) {
                final JButton j = new JButton("not clicked");
                j.setActionCommand((rowCounter + 1) * (colCounter + 1) + "");
                j.addActionListener(new ActionListener() {
                    boolean clicked = false;

                    @Override
                    public void actionPerformed(ActionEvent event) {
                        if(clicked == false) clicked = true;
                        else clicked = false;

                        if (clicked) {
                            j.setText(j.getActionCommand());

                        } else {
                            j.setText("not clicked");
                        }

                    }
                });
                buttons[rowCounter][colCounter] = j;
                panel.add(j);
                JButton buttonClicked = (j);  //THIS WAS ADDED

            }
        }
        theFrame.add(panel);
        theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theFrame.pack();
        theFrame.setVisible(true);

    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter number of rows");
        int rowCount = input.nextInt();

        Scanner input1 = new Scanner(System.in);
        System.out.println("Enter number of columns");
        int colCount = input1.nextInt();

        SquareGrid h = new SquareGrid(colCount, rowCount);
        h.createAndShowGui();
    }
}
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.Scanner;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类网格{
私有整数行数;
私有整数计数;
公共平方网格(整数行计数、整数列计数){
this.rowCount=rowCount;
this.colCount=colCount;
}
JFrame框架;
JButton[]]按钮;
JPanel小组;
私有void createAndShowGui(){
theFrame=新JFrame(“网格”);
按钮=新的JButton[rowCount][colCount];
panel=新的JPanel();
panel.setLayout(新的网格布局(rowCount、colCount));
对于(int rowCounter=0;rowCounter