简体   繁体   English

未显示JComponent,未调用paintComponent

[英]JComponent not being displayed, paintComponent not being called

I have a class which extends JComponent called Cell that doesn't get displayed, and its paintComponent is never called. 我有一个扩展了JComponent的类,称为Cell,它不会显示,并且它的paintComponent永远不会被调用。 Here is a short checklist of things I have done in an attempt to get it working: 这是我为使之正常工作所做的简短清单:

  • Cell constructor has this.setPreferredSize , this.setMaximumSize and this.setMinimumSize 单元构造函数具有this.setPreferredSizethis.setMaximumSizethis.setMinimumSize
  • The object (extension of JPanel ) that holds the cell calls this.add(cell) for each cell as well as this.validate() 持有单元格的对象( JPanel扩展名this.add(cell)为每个单元格以及this.validate()调用this.add(cell) this.validate()
  • The JPanel's that hold the other JPanels all call .validate() . JPanel's持有其他JPanels所有呼叫.validate()
  • The primary JPanel is added to the contentPane of the JFrame and the JFrame .pack() is called. 将主JPanel添加到JFramecontentPane中,并调用JFrame .pack()

GUIFrontend.java GUIFrontend.java

import java.awt.*;
public class GUIFrontend extends JFrame{
    private static final long serialVersionUID = -7074700257172600349L;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                GUIFrontend g = new GUIFrontend();
            }
        });
    }
    public GUIFrontend(){
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel primary = new JPanel();

        //Map
        JPanel mapContainer = new JPanel();
        mapContainer.setMinimumSize(new Dimension(772, 600));
        GTGrid gtg = new GTGrid();
        gtg.validate();
        mapContainer.add(gtg);
        mapContainer.validate();

        JPanel controls = new JPanel();
        controls.setBackground(Color.green);
        controls.setPreferredSize(new Dimension(160,600));

        primary.add(mapContainer);
        primary.add(controls);
        primary.validate();
        this.validate();
        this.getContentPane().add(primary);
        this.pack();
        this.validate();
        this.setVisible(true);
    }

}

GTGrid.java GTGrid.java

public class GTGrid extends Grid {
    private static final long serialVersionUID = -2787182463097088611L;

    public GTGrid() {
        this.height =  150;
        this.width = 193;
        //Minimum of 4x4 per cell
        this.setMinimumSize(new Dimension(width*4,height*4));
        GridLayout gl;
        this.setLayout(gl = new GridLayout(width,height));
        gl.setVgap(0);
        gl.setHgap(0);
        this.vmax = 2;
            //this.add(new JButton("Hello, World!"));
        this.intersections = new ArrayList<IntersectionController>();
        this.cells = new Cell[height][width];
        for(int dt=0;dt<this.height;dt++){
            for(int dl=0;dl<this.width;dl++){
                this.cells[dt][dl] = new Cell(dt,dl,this);
                this.cells[dt][dl].setDirection((dt%2==0)?Direction.East:Direction.West);
                //Paint detail
                this.add(cells[dt][dl]);
                this.validate();
            }
        }
    }
}

Cell.java (this method is huge, so only selections have been added) Cell.java (此方法很大,因此仅添加了选择)

    public Cell(int dt, int dl, Grid parent){
        //some deleted code unrelated to JComponent

        //Methods for Paint
        this.setPreferredSize(new Dimension(4,4));
        this.setMaximumSize(new Dimension(10,10));
        this.setMinimumSize(new Dimension(4,4));
    }
public void paintComponent(Graphics g){
    super.paintComponent(g);
    System.out.println("I'm printing!");
    Color oldColor = g.getColor();

    //Draw black border around cell;
    g.setColor(Color.black);
    g.drawRect(0, 0, this.getWidth(), this.getHeight());

    //Draw yellow line indicating direction of cell
    this.drawYellowLine(g);

    //Draw car if the cell has one
    if (this.hasCar())
        this.drawCar(g);

    g.setColor(oldColor);
}

Is there a problem with adding too many elements to that layout manager? 向该布局管理器添加太多元素是否存在问题? (there would be 150*193=28,950 components) (将有150 * 193 = 28,950个组件)

One layout manager has a hardcoded limit of 512 components. 一个布局管理器的硬编码限制为512个组件。 Don't remember which one it is. 不记得是哪一个。

When you use a GridLayout the components are all resized to fit the space avaialable to the entire grid. 使用GridLayout时,所有组件的大小都会进行调整,以适合整个网格可用的空间。 Maybe not enough spaces is available so the actual size of each cell becomes (0, 0), in which case the paint method will never be called on the component. 也许没有足够的可用空间,所以每个像元的实际大小变为(0,0),在这种情况下,永远不会在组件上调用paint方法。

The problem got resolved by removing the getWidth() and getHeight() methods from my Grid class (Which GTGrid extends). 从我的Grid类中删除了getWidth()getHeight()方法,从而解决了该问题(扩展了GTGrid)。 These were old methods that I rarely used anymore, and apparently they kept the GTGrid from being drawn because they were initialized to zero. 这些是我不再使用的旧方法,而且由于将它们初始化为零,因此显然它们无法绘制GTGrid。 In addition, I had to remove getWidth() & getHeight() from Cell.java 另外,我必须从Cell.java中删除getWidth()getHeight()

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

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