새 세션을 시작하는 데 어려움이 있습니다. 새 게임 메뉴 옵션을 사용할 때 새로운 인스턴스가 생깁니다. 실제로 현재 인스턴스를 새로운 인스턴스로 바꾸기를 원합니다. 난 당신이 새로운 프레임을 얻을 곳이 용의자 ...JFrame 다시 그리기/새로 고침
package tictactoe;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TicTacToe2 extends JFrame implements ActionListener {
char[][] game = new char[3][3];
JButton[][] buttons = new JButton[3][3];
JButton menuItem = new JButton();
JMenu menu = new JMenu ("TicTacToe");
JMenuItem newgame = new JMenuItem("Start New Game"),
exit = new JMenuItem("Exit");
TicTacToe2()
{
super("Tic Tac Toe");
setSize(500, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
JPanel northPanel= new JPanel();
northPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
add("North", northPanel);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3,3, 4, 4));
add("Center", buttonPanel);
Font font = new Font("Serif", Font.BOLD, 32);
for (int row=0; row < game.length; row++)
{
for (int col =0; col < game[row].length; col++)
{
game[row][col] = ' ';
JButton b = new JButton(" ");
b.setFont(font);
b.setBackground(Color.green);
b.addActionListener(this);
buttons[row][col] = b;
buttonPanel.add(b);
}
}
menu.add(newgame);
menu.add(exit);
newgame.addActionListener(this);
exit.addActionListener(this);
JMenuBar bar = new JMenuBar();
bar.add(menu);
setJMenuBar(bar);
}
public void actionPerformed(ActionEvent e) {
Object menusource = e.getSource();
if(menusource == newgame){
new TicTacToe2();
}else if(menusource == exit){
System.exit(0);
}
}
public static void main(String[] args) {
TicTacToe2 ttt = new TicTacToe2();
}
}
을 논리를 얻고 희망을 (BorderLayout.CENTER, buttonPanel을 = 새로운 TicTacToePanel()); (명시 적으로 상수 "Center"를 사용하는 것은 나쁜 습관이다.) 그리고 2 중첩 for를 실행하는 sampson-chen에 따라 TicTacToePanel.reset() 메소드를 가진다. – ignis