2012-02-03 40 views
3

我遇到了Java佈局問題。我試圖通過五個WorldWindowGLCanvas的網格將JLabel放置在最頂端的後面。喜歡的東西:JLabel在重繪其他組件時調整大小

------------------------------------------------ 
        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

回答

5

你想問的GridBagLayout做太多,而是應該使用嵌套JPanels實現這一目標。當然,讓中心JPanel使用GridBagLayout,但讓它由另一個使用BorderLayout的JPanel保存。將中央面板添加到BorderLayout.CENTER位置,將JLabel添加到主BorderLayout - 使用JPanel的BorderLayout.NORTH或BorderLayout.PAGE_START位置。還要確保JLabel已被設置爲顯示文本居中。

另外,你已經有了從Swing線程的角度看起來很危險的代碼。該非停止循環和Thread.sleep(...)似乎在Swing事件線程或EDT上被調用,並且這可能會導致您的應用程序凍結。考慮使用SwingWorker來允許在後臺線程上調用此代碼。

+0

+1 rel。示例如何Thread.sleep(int)塊EDT http://stackoverflow.com/a/7944388/714968 – mKorbel 2012-02-03 23:06:54

+0

啊謝謝。那就是訣竅。我仍然習慣於GUI的東西。我也會研究這個線程的背景。感謝您的高舉。 – Brandon 2012-02-03 23:11:18

+0

@Brandon:不客氣。我認爲GridBagLayout的部分問題在於,您要給所有組件提供相同的weightx和weighty常量,因此當GUI擴展時,所有組件都會增加。你使用相同的錨和填充。我自己,我討厭使用GridBagLayout,並避免它,除非必要。 – 2012-02-03 23:18:20

相關問題