有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用户界面什么是适合我的GUI(JAVA)的update()方法

我正在为新游戏显示一个8x8网格的图片,这些图片是使用二维数组和数学随机放置的。随机的

还有一个“新游戏”按钮,我希望用户能够刷新页面,而无需打开新的GUIenter image description here并重新显示网格

这是我显示网格的方法

for (int r = 0; r < ShinyButtons.ROWS; r++){
        for (int c = 0; c < ShinyButtons.ROWS; c++) {

              newbuttonTable[r][c] =  new JButton(icons[(int) (Math.random()*7)]);
              newbuttonTable[r][c].setLocation(10+c*69, 10+r*69);
              newbuttonTable[r][c].setSize(69, 69);
              add(newbuttonTable[r][c]);


    }

共 (1) 个答案

  1. # 1 楼答案

    "I want the user to be able to refresh the page without opening a new GUI"

    对于此功能,应该使用CardLayout。您可以使用show(p, "aCertainPanel")next()previous()等方法来“堆叠”面板并在其中导航

    请参见How to use CardLayout和一个简单的可运行示例here

    正如Marco13所指出的,避免使用空布局和手动设置位置和大小。这会产生许多问题。对于您的特定情况,看起来GridLayout将是您的最佳布局。请参见如何使用GridLayout以及Laying out Components Within a Container上的其他内容