我在 ArrayList 中有 9 个 JButton。但无论如何我都无法更改按钮的标签,,,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本上,@mKorbel 是对的:您看不到按钮发生变化,因为它从未添加到用户界面中。另外,还有一些您没有遵守的规则,
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
将
B1 = new JButton()
添加到Panel p = new Panel();
add
B1 = new JButton()
toPanel p = new Panel();
我认为您将 B1 与列表中的按钮混淆了,并且在错误的按钮中调用了 setText。如果您想更改单击按钮的标签,可以快速解决。
您应该将 ActionListener 拆分为不同的类以避免这样的混淆。
要为列表中的所有按钮应用文本,您可以尝试这样的操作。
基本上
,创建您自己的操作侦听器,其中包含执行您想要的操作的适当数据。
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.
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.
}
Basically, create your own action listener contain the appropriate data to do what you want.