java、swing、Gridlayout问题

发布于 2024-10-11 23:05:14 字数 2137 浏览 8 评论 0原文

我有一个带有 GridLayout 的面板

,但是当我尝试运行该程序时,仅显示 100 个按钮中的第一个按钮。 此外,其余的仅当我将光标移到它们上方时才会出现。 有什么问题吗?

这是整个类(Life.CELLS=10 并且 CellButton 是扩展 JButton 的类)

public class MainLayout extends JFrame {

    public MainLayout() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(650, 750);
        setLayout(new FlowLayout());
        //setResizable(false);

        final JPanel gridPanel = new JPanel(new GridLayout(Life.CELLS, Life.CELLS));

        for (int i=0; i<Life.CELLS; i++) {
            for (int j=0; j<Life.CELLS; j++) {
                CellButton jb = new CellButton(i, j);
                jb.setPreferredSize(new Dimension(jb.getIcon().getIconHeight(), jb.getIcon().getIconWidth()));
                buttons[i][j] = jb;
                grid[i][j] = false;

                gridPanel.add(jb);
            }
        }
        add(gridPanel);
    }
}

这是 CellButton 的代码

package classes;

import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class CellButton extends JButton {

    private int x;
    private int y;
    boolean alive;
    ImageIcon icon;
    boolean next;

    // icons for grids
    final ImageIcon dead = 
        new ImageIcon(JFrame.class.getResource("/images/image1.gif"));
    final ImageIcon live = 
        new ImageIcon(JFrame.class.getResource("/images/image2.gif"));

    public CellButton(int X, int Y) {
        super();
        x = X;
        y = Y;
        alive = false;
        icon = dead;
        setIcon(icon);
    }   

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public boolean isAlive() {
        return alive;
    }

    public void relive() {
        alive = true;
        icon = live;
        setIcon(icon);
    }

    public void die() {
        alive = false;
        icon = dead;
        setIcon(icon);
    }

    public void setNext(boolean n) {
        next = n;
    }

    public boolean getNext() {
        return next;
    }

    public ImageIcon getIcon() {
        return icon;
    }
}

I have a panel with GridLayout

But when I'm trying to run the program, only the first button out of 100 is shown.
Futhermore, the rest appear only when I move the cursor over them.
What's wrong with it?

Here's the whole class(Life.CELLS=10 and CellButton is a class which extends JButton)

public class MainLayout extends JFrame {

    public MainLayout() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(650, 750);
        setLayout(new FlowLayout());
        //setResizable(false);

        final JPanel gridPanel = new JPanel(new GridLayout(Life.CELLS, Life.CELLS));

        for (int i=0; i<Life.CELLS; i++) {
            for (int j=0; j<Life.CELLS; j++) {
                CellButton jb = new CellButton(i, j);
                jb.setPreferredSize(new Dimension(jb.getIcon().getIconHeight(), jb.getIcon().getIconWidth()));
                buttons[i][j] = jb;
                grid[i][j] = false;

                gridPanel.add(jb);
            }
        }
        add(gridPanel);
    }
}

This is code of CellButton

package classes;

import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class CellButton extends JButton {

    private int x;
    private int y;
    boolean alive;
    ImageIcon icon;
    boolean next;

    // icons for grids
    final ImageIcon dead = 
        new ImageIcon(JFrame.class.getResource("/images/image1.gif"));
    final ImageIcon live = 
        new ImageIcon(JFrame.class.getResource("/images/image2.gif"));

    public CellButton(int X, int Y) {
        super();
        x = X;
        y = Y;
        alive = false;
        icon = dead;
        setIcon(icon);
    }   

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public boolean isAlive() {
        return alive;
    }

    public void relive() {
        alive = true;
        icon = live;
        setIcon(icon);
    }

    public void die() {
        alive = false;
        icon = dead;
        setIcon(icon);
    }

    public void setNext(boolean n) {
        next = n;
    }

    public boolean getNext() {
        return next;
    }

    public ImageIcon getIcon() {
        return icon;
    }
}

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

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

发布评论

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

评论(1

独行侠 2024-10-18 23:05:14

您必须在代码中执行其他操作。请发布您正在使用的确切代码。下面的测试显示了一个 10x10 的 JButton 网格,正如我所期望的那样;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GridLayoutTest
{
 public static void main(String[] args)
 {
  JFrame frame = new JFrame();
  JPanel panel = new JPanel(new GridLayout(10,10,10,10));

  for (int i=0; i<100; i++)
  {
   panel.add(new JButton(String.valueOf(i)));
  }

  frame.add(panel);

  frame.setSize(600,600);
  frame.setVisible(true);
 }
}

编辑:
我看到您的问题,您错误地覆盖了 JComponent 中的 getX()getY() 方法。删除 int xint y 变量以及 getX()getY() 方法,< code>JButton 已经提供了这些。

You must be doing something else in your code. Please post the exact code you are using. The following test shows a 10x10 grid of JButtons as I'd expect;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GridLayoutTest
{
 public static void main(String[] args)
 {
  JFrame frame = new JFrame();
  JPanel panel = new JPanel(new GridLayout(10,10,10,10));

  for (int i=0; i<100; i++)
  {
   panel.add(new JButton(String.valueOf(i)));
  }

  frame.add(panel);

  frame.setSize(600,600);
  frame.setVisible(true);
 }
}

Edit:
I see your problem, you have overridden the getX() and getY() methods in JComponent incorrectly. Get rid of your int x and int y variables and the getX() and getY() methods, JButton already provides these.

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