当我从主框架开始使用getHeight()和getWidth()时,紧接在frame.pack()之后,我得到了真实的高度/宽度值。但是,当我在其他班级中使用它们时,它不起作用。
我已经尝试过以不同的方式组织我的主编(因为许多人在其他类似的问题中提出了建议),结果变得有些混乱,并且没有任何变化。

public static void main(String[] args) {
    JFrame frame = new JFrame("Rubik's Cube Solver");
    frame.setVisible(true);
    frame.setSize(1200,1200);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    BorderLayout layout = new BorderLayout();
    frame.setLayout(layout);
    Rubiks_Cube cube = new Rubiks_Cube();
    JPanel top = new JPanel();
    JButton scramble = new JButton("Scramble");
    JToggleButton solver = new JToggleButton("Solve");
    scramble.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            cube.scramble();
        }

    });
    solver.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            cube.solver();
        }

    });
    top.add(scramble);
    top.add(solver);

    frame.add(top, layout.PAGE_START);

    JPanel bottom = new JPanel();
    bottom.setBackground(Color.BLACK);
    JPanel green = new JPanel();
    JPanel red = new JPanel();
    JPanel blue = new JPanel();
    JPanel orange = new JPanel();
    JPanel white = new JPanel();
    JPanel yellow = new JPanel();

    bottom.setVisible(true);
    green.setVisible(true);
    red.setVisible(true);
    blue.setVisible(true);
    orange.setVisible(true);
    white.setVisible(true);
    yellow.setVisible(true);

    bottom.add(green);
    bottom.add(red);
    bottom.add(blue);
    bottom.add(orange);
    bottom.add(white);
    bottom.add(yellow);

    green.setPreferredSize(new Dimension(150,150));
    red.setPreferredSize(new Dimension(150,150));
    blue.setPreferredSize(new Dimension(150,150));
    orange.setPreferredSize(new Dimension(150,150));
    white.setPreferredSize(new Dimension(150,150));
    yellow.setPreferredSize(new Dimension(150,150));

    green.setMinimumSize(new Dimension(100,100));
    red.setMinimumSize(new Dimension(100,100));
    blue.setMinimumSize(new Dimension(100,100));
    orange.setMinimumSize(new Dimension(100,100));
    white.setMinimumSize(new Dimension(100,100));
    yellow.setMinimumSize(new Dimension(100,100));

    green.setMaximumSize(new Dimension(200,200));
    red.setMaximumSize(new Dimension(200,200));
    blue.setMaximumSize(new Dimension(200,200));
    orange.setMaximumSize(new Dimension(200,200));
    white.setMaximumSize(new Dimension(200,200));
    yellow.setMaximumSize(new Dimension(200,200));

    frame.add(bottom);
    frame.pack();

    green.add(new paintCube("green", cube));
    red.add(new paintCube("red", cube));
    blue.add(new paintCube("blue", cube));
    orange.add(new paintCube("orange", cube));
    white.add(new paintCube("white", cube));
    yellow.add(new paintCube("yellow", cube));

    System.out.print(green.getHeight); //<===== returns the real height

}}


这是我的另一堂课。

public class paintCube extends Rubiks_Cube {

private String facecolor = null;
private Rubiks_Cube cube = new Rubiks_Cube();

public paintCube(String color, Rubiks_Cube cube) {
    facecolor = color;
    cubelayout = cube.getCubeLayout();
}

    @Override
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        int a = 0;
        switch (facecolor) {
            case "green": a = 0; break;
            case "blue": a = 2; break;
            case "white": a = 4; break;
            case "orange": a = 3; break;
            case "yellow": a = 5; break;
            case "red": a = 1; break;
        }
        for (int i = 0; i <= 8; i++) {
            int b = cubelayout[a][i]/10;
            switch (b) {
                case 0: g2.setColor(Color.GREEN); break;
                case 1: g2.setColor(Color.RED); break;
                case 2: g2.setColor(Color.BLUE); break;
                case 3: g2.setColor(Color.ORANGE); break;
                case 4: g2.setColor(Color.WHITE); break;
                case 5: g2.setColor(Color.YELLOW); break;
            }
            int j = cubelayout[a][i]%10;
            switch (j) {
                case 0: g2.fillRect(0, 0, super.getWidth()/3, super.getHeight()/3); break;
                case 1: g2.fillRect(super.getWidth()/3, 0, super.getWidth()/3, super.getHeight()/3); break;
                case 2: g2.fillRect((super.getWidth()/3)*2, 0, super.getWidth()/3, super.getHeight()/3); break;
                case 3: g2.fillRect(0, super.getHeight()/3, super.getWidth()/3, super.getHeight()/3); break;
                case 4: g2.fillRect(super.getWidth()/3, super.getHeight()/3, super.getWidth()/3, super.getHeight()/3); break;
                case 5: g2.fillRect((super.getWidth()/3)*2, super.getHeight()/3, super.getWidth()/3, super.getHeight()/3); break;
                case 6: g2.fillRect(0, (super.getHeight()/3)*2, super.getWidth()/3, super.getHeight()/3); break;
                case 7: g2.fillRect(super.getWidth()/3, (super.getHeight()/3)*2, super.getWidth()/3, super.getHeight()/3); break;
                case 8: g2.fillRect((super.getWidth()/3)*2, (super.getHeight()/3)*2, super.getWidth()/3, super.getHeight()/3); break;
            }
        }

        g2.setStroke(new BasicStroke(4));
        g2.setColor(Color.BLACK);
        for (int i = 1; i <= 2; i++) {
            g2.drawLine((super.getWidth()/3)*i, super.getHeight(), (super.getWidth()/3)*i, 0);
            g2.drawLine(super.getWidth(), (super.getHeight()/3)*i, 0, (super.getHeight()/3)*i);
        }
        System.out.println(super.getHeight()); //<==== returns 1 everytime
        }}


我是新手,因此,如果发现任何其他错误,请更正。

最佳答案

在main()中,您将获得绿色的宽度/高度,绿色是您指定大小的JPanel。

在paintCube中,您将获得已添加到绿色的paintCube的宽度/高度。它由JPanel的默认布局管理器(即FlowLayout)进行布局,因为您没有指定布局管理器。您没有为该组件指定大小,因此其大小为零。

将绿色等的布局管理器更改为BorderLayout,并使用BorderLayout.CENTER向其添加paintCube,这样它将扩展为父级的大小(绿色)。

顺便说一句,您应该遵循Java约定并将类命名为PaintCube,而不是paintCube。

10-06 09:50