6

Is it possible to check the text in a GUI button?

 2 years ago
source link: https://www.codesd.com/item/is-it-possible-to-check-the-text-in-a-gui-button.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Is it possible to check the text in a GUI button?

advertisements

This is a really simple code where when I click a button, the text on the button becomes "..." which is a string variable called move.

What I'm trying to figure out is if it is possible to check the text of a button in the if (e.getSource()==) command.

JButton [] button;
String move="...";

button =new JButton[25];
for (int a=0;a<25;a++)
{
    button[a]=new JButton();
    p1.add(button[a]);
    button[a].addActionListener(this);
}   

public void actionPerformed(ActionEvent e)
{
    for (int a=0; a<25;a++)
    {
        if (e.getSource()==button[a])
        {
          button[a].setText(move);
        }
    }
}

So after the code above, a button will have the text "..." on it. Now what I want for the next if (e.getSource()==) command is: if the button I click has the text (move), run the code.

I tried hundreds of random codes to see if they'll work but no luck:

sort of like this:

if (e.getSource()==button[a].text(move))

if (e.getSource()==button[a].getText.equals(move))

if (The button i click has the text (move) it will run this code)
{
    button[a].setBackground(Color.GREEN);
}

I'm fairly new to programming and I need this for my Checkers game. Is it possible to do what I am asking for?


String comparison in Java should be done with .equals() not ==.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK