【问题标题】:I instantiate an extra duplicate and I cant find it我实例化了一个额外的副本,但我找不到它
【发布时间】:2016-12-08 14:40:32
【问题描述】:

所以这是我的班级游戏,我需要它来运行和处理新游戏。当我按下 Jbutton newGame 时,一个新游戏 Jpanel 应该开始存在,并且除了一个小细节之外应该运行一个新的循环。每次我运行游戏,完成它,然后第二次运行 KoniecHry 2 方法时,一次使用正确的面板,一次使用面板保持值为 NULL 并抛出空指针异常,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);
        }
    }    
}

【问题讨论】:

  • 您似乎正在创建一个新的 Panel 对象,但您没有将它放入我可以看到的任何 GUI 中。它只是挂在那里。
  • 我将它添加到面板构造函数的框架中我错了吗?用这个命令 game.add(this, BorderLayout.CENTER);
  • 啊,我明白了,它进入了没有显示给我们的代码,一些名为 Stol 的类。
  • true,并且当类 stol 完成时(游戏会话已经结束),我从游戏中调用 KoniecHry 方法来取消对它的引用
  • 为了获得更好的帮助,请发布minimal reproducible example,这是一个我们可以编译、运行、测试和修改的小程序,它足够小,可以作为代码格式的文本发布在这里,并且只有代码与您的问题相关,并允许您的程序编译和运行。

标签: java swing duplicates


【解决方案1】:

好的,我发现了错误。我一直有一个 mouseListener 实例,它使另一个按钮保持活动状态,该按钮永远不会停止存在,所以当我第二次关闭游戏时,之前游戏中的两个按钮和下一个按钮都开始了,问题是某些对象不再存在所以我得到一个零点

【讨论】:

    猜你喜欢
    • 2020-05-28
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 2022-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多