比较不同 JButton 的文本
Comparing the text of different JButtons
我目前正在编写一个游戏,其中一部分是将不同的方块放入棋盘中。我计划通过使用不同的按钮来模拟这一点,这些按钮将用于表示具有相应坐标的图块。例如,一个按钮会显示 "A1"、"A2" 等。我想要完成的是让用户单击 "A1" 磁贴,然后单击面板上的按钮代表 "A1" 会改变颜色,有没有办法通过板上的按钮并将其文本与用户的选择进行比较?以下是我用来创建板子的内容:
JButton[][] buttons = new JButton[9][12];
JPanel panel = new JPanel(new GridLayout(9,12,5,5));
panel.setBounds(10, 11, 800, 600);
frame.getContentPane().add(panel);
//board
for (int r = 0; r < 9; r++)
{
for (int c = 0; c < 12; c++)
{
buttons[r][c] = new JButton("" + (c + 1) + numberList[r]);
buttons[r][c].setBackground(Color.WHITE);
panel.add(buttons[r][c]);
}
}
这就是我在其中一个瓦片的代码上写的
JButton tile1 = new JButton ("A1");
tile1.setBounds(60,725,60,60);
frame.getContentPane().add(tile1);
tile1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String buttonText = tile1.getText();
// iterating through all buttons:
for(int i=0;i<buttons.length;i++){
for(int j=0;j<buttons[0].length;j++)
{
JButton b = buttons[i][j];
String bText = b.getText();
if(buttonText.equals(bText))
{
[i][j].setBackground(Color.BLACK);
}
}
}
}
} );
但是,它给我一个错误,说在“{”之后有一个预期的动作
您可以为您在循环中创建的每个 JButton 添加一个动作侦听器,如下所示:
buttons[r][c].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// your code here
}
} );
在您的代码中放置侦听器可能类似于
JButton[][] buttons = new JButton[9][12];
JPanel panel = new JPanel(new GridLayout(9,12,5,5));
panel.setBounds(10, 11, 800, 600);
frame.getContentPane().add(panel);
//board
for (int r = 0; r < 9; r++)
{
for (int c = 0; c < 12; c++)
{
buttons[r][c] = new JButton("" + (c + 1) + numberList[r]);
buttons[r][c].setBackground(Color.WHITE);
buttons[r][c].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
String buttonText = button.getText();
// now iterate over all the jbuttons you have
for(int i=0;i<buttons.length;i++){
for(int j=0;j<buttons[0].length;j++){
JButton b = buttons[i][j];
String bText = b.getText();
if(buttonText.equals(bText)){
// you found a match here
// and you have the positions i, j
//
}
}
}
}
} );
panel.add(buttons[r][c]);
}
}
并且您可以将要更改的颜色存储在全局静态数组中,并在您的动作侦听器中使用该数组。
有关向 JButton 添加侦听器的信息,您可以参考此线程 How do you add an ActionListener onto a JButton in Java
希望对您有所帮助!
你需要听众。
为您的 class 实施 ActionListener。这将要求您将 public void actionPerformed(ActionEvent e) {}
添加到 class。
您使用的每个 JButton 都应该有一个动作侦听器。
像这样应用一个:
JButton but = new JButton();
but.addActionListener(this);
最后,在我们添加的actionPerformed方法中,你需要添加类似这样的内容:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == but)
but.setBackground(Color.BLACK);
}
P.S。您可以通过以下方式获取按钮的文本值:
but.getText();
我目前正在编写一个游戏,其中一部分是将不同的方块放入棋盘中。我计划通过使用不同的按钮来模拟这一点,这些按钮将用于表示具有相应坐标的图块。例如,一个按钮会显示 "A1"、"A2" 等。我想要完成的是让用户单击 "A1" 磁贴,然后单击面板上的按钮代表 "A1" 会改变颜色,有没有办法通过板上的按钮并将其文本与用户的选择进行比较?以下是我用来创建板子的内容:
JButton[][] buttons = new JButton[9][12];
JPanel panel = new JPanel(new GridLayout(9,12,5,5));
panel.setBounds(10, 11, 800, 600);
frame.getContentPane().add(panel);
//board
for (int r = 0; r < 9; r++)
{
for (int c = 0; c < 12; c++)
{
buttons[r][c] = new JButton("" + (c + 1) + numberList[r]);
buttons[r][c].setBackground(Color.WHITE);
panel.add(buttons[r][c]);
}
}
这就是我在其中一个瓦片的代码上写的
JButton tile1 = new JButton ("A1");
tile1.setBounds(60,725,60,60);
frame.getContentPane().add(tile1);
tile1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String buttonText = tile1.getText();
// iterating through all buttons:
for(int i=0;i<buttons.length;i++){
for(int j=0;j<buttons[0].length;j++)
{
JButton b = buttons[i][j];
String bText = b.getText();
if(buttonText.equals(bText))
{
[i][j].setBackground(Color.BLACK);
}
}
}
}
} );
但是,它给我一个错误,说在“{”之后有一个预期的动作
您可以为您在循环中创建的每个 JButton 添加一个动作侦听器,如下所示:
buttons[r][c].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// your code here
}
} );
在您的代码中放置侦听器可能类似于
JButton[][] buttons = new JButton[9][12];
JPanel panel = new JPanel(new GridLayout(9,12,5,5));
panel.setBounds(10, 11, 800, 600);
frame.getContentPane().add(panel);
//board
for (int r = 0; r < 9; r++)
{
for (int c = 0; c < 12; c++)
{
buttons[r][c] = new JButton("" + (c + 1) + numberList[r]);
buttons[r][c].setBackground(Color.WHITE);
buttons[r][c].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
String buttonText = button.getText();
// now iterate over all the jbuttons you have
for(int i=0;i<buttons.length;i++){
for(int j=0;j<buttons[0].length;j++){
JButton b = buttons[i][j];
String bText = b.getText();
if(buttonText.equals(bText)){
// you found a match here
// and you have the positions i, j
//
}
}
}
}
} );
panel.add(buttons[r][c]);
}
}
并且您可以将要更改的颜色存储在全局静态数组中,并在您的动作侦听器中使用该数组。
有关向 JButton 添加侦听器的信息,您可以参考此线程 How do you add an ActionListener onto a JButton in Java
希望对您有所帮助!
你需要听众。
为您的 class 实施 ActionListener。这将要求您将 public void actionPerformed(ActionEvent e) {}
添加到 class。
您使用的每个 JButton 都应该有一个动作侦听器。 像这样应用一个:
JButton but = new JButton();
but.addActionListener(this);
最后,在我们添加的actionPerformed方法中,你需要添加类似这样的内容:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == but)
but.setBackground(Color.BLACK);
}
P.S。您可以通过以下方式获取按钮的文本值:
but.getText();