2012-06-14 50 views
0

我有一個標籤可以顯示來自「0 ft」的字符串來表示「999 ft」,因此最大需要6個字符串。我使用網格佈局我可以將該標籤放在應該是的特定列中。我也想要標籤的背景。但問題是我不能正確設置背景的大小。無法更改GridLayout中的JLabel的大小

I want to size down the black part to only the required amount which im not ablet to do

public Controller() { 
    super(new GridLayout(9, 9)); 
    try { 
     Background = ImageIO.read(getClass().getResourceAsStream(
       "background.jpg")); 
     TestImage = ImageIO.read(getClass().getResourceAsStream(
       "VTOLHorizontalLevelIndicator.png")); 
     setPreferredSize(new Dimension(Background.getWidth(null), 
       Background.getHeight(null))); 

    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 

    for (Direction direction : Direction.values()) { 
     directionMap.put(direction, false); 
    } 
    setKeyBindings(); 
    Timer timer = new Timer(TIMER_DELAY, new TimerListener()); 
    timer.start(); 
    int i = 3; 
    int j = 11; 
    JLabel[][] panelHolder = new JLabel[i][j]; 
    setLayout(new GridLayout(i, j)); 

    for (int m = 0; m <= 2; m++) { 
     for (int n = 0; n <= 10; n++) { 
      if (m == 1 && n == 1) { 
       lblAltitude.setFont(new Font("Gotham Bold",1,12)); 
       lblAltitude.setToolTipText("Altitude"); 
       lblAltitude.setOpaque(true); 
       lblAltitude.setBackground(Color.black); 
       lblAltitude.setMinimumSize(new Dimension(50,25)); 
       add(lblAltitude); 
      } else { 
       panelHolder[m][n] = new JLabel(" "); 
       add(panelHolder[m][n]); 
      } 
     } 
    } 

} 

上面的代碼顯示了我從中初始化的網格佈局和標籤的構造。 我使用正確的佈局嗎?我主要關注的是讓標籤放在正確的位置,可能不是網格方式,而是在此GUI中的特定座標處。

回答

4

GridLayout將展開所有組件以填充面板(使每個組件的大小相同)。

+0

嘗試使用其他佈局類型,如'FlowLayout',玩了一下,看看有什麼最適合你。它很難詳細描述你應該做什麼,因爲只有你確切地知道你想要它的外觀和感覺。 –

+0

可以說,我需要設置在特定的座標一些的JLabel在上面的圖形用戶界面,它是完全必要使用佈局,有沒有用,我可以設置labesl的位置的任何解決方案? (原諒我,如果我問上面的任何愚蠢的問題,即時通訊與這些佈局和搖擺的東西一個小白,這樣下去容易對我) – md1hunox

+1

你絕對可以使用絕對定位,但請記住,當你或你的用戶,調整窗口,組件可能會隱藏或阻塞。嘗試使用佈局。 –