Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to change the contents of a JFrame after it has been displayed (java)
    primarykey
    data
    text
    <p>I am designing a GUI to emulate a Nurikabe game (description <a href="http://www.nikoli.com/en/puzzles/nurikabe/rule.html" rel="nofollow">here</a>). I basically have two JPanels, and when one of buttons in the control panel (panel 2) is clicked, I want to change the buttons in the game panel (panel 1).</p> <p>Panel 1 has 36 buttons, either non-clickable buttons displaying numbers or clickable blank buttons, all contained in a GridLayout. </p> <p>Panel 2 has three buttons, new puzzle, check puzzle, and reset current puzzle.</p> <p>The problem I am running into is that I cannot figure out how to change Panel 1's buttons when either the reset or new puzzle button is clicked without having to display a new window.</p> <p>Is there a way to do this?</p> <p>Code: (I have removed the check puzzle and reset puzzle buttons)</p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SSCCE extends JFrame { private static final int[][] puzzle1 = { { 0, 1 }, { 1, 0 } }; private static final int[][] puzzle2 = { { 1, 0 }, { 0, 1 } }; private int[][] puzzle; private JFrame frame; private JPanel gridPanel; private JPanel buttonPanel; public SSCCE(final int puzzleNum) { frame = new JFrame("SSCCE"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); if (puzzleNum == 1) { puzzle = puzzle1; } else { puzzle = puzzle2; } setLayout(new BorderLayout()); gridPanel = new JPanel(new GridLayout(2, 2)); for (int i = 0; i &lt; this.puzzle.length; i++) { for (int j = 0; j &lt; this.puzzle[0].length; j++) { JButton button; if (this.puzzle[i][j] &gt; 0) { button = new JButton("" + this.puzzle[i][j]); } else { button = new JButton(); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JButton temp = ((JButton) event.getSource()); if (temp.getBackground() == Color.BLACK) { // if the button is black, set it to white temp.setBackground(Color.WHITE); } else if (temp.getBackground() == Color.WHITE) { // if the button is white, set it to black temp.setBackground(Color.BLACK); } } }); } button.setBorderPainted(false); button.setContentAreaFilled(false); button.setOpaque(true); button.setBackground(Color.WHITE); gridPanel.add(button); } } buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); JButton changePuzzle = new JButton("New Puzzle"); changePuzzle.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { loadNewPuzzle(puzzleNum); } }); buttonPanel.add(changePuzzle); add(gridPanel, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH); setTitle("SSCCE"); setLocation(100, 100); pack(); setSize(150, 150); } private void loadNewPuzzle(int puzzleNum) { if (puzzleNum == 1) { puzzleNum = 2; } else { puzzleNum = 1; } // I know this is the wrong way to do it, but I'm not sure how // to do it. SSCCE newGame = new SSCCE(puzzleNum); newGame.setVisible(true); } public static void main(String[] args) { SSCCE game = new SSCCE(1); game.setVisible(true); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload