0
ここに私のクラスのゲームです、私はそれが新しいゲームを実行し、対処する必要があります。私がJbutton newを押すとすぐに、新しいゲームJpanelが既存のゲームを開始し、新しいサイクルがもう少し細かく実行されるはずです。毎回私はゲームを実行し、それを終了し、新しいゲームを2回実行します。KoniecHry 2、正しいパネルで1回、NULL値を保持するパネルで1回、nullpointer例外をスローする、JOptionPaneダイアログも前回のセッション名で最初に投げられたものと2回投げられたものの、一度ヌルで これらのインスタンスを複製するアイデアはありますか?私は余分な複製をインスタンス化し、私はそれを見つけることができません
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);
}
}
}
あなたは新しいPanelオブジェクトを作成しているように見えますが、表示できるGUIに配置していません。それはちょうどそこにぶら下がっている。 –
パネルのコンストラクタを使ってフレームに追加しますか?このコマンドを使ってgame.add(this、BorderLayout.CENTER); –
ああ、私たちには表示されていないコード、Stolというクラスがあります。 –