显示按下的Jbutton的行和列 [英] displaying the rows and columns of the Jbutton pressed

查看:126
本文介绍了显示按下的Jbutton的行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在这个游戏板上工作,在那里我创建了一个5x5 Jbuttons板.

I am working on this game board where i have created a 5x5 Jbuttons board.

import java.awt.Color;
import java.awt.Graphics;
import java.util.*;
import java.awt.event.*; // Needed for ActionListener and ActionEvent
import javax.swing.*; // Needed for JFrame and JButton

public class boardGame extends JFrame implements ActionListener {
//colours variable
private Color[] colors;
// This stores all buttons
JButton[][] buttons = new JButton[5][5];
String[] buttonLabels = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};

public boardGame(String title) {
super(title);
setLayout(null);

Color[] colors = new Color[4];

//Initialize the values of the array
colors[0] = Color.red;
colors[1] = Color.blue;
colors[2] = Color.yellow;
colors[3] = Color.green;


for(int row=0; row<5; row++) {
    for (int col=0; col<5; col++) {
        buttons[row][col] = new JButton(buttonLabels[row*5+col]);
        buttons[row][col].setLocation(10+col*55, 10+row*55);
        buttons[row][col].setSize(50,50);
        buttons[row][col].addActionListener(this);

        buttons[row][col].setBackground(colors[new Random().nextInt(4)]);

        add(buttons[row][col]);
    }
}

//labels
JLabel label1 = new JLabel("COL: ");
label1.setLocation(10,260);
label1.setSize(100,100);
add(label1);

JLabel label2 = new JLabel("ROW: ");
label2.setLocation(10,280);
label2.setSize(100,100);
add(label2);

setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(305,395);
}


// This is the single event handler for all the buttons
public void actionPerformed(ActionEvent e) {
JButton selectedBtn = (JButton) e.getSource();

for (int row = 0; row < buttons.length; row++) {
  for (int col = 0; col < buttons[row].length; col++) {
    if (buttons[row][col] == selectedBtn) {
      System.out.printf("Selected row and column: %d %d%n", row+1, col+1);
       }
     }
   }
 }

public static void main(String args[]) {
boardGame frame = new boardGame("The Board Game");
frame.setVisible(true);  
}

}

我是这个动作命令和动作监听器(按下鼠标)事件的新手.您一定已经注意到我在我的代码中使用了此代码:

I am new to this action command and action listener (mouse pressed) event. You must have noticed i used this in my code:

// This is the single event handler for all the buttons
public void actionPerformed(ActionEvent e) {
JButton selectedBtn = (JButton) e.getSource();

for (int row = 0; row < buttons.length; row++) {
  for (int col = 0; col < buttons[row].length; col++) {
    if (buttons[row][col] == selectedBtn) {
      System.out.printf("Selected row and column: %d %d%n", row+1, col+1);
       }
     }
   }
 }

这段代码显示了正确按下的按钮的行和列.但是我想做的是我想在我的Jlabels前面显示此信息.

this piece of code is displaying the rows and columns of the button pressed correctly. but what i'm trying to do is that i'm trying to display this information in front of my Jlabels.

即,如果按下一个按钮,并且该按钮位于第2行和第4列,则应显示为:

i.e if a button is pressed and it lies on a row 2 and column 4 it should show it like this :

//labels
JLabel label1 = new JLabel("COL: ");
label1.setLocation(10,260);
label1.setSize(100,100);
add(label1);

JLabel label2 = new JLabel("ROW: ");
label2.setLocation(10,280);
label2.setSize(100,100);
add(label2); 

任何帮助将不胜感激:)

any help will be appreciated :)

推荐答案

您有按钮,因此需要使用setBackground方法(您的颜色在这里) 因此您的解决方案可以是:生成0到3之间的随机数(针对您的颜色数组...)

You have button, so you need to use the method setBackground( YOUR COLOR HERE) so your solution can be: generate a random number between 0 and 3 (for your array of colors...)

for(int row=0; row<5; row++) {
  for (int col=0; col<5; col++) {
    buttons[row][col] = new JButton(buttonLabels[row*5+col]);
    buttons[row][col].setLocation(10+col*55, 10+row*55);
    buttons[row][col].setSize(50,50);
    buttons[row][col].addActionListener(this);
   
    buttons[row][col].setBackground(colors[new Random().nextInt(4)]);  ///this will do the trick..
    
    add(buttons[row][col]);
  }
}


如果需要获取单击按钮的坐标",请然后在actionPerformed中进行逆运算以获取按钮的x和y.


If you need to get the "Coordinates of the clicked button" then in the actionPerformed do the reverse math to get the x and y of the button.

Point p = btn1.getLocation();
int xCoordinates = (p.x - 10) / 55;
int yCoordinates = (p.x - 10) / 55;
System.out.println("Here is x: " + xCoordinates);
System.out.println("Here is y: " + yCoordinates);

这篇关于显示按下的Jbutton的行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆