简体   繁体   English

JFrame重绘/重新加载

[英]JFrame Repaint / reload

I'm having difficulty kicking off a new session. 我很难开始一个新的会议。 When I use the menu option new game, I get a new instance - when really, I just want to replace the current instance with a new one. 当我使用菜单选项新游戏时,我得到一个新实例 - 当真的,我只想用新的实例替换当前实例。 I have tried many different things and still can't solve it... 我尝试了很多不同的东西,仍然无法解决它...

Here is my code: 这是我的代码:

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

}
}

You are creating a new instance each time when going through the menu for "new game" by: 每次通过“新游戏”菜单时,您正在创建一个新实例:

        new TicTacToe2();   

If you'd like to just reset the instance, write a reset() method that sets the game state to initial state. 如果您只想重置实例,请编写一个reset()方法,将游戏状态设置为初始状态。

public void actionPerformed(ActionEvent e) {
    Object  menusource = e.getSource();   
    if(menusource ==  newgame){ 
        new TicTacToe2();   //What are you doing with this object? 
                            //Should really be resetGame();
    }else if(menusource ==  exit){
        System.exit(0);
    }  
} 

Could be a problem.. I suspect that's where you get a new frame instance. 可能是一个问题..我怀疑你得到一个新的框架实例。 Have a method that clears the arrays to a default value, resets the score and you should be good to go. 有一个方法可以将数组清除为默认值,重置分数,你应该很好。

Instead of creating a new instance of your class 而不是创建您的类的新实例

new TicTacToe2();

when the "New Game" menu item is clicked, you could clear the button array text and prepare for a new game: 单击“新游戏”菜单项时,您可以清除按钮阵列文本并准备新游戏:

for (int i=0; i < buttons.length; i++ ) {
   for (int j=0; j < buttons[0].length; j++ ) {
      buttons[i][j].setText(" ");
   }
}

As above answer already mentioned that you do not have to create a new instance like 如上所述,已经提到过您不必创建新的实例

new TicTacToe2()

So, i will continue from there. 所以,我会从那里继续。 Your if statement should be something like 你的if语句应该是这样的

if (menusource == newgame) {
     getContentPane.removeAll();
     setContentPane(aFunc);
}

Create a function which looks like something below which sets layout and adds the component to it. 创建一个看起来像下面的函数来设置布局并将组件添加到它。 Put your drawing logic there and give it as an argument to setContent pane. 将绘图逻辑放在那里并将其作为setContent窗格的参数。 For example 例如

 private JPanel aFunc() {
    custSelectPanel.setLayout(null);

    customerTable.setDragEnabled(false);
    customerTable.setFillsViewportHeight(true);

    ......

    cancelButton.setLocation(350, 0);
    cancelButton.setSize(100, 40);
    buttonPanel.add(cancelButton);

    return custSelectPanel;
}

I hope you are getting the logic here 我希望你在这里得到逻辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM