[英]Reference clicked JButton inside ActionListener
我正在嘗試使用swing編寫Tic Tac Toe程序,但我似乎遇到了一些麻煩。 在我的匿名內部類中,我嘗試為每個按鈕設置actionListener,但是我無法找到允許我引用按鈕並將它們設置為X或Y的類型或變量。我試過了.getSource()。setText()在我的匿名類中,但是返回時出現了錯誤。 有什么想法嗎? 謝謝! 亞歷克斯
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TicTacToe {
public JFrame frame;
public JLabel label;
public JPanel panel;
public static int counter;
public void go()
{
frame = new JFrame("TicTacToe");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new GridLayout(3,3,10,10));
frame.add(BorderLayout.CENTER, panel);
label= new JLabel("TIC TAC TOE");
frame.add(BorderLayout.NORTH, label);
;
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 1");
JButton button3 = new JButton("Button 1");
JButton button4 = new JButton("Button 1");
JButton button5 = new JButton("Button 1");
JButton button6 = new JButton("Button 1");
JButton button7 = new JButton("Button 1");
JButton button8 = new JButton("Button 1");
JButton button9 = new JButton("Button 1");
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(button7);
panel.add(button8);
panel.add(button9);
frame.setVisible(true);
panel.setVisible(true);
}
public static void main(String[] args)
{
TicTacToe gui = new TicTacToe();
gui.go();
}
}
請記住, ActionListener
可用於許多不同類型的組件,因此源參考是一般化的。 您需要轉換回預期值
button9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source instanceof JButton) {
JButton btn = (JButton)source;
// Go ahead and do what you like
}
}
});
雖然我知道你的ActionListener
,但它幾乎可以保證Object
的源類型是JButton
,我從不喜歡盲目地投射對象,但那就是我
如果您收到錯誤,那么您應該發布它們但我假設它是因為您沒有斷言源對象實際上是一個按鈕。 對於您正在做的事情,有兩種直接的解決方案。
首先 ,因為您只為每個按鈕添加一個動作偵聽器,您可以假設它是動作事件所引用的對象。 請注意,按鈕需要是實例變量或聲明為final:
button1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
button1.setText("X/Y");
}
});
其次,這將解決您的錯誤的原因,是斷言ActionEvent源對象實際上是一個按鈕。 這是通過檢查源是JButton的實例來完成的:
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
((JButton) e.getSource()).setText("X/Y");
}
}
});
我仔細檢查了一下你的代碼。 我不太清楚為什么你的代碼有錯誤,但我沒有得到任何錯誤。 我建議,因為你有共同的代碼,像我在數組中那樣重用它。 我還添加了一個boolean
用於在每次按鈕點擊時切換玩家。
最后,我建議在構造函數中設置JFrame,或者在構造函數調用的私有方法中設置JFrame(更復雜的用戶界面可能包含大量代碼,並且將其分解為代碼可維護性的良好習慣),就像我展示的那樣下面。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TicTacToe {
public static final boolean PLAYER_X = false;
public static final boolean PLAYER_O = true;
public static int counter;
private JFrame frame;
private JLabel label;
private JPanel panel;
private JButton[] buttons;
private boolean player;
public TicTacToe() {
frame = new JFrame("TicTacToe");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setPreferredSize(new Dimension(500, 500));
panel.setLayout(new GridLayout(3, 3, 10, 10));
frame.add(BorderLayout.CENTER, panel);
label = new JLabel("TIC TAC TOE");
frame.add(BorderLayout.NORTH, label);
/* Set the initial player turn to player X */
player = PLAYER_X;
/* Create the JButtons */
buttons = new JButton[9];
/* Loop through and set all of them up */
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton();
buttons[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
((JButton)e.getSource()).setText(player ? "O" : "X"); /* Set button text */
player = !player; /* Switch turns */
}
}
});
/* Add all of the buttons to the panel. */
panel.add(buttons[i]);
}
/* Pack the frame to the contents. Basically a "fit to contents". */
frame.pack();
}
public void go() {
frame.setVisible(true);
panel.setVisible(true);
}
public static void main(String[] args) {
TicTacToe gui = new TicTacToe();
gui.go();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.