繁体   English   中英

如何使JButton不可编辑

[英]How to make JButton Uneditable

我正在使用简单的JButton创建一个Tic tac toe项目,我已经在JButton中将图像设置为0和*,但是我希望如果单击一次JButton不应更改它们,以便当1个用户按下按钮时,图像在JButton上不应更改

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");
}

}

我假设您的按钮开始时没有任何文本,并且通过触发ActionListener更改。 如果是这样的话,那么在更改JButton的文本之前,只需检查一下当前保存的文本是否为空String 如果为空,请进行更改。 如果不为空,则不执行任何操作。

因此,您的ActionListener应该看起来像这样:

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 "*"
        }
    }
});

您还可以在更改按钮后将其禁用,如下所示:

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);
    }
});

自添加代码以来进行更新

您只需要将此if条件更新为:

if((temp==btn1||temp==btn2||temp==btn3||temp==btn4||temp==btn5||temp==btn6||temp==btn7||temp==btn8||temp==btn9) 
                    && temp.getText().equals("")) {

暂无
暂无

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

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