gpt4 book ai didi

java - 无法找到按下按钮的坐标

转载 作者:行者123 更新时间:2023-11-29 05:32:16 24 4
gpt4 key购买 nike

唯一的问题是,每当我将 x 和 y 的初始值设置为大于 10 时,它都会给出错误的结果。请帮助。对于 x 和 y 小于 10 的值,它工作正常。我还对其进行了调试,发现每当在第 10 个索引之后按下按钮时,它的行为就像将变量 i 设置为 1。我无法解决这个问题,因为我是 java 的新手。所以我真的需要帮助。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

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

import javax.swing.JFrame;

class butMaddFrame extends JFrame implements ActionListener
{
int x=12;
int y=12;
JButton[][] buttons = new JButton[x][y];
JPanel mPanel = new JPanel();
JPanel bPanel = new JPanel();
JPanel cPanel = new JPanel();
JTextArea scoreKeeper = new JTextArea();
Container c = getContentPane();
int[][] intArray = new int[x][y];

public butMaddFrame()
{
butGen();
score2();
//cPanel.add(scoreKeeper);
bPanel.setLayout(new GridLayout(x,y));
mPanel.setLayout(new BorderLayout());

mPanel.add(bPanel, BorderLayout.CENTER);
// mPanel.add(cPanel, BorderLayout.LINE_END);
c.add(mPanel);

setTitle("ButtonMaddness");
setSize(1000,400);
setLocation(200,200);
setVisible(true);
}

private void butGen()
{
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
{
buttons[i][j] = new JButton(String.valueOf(i)+"x"+String.valueOf(j));
buttons[i][j].setActionCommand("button" +i +"_" +j);
buttons[i][j].addActionListener(this);
bPanel.add(buttons[i][j]);
}
}

private void score()
{
// String string = "";
// for(int i=0;i<x;i++)
// {
// for(int j=0;j<y;j++)
// string += i+"x"+j+" => " +String.valueOf(intArray[i][j]) +"\t";
// string+= "\n";
// }
// scoreKeeper.setText(string);

}

private void score2()
{
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
buttons[i][j].setText(String.valueOf(intArray[i][j]));


}

public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().contains("button"))
{
int i = Integer.parseInt(Character.toString(e.getActionCommand().replaceAll("button","").replaceAll ("_", "").charAt(0)));
int j = Integer.parseInt(Character.toString(e.getActionCommand().replaceAll("button","").replaceAll ("_", "").charAt(1)));

intArray[i][j]++;
// buttons[i][j].setVisible(false);


buttons[i][j].setBackground(Color.black);



System.out.println(e.getActionCommand() +" " +i +" " +j);
}
// score2();
}
}




import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class buttonMaddness {
public static void main(String[] args)
{
butMaddFrame myFrame = new butMaddFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

最佳答案

你的问题在这里:

replaceAll("_", "").charAt(0)

因为你的一些按钮有类似 11_9 的东西
例子。所以你只得到数字 11 的第一个 1。

只需更改您的 actionPerformed 方法
为此,您的错误将得到修复。

    public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().contains("button")) {

String str = e.getActionCommand().replaceAll("button", "");
System.out.println(str);
String[] v = str.split("_");
int i = Integer.parseInt(v[0]);
int j = Integer.parseInt(v[1]);
/*
int i = Integer.parseInt(Character.toString(e.getActionCommand()
.replaceAll("button", "").replaceAll("_", "").charAt(0)));
int j = Integer.parseInt(Character.toString(e.getActionCommand()
.replaceAll("button", "").replaceAll("_", "").charAt(1)));
*/

intArray[i][j]++;
// buttons[i][j].setVisible(false);

buttons[i][j].setBackground(Color.black);

System.out.println(e.getActionCommand() + " " + i + " " + j);
}
// score2();
}

关于java - 无法找到按下按钮的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20712988/

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