2013-07-29 25 views
2

我有一個面板格式化爲GridBagLayout。即使我使用setMinimumSize來設置尺寸,它仍然太小。當您縮小窗口(只需使用鼠標拖動)時,它會超過標籤的大小,以便標籤顯示爲firs...。但是,只有最左邊的標籤會像這樣縮小。無法停止面板變得太小

但是,正如您從運行下面的代碼所看到的那樣,面板仍然有可能脫離窗口的邊緣。我沒關係的行爲,我只是不希望標籤的文字被搞砸了。澄清:我想既不標籤收縮,我希望面板滑下週圍面板的邊緣。

這是我能想出,在某種程度上這是很容易看到再現這種行爲最簡單的例子:

import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 

import javax.swing.Box; 
import javax.swing.BoxLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 

public class TestMain { 
    private JFrame frame; 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        TestMain window = new TestMain(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public TestMain() { 
     initialize(); 
    } 

    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 450, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel outer = new JPanel(); 
     outer.setLayout(new BoxLayout(outer, BoxLayout.Y_AXIS)); 
     outer.setBorder(new EmptyBorder(30,30,30,30)); 
     outer.setBackground(Color.DARK_GRAY); 

     JPanel inner = new JPanel(); 
     inner.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 

     JLabel labelOne = new JLabel(); 
     labelOne.setText("first label"); 
     c.anchor = GridBagConstraints.LINE_END; 
     inner.add(labelOne, c); 

     JLabel labelTwo = new JLabel(); 
     labelTwo.setText("second label"); 
     c.anchor = GridBagConstraints.LINE_START; 
     c.gridx = 2; 
     inner.add(labelTwo, c); 

     c.gridx = 1; 
     c.weighty = 1.0; 
     inner.add(Box.createHorizontalStrut(100), c); 

     outer.add(inner); 
     frame.add(outer); 
    } 
} 

運行這段代碼,然後水平調整窗口大小。最終窗口被調整得足夠小,標籤變得混亂。

+0

您可以嘗試設置ipadx/ipady約束來定義最小可能的單元大小。 – StanislavL

回答

2

我不知道GridBagLayout爲什麼會這樣做。但是如果你給每個標籤一個weightx = 1.0,它們都會縮小。

簡單的解決方案是使用FlowLayout。組件始終以其首選大小顯示。

+0

我不想縮小。 – durron597

+0

我明白你想要什麼,但是GridBagLayout似乎不是以這種方式工作的,因爲它被設計爲基於可用空間動態縮小/增長組件。使用更簡單的佈局管理器。 – camickr

+0

不幸的是,我需要更復雜的佈局管理器,因爲我的真實應用程序以更復雜的方式進行佈局。我希望包裹的面板或其他東西可以解決它。 – durron597

2

根據@ camrickr的評論,我嘗試在FlowLayout面板中包裹內部面板,否則它什麼也不做,並修復它。

import java.awt.Color; 

public class TestMain { 
    private JFrame frame; 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        TestMain window = new TestMain(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public TestMain() { 
     initialize(); 
    } 

    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 450, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel outer = new JPanel(); 
     outer.setLayout(new BoxLayout(outer, BoxLayout.Y_AXIS)); 
     outer.setBorder(new EmptyBorder(30,30,30,30)); 
     outer.setBackground(Color.DARK_GRAY); 

     JPanel middleFlowLayout = new JPanel(); // Added this wrapping panel 

     JPanel inner = new JPanel(); 
     inner.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 

     JLabel labelOne = new JLabel(); 
     labelOne.setText("first label"); 
     c.anchor = GridBagConstraints.LINE_END; 
     inner.add(labelOne, c); 

     JLabel labelTwo = new JLabel(); 
     labelTwo.setText("second label"); 
     c.anchor = GridBagConstraints.LINE_START; 
     c.gridx = 2; 
     inner.add(labelTwo, c); 

     c.gridx = 1; 
     c.weighty = 1.0; 
     inner.add(Box.createHorizontalStrut(100), c); 

     middleFlowLayout.add(inner); // Wrap the inner pannel 
     outer.add(middleFlowLayout); 
     frame.add(outer); 
    } 
}