如何在显示 JFrame 后更改其内容 (java)

发布于 2024-11-02 20:00:02 字数 3190 浏览 2 评论 0原文

我正在设计一个 GUI 来模拟 Nurikabe 游戏(说明此处)。我基本上有两个 JPanel,当单击控制面板(面板 2)中的按钮之一时,我想更改游戏面板(面板 1)中的按钮。

面板 1 有 36 个按钮,要么是显示数字的不可点击的按钮,要么是可点击的空白按钮,全部包含在 GridLayout 中。

面板 2 有三个按钮,新谜题、检查谜题和重置当前谜题。

我遇到的问题是,当单击“重置”或“新拼图”按钮而无需显示新窗口时,我无法弄清楚如何更改面板 1 的按钮。

有办法做到这一点吗?

代码:(我已经删除了检查拼图和重置拼图按钮)

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 < this.puzzle.length; i++) {
        for (int j = 0; j < this.puzzle[0].length; j++) {
            JButton button;
            if (this.puzzle[i][j] > 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);
}
}

I am designing a GUI to emulate a Nurikabe game (description here). 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).

Panel 1 has 36 buttons, either non-clickable buttons displaying numbers or clickable blank buttons, all contained in a GridLayout.

Panel 2 has three buttons, new puzzle, check puzzle, and reset current puzzle.

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.

Is there a way to do this?

Code: (I have removed the check puzzle and reset puzzle buttons)

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 < this.puzzle.length; i++) {
        for (int j = 0; j < this.puzzle[0].length; j++) {
            JButton button;
            if (this.puzzle[i][j] > 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);
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏天碎花小短裙 2024-11-09 20:00:02
// suppose you want to make button 2 visible and hide button 1 in panel 1 on clicking 
//   button  3 from panel 2, you can do something like this:

// add everything you want to display or not in respective panels.
// Then the contents that you dont want to get displayed,make them invisible by calling
   setVisible(false).
// Then for button 3 in panel 2, write actionlistener like this:
     button3.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
              // make the visible buttons invisible by using setVisible(false);
              // make the invisible buttons visible by using setVisible(true);
          }
     }); 
// suppose you want to make button 2 visible and hide button 1 in panel 1 on clicking 
//   button  3 from panel 2, you can do something like this:

// add everything you want to display or not in respective panels.
// Then the contents that you dont want to get displayed,make them invisible by calling
   setVisible(false).
// Then for button 3 in panel 2, write actionlistener like this:
     button3.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
              // make the visible buttons invisible by using setVisible(false);
              // make the invisible buttons visible by using setVisible(true);
          }
     }); 
烟花易冷人易散 2024-11-09 20:00:02

发布一些代码。

只要您引用了 JPanel 或 JButton,您就可以根据需要随意更改它们。您可以删除组件、添加组件等。

Post some code.

As long as you have references to the JPanels or JButtons, you are free to change them as you see fit. You can remove components, add components etc.

甜`诱少女 2024-11-09 20:00:02

您可以使用 CardLayout

You might use a CardLayout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文