2011-06-03 77 views
2

我正在使用內部添加了JPanel的JDialog。 Jpanel的佈局是BorderLayout。現在我有一些信息和圖像要顯示在該面板中。所以我在Border.Center中添加了所有信息並在Border.south中添加了圖像。但圖像沒有正確定位。以下信息可能有助於理解:設置圖像在適當的位置在java swing中的邊框佈局

enter image description here

的圖像是在底部和水平中心顯示。但我想要的是該圖像應顯示在Border.south的左上角。我怎樣才能做到這一點?是否有可能 ?

編輯: -

public static void main(String[] args) { 
    JDialog dialog = new JDialog(); 
    dialog.setSize(350, 350); 
    JPanel panel = new JPanel(); 
    panel.setLayout(new BorderLayout()); 
    JScrollPane scroll = new JScrollPane(panel); 

    JEditorPane textPane = new JEditorPane(); 
    textPane.setContentType("text/html"); 
    StringBuffer buffer = new StringBuffer(); 
    buffer.append(String.format("<div><b>Name:</b>%s</div>", "Harry")); 
    buffer.append(String.format("<div><b>Id:</b>%s</div>", "Joy")); 
    textPane.setText(buffer.toString()); 

    panel.setBackground(Color.white); 
    panel.add(textPane,BorderLayout.CENTER); 
    JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.LEADING)); 
    JLabel lbl = new JLabel("Image will be here."); 
    lbl.setFont(new Font(Font.SANS_SERIF,0,40)); 
    jPanel.add(lbl); 
    panel.add(jPanel,BorderLayout.SOUTH); 
    dialog.add(scroll); 
    dialog.setVisible(true); 
} 

在這段代碼中我添加一個JLabel( 「形象將出現在這裏。」)而不是圖像來表示的情況。

回答

0

JPanel加上FlowLayout(FlowLayout.LEFT)加上SOUTH。將圖像添加到JPanel,它應該結束在SOUTH區域的左上角。

+0

@Andrew:通過這樣做的圖像左側,但不是頂部。我也嘗試了FlowLayout.Leading,但輸出相同。 – 2011-06-03 06:55:38

+0

@Harry:我可以問:你還有什麼添加到'JPanel'?或者'JPanel'的首選大小是什麼?或其他十幾個問題,但我會說 - 爲了更快提供更好的幫助,請發佈[SSCCE](http://pscode.org/sscce.html)。對於這種性質的SSCCE,您可以將圖像替換爲想要表示圖像的組件,使用代碼生成圖像,或熱鏈接到網絡上可用的圖像(例如,在我的[媒體頁面](http:// http:// pscode.org/media/#image))。 – 2011-06-03 07:54:47

+0

@Andrew:看我的編輯。 – 2011-06-03 08:18:08

0

如果有人知道TableLayout,那很容易做到這一點。

double p = TableLayout.PREFERRED; 
double f = TableLayout.FILL; 
double[][] sizes = { 
    {p,f}, 
    {p,p,f} 
}; 

panel.setLayout(new TableLayout(sizes)); 

panel.add(textPane, "0,0, 1,0"); 
panel.add(jPanel, "0,1");