기본적으로 텍스트를 추가하기 전에 "for"루프의 각 단추를 final로 선언하면 문제가 발생합니다. 내가 그렇게 할 때, 그것은 나에게 많은 다른 오류를 준다. 누군가가 나를 도와 줄 수 있을까요? 이 함께 액션 리스너를 교체누군가가 클릭하면 JAVA Jbutton에 텍스트를 추가하는 데 도움이 될 수 있습니까?
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class tictactoe {
public static final int FRAME_WIDTH = 700;
public static final int FRAME_HEIGHT = 200;
public static void main(String[] args)
{
int slots = 9;
JButton[] gameButton = new JButton[9];
JPanel ticTacToeBoard = new JPanel();
ticTacToeBoard.setLayout(new GridLayout(3, 3));
for (int i = 0; i < slots; i++)
{
gameButton[i] = new JButton();
ticTacToeBoard.add(gameButton[i]);
gameButton[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gameButton[i].setText("x");
}
});
}
JButton clearButtons = new JButton("New Game");
JButton exit = new JButton("Exit");
JPanel rightPanel = new JPanel();
JLabel wonLabel = new JLabel();
rightPanel.setLayout(new GridLayout(1, 3));
rightPanel.add(wonLabel);
rightPanel.add(clearButtons);
rightPanel.add(exit);
JFrame mainFrame = new JFrame();
mainFrame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
mainFrame.setVisible(true);
mainFrame.setLayout(new GridLayout(1,2));
mainFrame.add(ticTacToeBoard);
mainFrame.add(rightPanel);
}
}
합니다. 감사. :) – commandrix