开发者

I have 9 JButtons in the ArrayList . But I am not able to change the Lable of the Buttons in any event,,,

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

class One implements ActionListener
{
    JFrame frame = new JFrame();
    ArrayList<JButton> myB = new ArrayList<JButton>();
    Panel p = new Panel();
    Dimension d = new Dimension(20, 20);
    String s = "", s1 = "";
    JButton B = new JButton(), B1 = new JButton();

    public void addButtons()
    {
        for(int i = 0; i < 9; i++)
        {
            myB.add(new JButton());         //IMP
        }
    }

    public void display()
    {
        frame.getContentPane().add(p);
        for(JButton btn : myB)
        {
            btn.setPreferredSize(d);
            p.add(btn);        //IMP
        }
        p.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 5));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 300);
        frame.setVisible(true);
    }

    public void GamePlay()
    {
        s = JOptionPane.showInputDialog(null, "HUMAN or COMPUTER");
        if(s.equals("HUMAN"))
        {
            for(JBut开发者_JAVA百科ton B1 : myB)
            {// advanced for loop
                B1.addActionListener(this);
            }
        }
        else
        {
            s1 = "COMPUTER";
        }
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        // TODO Auto-generated method stub
        System.out.println(" action performed!!");
        B1.setText("X");
    }
}

public class Two
{
    public static void main(String[] args)
    {
        One a = new One();
        a.addButtons();
        a.display();
        a.GamePlay();
    }
}


Basically, @mKorbel is right: you don't see the button changing because it was never added to the ui. Plus, there are a handful of rules you didn't follow

  • do not mix AWT with Swing components, use Swing consistently. They are easily recognizable by the J prefix
  • do follow java naming conventions
  • do choose narrative names (vs. B, s1, ...)
  • do not call setPreferredSize
  • do format the code to make it easily readable (vs too many white lines, inconsistent indentation)


add B1 = new JButton() to Panel p = new Panel();


I think you are confusing B1 with the button in the list, and are calling setText in the wrong button. If you want to change the label of the clicked button, a quick fix would be.

public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

    System.out.println(" action performed!!");
    B1 = (JButton)e.getSource();
    B1.setText("X");

}

You should have split the ActionListener into a differnt class to avoid confusion like this. To apply the text for all button in the list, you can try something like this.

class MyActionListener implements ActionListener{

    ArrayList<JButton> buttonList;
    public MyActionListener(ArrayList<JButton> a) {
        buttonList = a;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        for(JButton jb: buttonList) {
            jb.setText("X");
        }
    }
}


class One {
    MyActionListener buttonListener;
    ...
    public void GamePlay() {
    ...
        if(s.equals("HUMAN")) {
            buttonListener = new MyActionListener(myB);
            for(JButton B1 : myB) {
                B1.addActionListener(buttonListener);
        }
    ...
    }

}

Basically, create your own action listener contain the appropriate data to do what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜