我在 ArrayList 中有 9 个 JButton。但无论如何我都无法更改按钮的标签,,,

发布于 2024-11-04 05:14:26 字数 1832 浏览 2 评论 0原文

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扛起拖把扫天下 2024-11-11 05:14:26

基本上,@mKorbel 是对的:您看不到按钮发生变化,因为它从未添加到用户界面中。另外,还有一些您没有遵守的规则,

  • 不要将 AWT 与 Swing 组件混合使用,要始终如一地使用 Swing。它们可以通过 J 前缀轻松识别
  • 请遵循 java 命名约定
  • 请选择叙述性名称(相对于 B、s1...)
  • 不要调用 setPreferredSize
  • 请格式化代码以使其易于阅读(相对于 B、s1 等)白线太多,缩进不一致)

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)
南汐寒笙箫 2024-11-11 05:14:26

B1 = new JButton() 添加到 Panel p = new Panel();

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

溺深海 2024-11-11 05:14:26

我认为您将 B1 与列表中的按钮混淆了,并且在错误的按钮中调用了 setText。如果您想更改单击按钮的标签,可以快速解决。

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

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

}

您应该将 ActionListener 拆分为不同的类以避免这样的混淆。
要为列表中的所有按钮应用文本,您可以尝试这样的操作。

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

基本上

,创建您自己的操作侦听器,其中包含执行您想要的操作的适当数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文