所以这是我的班级游戏,我需要它来运行和处理新游戏。一旦我按下Jbutton newGame,一个新的游戏Jpanel应该开始存在,一个新的循环应该从一个小细节分开。每次我运行游戏,完成它,然后第二次运行新游戏KoniecHry 2,一次使用正确的面板,一次面板保持值为NULL并抛出一个nullpointer异常,JOptionPane对话框也是抛出两次,第一次使用之前的会话名称抛出,一次使用null 我在哪里复制这些实例?
public class Game extends JFrame {
public static final int SIRKA_OKNA = 1000;
public static final int VYSKA_OKNA = 600;
public static final int SIRKA_KARTY = SIRKA_OKNA/15;
public static final int VYSKA_KARTY = VYSKA_OKNA/10;
public static final int MEDZERA_OBJEKTOV = SIRKA_KARTY/20;
public static final int SIRKA_POSUVACA = SIRKA_KARTY/3;
private final ArrayList <JButton> mainMenu;
private final Font typPisma;
private static Game game;
private Table table;
private String nameOfTheWinner;
private Panel panel;
public static void main(String args[]){
Game.game=new Game();
game.setPreferredSize(new Dimension(Game.SIRKA_OKNA,Game.VYSKA_OKNA));
game.setUndecorated(true);
game.setResizable(false);
game.pack();
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setLocationRelativeTo(null);
game.setVisible(true);
}
public Game(){
typPisma = new Font("TimesRoman", Font.PLAIN, 10);
mainMenu = new ArrayList<>();
GridLayout rozlozenie = new GridLayout(6,2,50,50);
JButton newGame = new JButton("New Game");
JButton exitGame = new JButton("Exit Game");
this.setLayout(rozlozenie);
this.hlavneMenu.add(newGame);
this.add(newGame);
newGame.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
game.newGame();
game.zmenStavMenu(false);
}
});
hlavneMenu.add(exitGame);
this.add(exitGame);
exitGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
}
class Panel extends JPanel {
private Table table;
private Game game;
Panel(Table table,Game game) {
this.table= table;
this.game = game;
String menoPrvehoHraca = JOptionPane.showInputDialog(this,"Ako sa volá hráč č.1?");
String menoDruhehoHraca = JOptionPane.showInputDialog(this,"Ako sa volá hráč č.2?");
this.table= new table(this.game,this,10,menoPrvehoHraca,menoDruhehoHraca);
game.setLayout(new BorderLayout());
game.add(this, BorderLayout.CENTER);
System.out.println("Vytvoril sa panel");
}
public void paintComponent(Graphics g) {
if (this.table!= null) {
g.setFont(typPisma);
this.table.paint(g);
}
if(game.nameOfTheWinner!= null) {
g.setColor(Color.white);
g.fillRect(0, 0, Game.SIRKA_OKNA, Game.VYSKA_OKNA);
}
}
}
public void endOfTheGame(String nameOfTheWinner) {
JOptionPane.showMessageDialog(null,nameOfTheWinner+ " vyhral hru");
this.nameOfTheWinner= nameOfTheWinner;
this.panel.repaint();
this.table= null;
this.panel = null;
this.zmenStavMenu(true);
}
private void newGame(){
this.panel = new Panel(this.table,this);
this.nameOfTheWinner= null;
}
private void changeState(boolean state) {
for(JButton currentButton: this.mainMenu){
currentButton.setVisible(state);
currentButton.setEnabled(state);
}
}
}
答案 0 :(得分:0)
好的,我发现了错误。我一直有一个mouseListener的实例,它保留了另一个永不停止存在的按钮,所以当我第二次关闭游戏时,游戏之前和下一个按钮都开始了,问题是某些物体不再存在,所以我得到一个零点