简体   繁体   English

java,swing,Gridlayout问题

[英]java, swing, Gridlayout problem

I have a panel with GridLayout 我有一个带有GridLayout的面板

But when I'm trying to run the program, only the first button out of 100 is shown. 但是,当我尝试运行该程序时,仅显示100个按钮中的第一个按钮。 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) 这是整个类(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);
    }
}

This is code of CellButton 这是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;
    }
}

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; 以下测试显示了我所期望的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);
 }
}

Edit: 编辑:
I see your problem, you have overridden the getX() and getY() methods in JComponent incorrectly. 我看到了您的问题,您错误地重写了JComponentgetX()getY()方法。 Get rid of your int x and int y variables and the getX() and getY() methods, JButton already provides these. 抛弃int xint y变量以及getX()getY()方法, JButton已经提供了这些方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM