i am creating a Tic tac toe project with the simple JButtons i have set the images for 0 and * in JButton but i want that JButton Should not be change if they are clicked once so that when 1 user will press on a button then the image on the JButton should not be changed
import javax.swing.*;
import java.awt.event.*;
class My
{
public static Boolean flag=true;
}
class Tic extends JFrame implements ActionListener
{
JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
ImageIcon img,img1;
Tic()
{
img=new ImageIcon("C:\\Users\\Love\\Desktop\\z.jpeg");
img1=new ImageIcon("C:\\Users\\Love\\Desktop\\k.jpeg");
btn1=new JButton();btn2=new JButton();btn3=new JButton();
btn4=new JButton();btn5=new JButton();btn6=new JButton();
btn7=new JButton();btn8=new JButton();btn9=new JButton();
btn1.setBounds(0,0,80,70);btn2.setBounds(80,0,80,70);btn3.setBounds(160,0,80,70);
btn4.setBounds(0,70,80,70);btn5.setBounds(80,70,80,70);btn6.setBounds(160,70,80,70);
btn7.setBounds(0,140,80,70);btn8.setBounds(80,140,80,70);btn9.setBounds(160,140,80,70);
btn1.addActionListener(this);btn2.addActionListener(this);btn3.addActionListener(this);
btn4.addActionListener(this);btn5.addActionListener(this);btn6.addActionListener(this);
btn7.addActionListener(this);btn8.addActionListener(this);btn9.addActionListener(this);
add(btn1);add(btn2);add(btn3);
add(btn4);add(btn5);add(btn6);
add(btn7);add(btn8);add(btn9);
setLayout(null);
setVisible(true);
setSize(246,240);
setLocation(400,200);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
JButton temp=(JButton)e.getSource();//System.out.println(temp);
if(temp==btn1||temp==btn2||temp==btn3||temp==btn4||temp==btn5||temp==btn6||temp==btn7||temp==btn8||temp==btn9){
if(My.flag==true)
{
temp.setIcon(img1);temp.setEnabled(false);
//temp.setText("<html><font color=red></font></html>");
My.flag=false;
}
else
{
temp.setIcon(img);
temp.setEnabled(false);
My.flag=true;
}
// btn1 btn2 btn3
//btn
}
}
public static void main(String args[])
{
new Tic().setTitle("Tic_Tac_Toe..Love--Soni");
}
}
I'm assuming that your buttons are starting with no text in them, and changing by firing an ActionListener
. If that is the case, then before you change the text in the JButton
, just check to see if the text it currently holds is an empty String
. If it is empty, make the changes. If it isn't empty, do nothing.
So your ActionListener
should look something like this:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
if(b.getText().equals("")){
//Logic to determine what it should be set to...
b.setText("0"); // or "*"
}
}
});
You could also disable the buttons after they have changed, which would look like this:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
//Logic to determine what it should be set to...
b.setText("0"); // or "*"
b.setEnabled(false);
}
});
UPDATE SINCE CODE ADDED
You should just have to update your if condition to this:
if((temp==btn1||temp==btn2||temp==btn3||temp==btn4||temp==btn5||temp==btn6||temp==btn7||temp==btn8||temp==btn9)
&& temp.getText().equals("")) {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.