简体   繁体   English

如何知道从网格单击了哪个组件?

[英]How to know what component was clicked from a grid?

I have created a grid that contains 10x10 buttons using 2d arrays. 我使用2d数组创建了一个包含10x10按钮的网格。 i tried x.getSource().getLabel() but compiler says they are not compatible. 我试过x.getSource()。getLabel(),但是编译器说它们不兼容。 also i want to get the specific button that was clicked. 我也想得到被点击的特定按钮。

I want to get the exact button that was clicked from the grid i made and get its label. 我想获取从我制作的网格中单击的确切按钮并获取其标签。 what method i need to use? 我需要使用什么方法?

import javax.swing.JFrame; //imports JFrame library
import javax.swing.JButton; //imports JButton library
import java.awt.GridLayout; //imports GridLayout library
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class ButtonGrid extends JFrame implements ActionListener
{
    JFrame frame=new JFrame(); //creates frame
    JButton[][] grid; //names the grid of buttons
    public int x;
    public int y;
    public ButtonGrid(int width, int length)
    { //constructor
        char temp;
        String charput;
        frame.setLayout(new GridLayout(width,length)); //set layout
        grid = new JButton[width][length]; //allocate the size of grid
        for(int y=0; y<length; y++)
        { //start
            for(int x=0; x<width; x++) 
            {
                temp=charRand(); //get random character
                charput = ""+temp; //converts character to string
                grid[x][y]=new JButton(); //creates new button
                frame.add(grid[x][y]); //adds button to grid
                grid[x][y].addActionListener(this);
                grid[x][y].setLabel(charput); //set charput as label
            }
        }
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack(); //sets appropriate size for frame
        frame.setVisible(true); //makes frame visible
    }
    /* generates  randomiz letter for the button of the grid*/
    public char charRand()
    {
        String consonantList = new String("BCDFGHL"); //list 1
        String consonantList2 = new String("MNPRSTWY"); //list 2
        String consonantList3= new String("JQXZVK"); //list 3
        String vowelList = new String("AEIOU"); //list of vowels
        int vowelOrConsonant; //holder of random number 
        int chosen; //selects the chosen random letter
        Random randGen = new Random(); //generates random int value
        char selected; //gets the random letter chosen by variable chosen

        vowelOrConsonant = randGen.nextInt(4);
        if (vowelOrConsonant == 0)
        {
            chosen = randGen.nextInt(5); //list of vowels
            selected = vowelList.charAt(chosen); //selects a char from vowels
        }
        else if(vowelOrConsonant == 1)
        {
            chosen = randGen.nextInt(7); //list 1
            selected = consonantList2.charAt(chosen); //selects a char
        }
        else if(vowelOrConsonant == 2)
        {
            chosen = randGen.nextInt(8); //list 2
            selected = consonantList2.charAt(chosen); //selects a char
        }
        else
        {
            chosen = randGen.nextInt(6); //list 3
            selected = consonantList.charAt(chosen);
        }
        return selected; //returns the random letter
    }

    public static void main(String[] args) 
    {
        new ButtonGrid(10,10);//makes new ButtonGrid with 2 parameters
    }

    public void actionPerformed(ActionEvent x)
    {
        /* i get wrong output on this line. 
         * i want to get the exact button that was clicked and get its label.
         */ 
        if (x.getSource()==grid[x][y])
            JOptionPane.showMessageDialog(null,x.getSource().getLabel);
    }
}

getSource() returns an Object , so you need to cast it to JButton , like this: getSource()返回一个Object ,因此您需要将其JButtonJButton ,如下所示:

public void actionPerformed(ActionEvent x) {
    JOptionPane.showMessageDialog(null, ((JButton)x.getSource()).getText());
}

Also note that getLabel() and setLabel() are deprecated and should be replaced by getText() and setText() . 另请注意,不赞成使用getLabel()setLabel() ,应将其替换为getText()setText()

You can make a class that extends Jbutton. 您可以创建一个扩展Jbutton的类。 and add two fields to it of type int(X & Y ). 并向其中添加两个类型为int(X&Y)的字段。 The constructor will look like this: public MyButton(int x, y); 构造函数将如下所示:public MyButton(int x,y);

And when you are filling your grid, don't use directly the Jbutton class. 在填充网格时,请勿直接使用Jbutton类。 Use your class and for X & Y supply the i & j parameters of the two for cycles that you are using. 使用您的类,对于X&Y,请为您正在使用的循环提供两者的i&j参数。 Now when you are using Action Listener for your button you can use his X & Y fields as they represent its place on the grid. 现在,当您为按钮使用Action Listener时,您可以使用其X&Y字段,因为它们表示其在网格中的位置。 Hope this helps! 希望这可以帮助! It tottaly worked for me and its simple as hell. 它完全为我工作,简单到地狱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM