Reputation: 347
I am creating a Sudoku game. The game has some buttons such as new game, re-start, solve, etc.
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;
}
The problem that I am having is that when I press, let's say the solve button and then the new game button, the new game that appears gets all funny and doesn't work as it should.
I'm just learning JavaSwing, so any help would be greatly appreciated! :)
Upvotes: 0
Views: 1305
Reputation: 5395
I think it is because actually you're not removing a JPanel
with previously used sudoku board, before adding another one. Try:
panel.remove(sudoku);
sudoku = PanelSudoku();
panel.add(sudoku);
panel.revalidate();
panel.repaint();
Right now you add new sudokuNuevo
on a top of another.
Upvotes: 3