第一次在这里用户,所以请耐心等待...我想创建一个代码,询问用户他们想要创建乘法表的行数和列数,但老实说我不知道去哪里从这里......有人可以帮忙吗?我想要一些细节......例如...为此目的在这里创建一个for循环......
抱歉,如果我的代码格式不正确...这就是我到目前为止..正确地要求用户输入多个行和列并显示那些行和列..我想这样做一次用户单击某个交叉点的按钮,将显示答案。
例如:点击时的第一个按钮将显示“1 * 1 = 1”
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((rowCount + 1) * (colCount + 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());
}
}
});
String event;
buttons[rowCounter][colCounter] = j;
panel.add(j);
}
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();
}
}
答案 0 :(得分:2)
单击按钮时,您可以通过从事件中获取该按钮来确定单击了哪个按钮:
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.
答案 1 :(得分:2)
我想这样做,一旦用户点击某个十字路口的按钮,就会显示答案。
例如:点击时的第一个按钮将显示“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();
}
}
答案 2 :(得分:0)
John Hurley的CS202级ROFL -John Hurley的CS202级ROFL