Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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_User Interface - Fatal编程技术网

Java 一个简单的GUI乘法表

Java 一个简单的GUI乘法表,java,user-interface,Java,User Interface,我应该使用GUI创建一个简单的乘法表,并且大部分情况下,它都在做它应该做的事情。但是我就是想不出它的底部,如果你点击一个特定的按钮,一个文本会告诉你这两个数字的乘积和答案 无论我点击哪个按钮,它都只会告诉我输入的数字并将它们相乘。这是我的密码: import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListen

我应该使用GUI创建一个简单的乘法表,并且大部分情况下,它都在做它应该做的事情。但是我就是想不出它的底部,如果你点击一个特定的按钮,一个文本会告诉你这两个数字的乘积和答案

无论我点击哪个按钮,它都只会告诉我输入的数字并将它们相乘。这是我的密码:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JOptionPane;

public class MultiplicationTable {
    int clickedCount;
    int rowCount;
    int colCount;

    JFrame frame;
    JPanel buttonPanel, countPanel;
    JLabel countLabel;


    private void createAndShowGui(){

        rowCount = Integer.parseInt(JOptionPane
                .showInputDialog("How many rows do you want your multiplication table to be? "));

        colCount = Integer
                .parseInt(JOptionPane
                        .showInputDialog("How many columns do you want your multiplication table to be? "));



        frame = new JFrame ("Simple Multiplication Table");
        frame.setLayout(new BorderLayout());

        buttonPanel = new JPanel();

        buttonPanel.setLayout(new GridLayout(rowCount, colCount));


        countPanel = new JPanel();      
        countLabel = new JLabel("? x ? = ?");

        for(int rowCounter = 1; rowCounter <= rowCount; rowCounter++)
            for(int colCounter = 1; colCounter <= colCount; colCounter++){

                final JButton j = new JButton(Integer.toString(rowCounter * colCounter));

                j.addActionListener(new ActionListener(){


                    @Override
                    public void actionPerformed(ActionEvent event) {
                        for(int rc = 1; rc <= rowCount; rc++)
                            for(int cc = 1; cc <= colCount; cc++){
                                countLabel.setText(rc + " x " + cc + " = " + String.valueOf(rc * cc));  
                            }   
                    }
                });

                buttonPanel.add(j); 
            }

        frame.add(buttonPanel, BorderLayout.NORTH);
        countPanel.add(countLabel);
        frame.add(countPanel, BorderLayout.SOUTH);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args){
        MultiplicationTable c = new MultiplicationTable();
        c.createAndShowGui();
    }
}
导入java.awt.BorderLayout;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JOptionPane;
公共类乘法表{
点击计数;
整数行数;
int colCount;
JFrame框架;
JPanel按钮面板,计数面板;
JLabel计数标签;
私有void createAndShowGui(){
rowCount=Integer.parseInt(JOptionPane
.showInputDialog(“您希望乘法表有多少行?”);
colCount=整数
.parseInt(JOptionPane)
.showInputDialog(“您希望乘法表有多少列?”);
frame=新的JFrame(“简单乘法表”);
frame.setLayout(新的BorderLayout());
buttonPanel=新的JPanel();
setLayout(新的GridLayout(rowCount,colCount));
countPanel=新的JPanel();
countLabel=新的JLabel(“?x=?”);

对于(int rowCounter=1;rowCounter您当前的
ActionListener
没有上下文,它不知道使用了哪两个值来生成您给按钮的值

您可以做的是创建一个
ActionListener
,它接受用于生成按钮文本的值,这样就能够相应地更新标签。例如

public class MultiplerActionListener implements ActionListener {
    private int[] values;
    private JLabel label;
    public MultiplerActionListener(JLabel label, int... valuesToMultiple) {
        values = valuesToMultiple;
        this.label = label;
    }

    @Override
    public void actionPerformed(ActionEvent event) {        
        int answer = 1; 
        StringBuilder sb = new StringBuilder(64);
        for (int value : values) {
            if (sb.length() > 0) {
                sb.append(" x ");
            }
            sb.append(value);
            answer *= value;
        }
        sb.append(" = ");
        sb.append(answer);
        label.setText(sb.toString());
    }
}
然后,您只需在创建按钮时应用它

for(int rowCounter = 1; rowCounter <= rowCount; rowCounter++) {
    for(int colCounter = 1; colCounter <= colCount; colCounter++){
        final JButton j = new JButton(Integer.toString(rowCounter * colCounter));
        j.addActionListener(new MultiplerActionListener(countLabel, rowCounter, colCounter));
        buttonPanel.add(j); 
    }
}

for(int rowCounter=1;rowCounter)您实际希望看到什么?