簡體   English   中英

JPanel重新驗證和重新繪制無法正常工作(Java Swing)

[英]JPanel revalidate and repaint not working properly (Java Swing)

我正在創建數獨游戲。 游戲具有一些按鈕,例如新游戲,重新啟動,解決等。

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,因此任何幫助將不勝感激! :)

我認為這是因為實際上您沒有在添加另一塊板之前刪除具有先前使用的數獨板的JPanel 嘗試:

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

現在,您可以在另一個頂部添加新的sudokuNuevo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM