2011-11-22 143 views
1

我正在編寫一個獨立的應用程序,使用幾個具有不同佈局的JPanel來安排用戶界面。 現在我的問題是,當我採取窗口的上部(它是一個在另一個使用邊框佈局的面板內的邊框佈局中的面板),爲了添加一個擴展面板的類是爲了在上面繪製圖標我的窗口的頂部(繪製在面板上)。問題在於佈局切割了圖標的一部分,換句話說,將面板縮小到一定的大小。 我試圖更改爲flowlayout和其他人,但是是一樣的...所以我想問一個選項,告訴佈局,容器(面板或其他人)不能設置爲一個尺寸低於給定的大小存在?其他建議也都會幫助,但請記住,我正在嘗試在GUI中添加帶有最小改變的圖標。使用java佈局定義容器的最小尺寸

感謝讀這篇文章,幫助 卡察夫

+0

如果你的GUI有嵌套JPanels然後發佈一個http://sscce.org/那演示了你的問題,有一些方法如何設置任何setXxxSize而不聲明Container/JPanel/JComponents setXxxSize :-) – mKorbel

回答

1

集裝箱可裝爲的minimumSize JComponent的,簡單的例子,

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class CustomComponent extends JFrame { 

    private static final long serialVersionUID = 1L; 

    public CustomComponent() { 
     setTitle("Custom Component Graphics2D"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    public void display() { 
     add(new CustomComponents());// 
     pack(); 
     // enforces the minimum size of both frame and component 
     setMinimumSize(getSize()); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     CustomComponent main = new CustomComponent(); 
     main.display(); 
    } 
} 

class CustomComponents extends JPanel { 

    private static final long serialVersionUID = 1L; 

    @Override 
    public Dimension getMinimumSize() { 
     return new Dimension(100, 100); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(400, 300); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     int margin = 10; 
     Dimension dim = getSize(); 
     super.paintComponent(g); 
     g.setColor(Color.red); 
     g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2); 
    } 
} 
+0

非常感謝你的快速回復。 – moshe