JPanel重新验证并重新绘制无法正常工作(Java Swing)

时间:2015-05-17 20:27:14

标签: java swing jpanel

我正在创建一个数独游戏。游戏中有一些按钮,如新游戏,重新启动,解决等等。

public void inicio() {

    JPanel panel = new JPanel();

    panel.setLayout(null);
    panel.setPreferredSize(new Dimension(735, 620));

 ...

 button2 = new JButton("New Game"); //Other buttons like this one

 ...

 JPanel sudoku = PanelSudoku();
 sudoku.setBounds(228, 90, 470, 470);

 ...
    //Botón nuevo juego
    button2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {

            ///AQUÍ CÓDIGO  

            setGame();
            panel.remove(sudoku);
            JPanel sudokuNuevo = PanelSudoku();
            sudokuNuevo.setBounds(228, 90, 470, 470);
            panel.add(sudokuNuevo);
            panel.revalidate();
            panel.repaint();


            ///

        }
    });

    //Botón reiniciar juego
    button3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {

            ///AQUÍ CÓDIGO  

            panel.remove(sudoku);
            JPanel sudokuNuevo = PanelSudoku();
            sudokuNuevo.setBounds(228, 90, 470, 470);
            panel.add(sudokuNuevo);
            panel.revalidate();
            panel.repaint();

            ///

        }
    });

    //Botón Comprobar
    button4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {

            ///AQUÍ CÓDIGO

            SwingUtilities.invokeLater(new Runnable() {

                public void run() {

                    VentanaKeepTrying ejecutable = new VentanaKeepTrying();
                    //VentanaWinner ejecutable = new VentanaWinner();
                    ejecutable.setVisible(true);

                }
            });

            ///

        }
    });

    //Botón resolver
    button5.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {

            ///AQUÍ CÓDIGO

            SwingUtilities.invokeLater(new Runnable() {

                public void run() {

                    panel.remove(sudoku);
                    JPanel sudokuNuevo = PanelSudokuResuelto();
                    sudokuNuevo.setBounds(228, 90, 470, 470);
                    panel.add(sudokuNuevo);
                    panel.revalidate();
                    panel.repaint();

                }
            });

            ///

        }

  ...

  //Agregar elementos al panel
    panel.add(imagenSudoku);
    panel.add(imagenMenu);
    panel.add(combo1);
    panel.add(button2);
    panel.add(button3);
    panel.add(button4);
    panel.add(button5);
    panel.add(button6);
    panel.add(button7);
    panel.add(button8);
    //panel.add(buttonR);
    panel.add(sudoku);
    panel.add(imagenV2);

    //Agregar panel
    add(panel);

    pack();

    setSize(735, 620);
    setTitle("Sudoku - ©2015 Games V²");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
}

....

 public JPanel PanelSudoku() {

    JPanel panel = new JPanel();

    //panel.setLayout(null);
    panel.setPreferredSize(new Dimension(470, 470));

    JPanel panelSudoku = new JPanel();
    panelSudoku.setPreferredSize(new Dimension(470, 470));
    //panelSudoku.setBackground(Color.WHITE);

    //Espacio en el borde
    panelSudoku.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));

    //Número de filas, columnas, espacio horizontal, espacio vertical.
    panelSudoku.setLayout(new GridLayout(9, 9, 12, 12));

    //Llenar Sudoku con Array del Nível
    for (int i = 0; i < b1.size(); i++) {

        //Casilla vacia
        if (b1.get(i).equals(".")) {

            JTextPane textPane = new JTextPane();
            textPane.setText("");
            textPane.setEditable(true);
            textPane.setFont(new Font("RockoFLF", Font.BOLD, 24));

            //Color azul para la región
            if (i == 0 || i == 1 || i == 2 || i == 9 || i == 10 || i == 11 || i == 18 || i == 19 || i == 20 || i == 6 || i == 7 || i == 8 || i == 15 || i == 16 || i == 17 || i == 24 || i == 25 || i == 26 || i == 30 || i == 31 || i == 32 || i == 39 || i == 40 || i == 41 || i == 48 || i == 49 || i == 50 || i == 54 || i == 55 || i == 56 || i == 63 || i == 64 || i == 65 || i == 72 || i == 73 || i == 74 || i == 60 || i == 61 || i == 62 || i == 69 || i == 70 || i == 71 || i == 78 || i == 79 || i == 80) {
                Color colore = new Color(224, 235, 250); //Lightblue
                textPane.setBackground(colore);
            }

            //Color colore2 = new Color(234, 112, 165);
            Color colore2 = new Color(255, 127, 80); //Orange

            //Estilos - Centrar y Colores de letra
            StyledDocument doc = textPane.getStyledDocument();
            SimpleAttributeSet center = new SimpleAttributeSet();
            StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
            StyleConstants.setForeground(center, colore2);
            doc.setParagraphAttributes(0, doc.getLength(), center, false);

            //Agregar al panel
            panelSudoku.add(textPane);

            //Para sólo permitir números del 1 al 9
            textPane.addKeyListener(new KeyListener() {

                @Override
                public void keyTyped(KeyEvent e) {

                    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                }

                @Override
                public void keyPressed(KeyEvent e) {

                    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                }

                @Override
                public void keyReleased(KeyEvent e) {
                    String t = textPane.getText();
                    if (!validarStringEnteroPositivo(t)) {
                        textPane.setText("");
                    } else {

                    }
                    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                }
            });

        } 
        //Casilla con número dado al usuario
        else {

            JTextPane textPane = new JTextPane();

            //Poner el número
            textPane.setText((b1.get(i)));
            //Que no se pueda editar
            textPane.setEditable(false);
            //Estilo
            textPane.setFont(new Font("RockoFLF", Font.BOLD, 24));

            //Color azul para la región
            if (i == 0 || i == 1 || i == 2 || i == 9 || i == 10 || i == 11 || i == 18 || i == 19 || i == 20 || i == 6 || i == 7 || i == 8 || i == 15 || i == 16 || i == 17 || i == 24 || i == 25 || i == 26 || i == 30 || i == 31 || i == 32 || i == 39 || i == 40 || i == 41 || i == 48 || i == 49 || i == 50 || i == 54 || i == 55 || i == 56 || i == 63 || i == 64 || i == 65 || i == 72 || i == 73 || i == 74 || i == 60 || i == 61 || i == 62 || i == 69 || i == 70 || i == 71 || i == 78 || i == 79 || i == 80) {
                Color colore = new Color(224, 235, 250);
                textPane.setBackground(colore);
            }

            //Estilos - Centrar y Colores de letra
            StyledDocument doc = textPane.getStyledDocument();
            SimpleAttributeSet center = new SimpleAttributeSet();
            StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
            StyleConstants.setForeground(center, Color.darkGray);
            doc.setParagraphAttributes(0, doc.getLength(), center, false);

            //Agregar al panel
            panelSudoku.add(textPane);
        }
    }

    //Agregar panel
    panel.add(panelSudoku);

    add(panel);

    //Lo regresa para poderlo útilizar

    return panel;
}

我遇到的问题是,当我按下时,让我们说解决按钮,然后是新的游戏按钮,出现的新游戏变得有趣并且无法正常工作。

我刚刚学习JavaSwing,所以任何帮助都将不胜感激! :)

1 个答案:

答案 0 :(得分:3)

我认为这是因为实际上您在添加另一个数据库之前并没有删除先前使用过的数独板的JPanel。尝试:

panel.remove(sudoku);
sudoku = PanelSudoku();
panel.add(sudoku);
panel.revalidate();
panel.repaint();

现在,您可以在另一个上方添加新的sudokuNuevo