重绘其他组件时,JLabel会调整大小

时间:2012-02-03 22:53:03

标签: java swing layout-manager concurrency worldwind

我的Java布局有问题。我正试图将JLabel放在最顶层的五个WorldWindowGLCanvas网格中。类似的东西:

------------------------------------------------
                     JLabel
------------------------------------------------
                       |
         WWCanvas      |       WWCanvas
                       |

------------------------------------------------
                       |
         WWCanvas      |       WWCanvas
                       |

我目前在JPanel中使用GridBagLayout。程序首次启动时,一切都看起来应该如此。然而,在第一次重绘所有WorldWindowGLCanvas时,顶级JLabel的大小立即增加一倍并且占用太多空间。这是一些相关的代码:

JFrame f = new JFrame("ScintDisplay");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridBagLayout());

        JLabel timeLabel = new JLabel(getTime(), JLabel.CENTER);
        timeLabel.setSize(new Dimension(200,200));


        WorldWindowGLCanvas wwd1 = new WorldWindowGLCanvas();
        wwd1.setPreferredSize(new java.awt.Dimension(960, 1000));

        WorldWindowGLCanvas wwd2 = new WorldWindowGLCanvas();
        wwd2.setPreferredSize(new java.awt.Dimension(500, 480));

        WorldWindowGLCanvas wwd3 = new WorldWindowGLCanvas();
        wwd3.setPreferredSize(new java.awt.Dimension(500, 480));

        WorldWindowGLCanvas wwd4 = new WorldWindowGLCanvas();
        wwd4.setPreferredSize(new java.awt.Dimension(500, 480));

        WorldWindowGLCanvas wwd5 = new WorldWindowGLCanvas();
        wwd5.setPreferredSize(new java.awt.Dimension(500, 480));

        wwd1.setModel(new BasicModel());
        wwd2.setModel(new BasicModel());
        wwd3.setModel(new BasicModel());
        wwd4.setModel(new BasicModel());
        wwd5.setModel(new BasicModel());

        addComponent(f, wwd2, 0, 2, 1, 4, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        addComponent(f, wwd1, 1, 2, 2, 8, GridBagConstraints.CENTER, GridBagConstraints.BOTH);            
        addComponent(f, wwd3, 3, 2, 1, 4, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        addComponent(f, wwd4, 0, 6, 1, 4, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        addComponent(f, wwd5, 3, 6, 1, 4, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        addComponent(f, timeLabel, 0, 0, 4, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);

        f.setSize(1920, 1200);
        f.setVisible(true);


        double lat1 = getTimeLon();

        for( ; ; )
        {
            //System.out.println(lat);
            timeLabel.setText(getTime());
            System.out.println(timeLabel.getSize());
            wwd1.getView().setEyePosition(Position.fromDegrees(0, lat1, 10500000));
            wwd2.getView().setEyePosition(Position.fromDegrees(0, 0, 23500000));
            wwd3.getView().setEyePosition(Position.fromDegrees(0, 0, 23500000));
            wwd4.getView().setEyePosition(Position.fromDegrees(0, 0, 23500000));
            wwd5.getView().setEyePosition(Position.fromDegrees(0, 0, 23500000));
            wwd1.redraw();
            wwd2.redraw();
            wwd3.redraw();
            wwd4.redraw();
            wwd5.redraw();
            Thread.sleep(30000);
            lat1 = getTimeLon();
        }

     private static void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, int anchor, int fill)
    {
            GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0, anchor, fill, insets, 0, 0);
            container.add(component, gbc);
    }

我从未使用之前重绘的GL组件完成GUI。我错过了什么吗?这是一个布局问题。或者与重绘有关?

谢谢, -B

1 个答案:

答案 0 :(得分:5)

你试图让GridBagLayout做太多,而应该使用嵌套的JPanel来实现这一点。让中心JPanel使用GridBagLayout,当然,让它由另一个使用BorderLayout的JPanel持有。将中心面板添加到BorderLayout.CENTER位置,将JLabel添加到主BorderLayout - 使用JPanel的BorderLayout.NORTH或BorderLayout.PAGE_START位置。还要确保JLabel已设置为以文本为中心显示。

此外,从Swing线程的角度来看,你的代码看起来很危险。这个非停止循环和Thread.sleep(...)似乎在Swing事件线程或EDT上被调用,这可能导致应用程序冻结。考虑使用SwingWorker来允许在后台线程上调用此代码。