java中jframe怎么改位置_如何在显示后更改JFrame的内容(java)

我正在设计一个GUI来模拟Nurikabe游戏(描述here)。我基本上有两个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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值