2012-05-24 49 views
2

好吧,我有一個Jpanel,它在下面的圖像中使用覆蓋佈局顯示爲白色。它包含一個ScrollPane來保存一個圖像(「無圖像可用」)和一個JButton(「註釋」)。JButton定位問題

enter image description here

我想在JPanel的右下角來定位該按鈕。我嘗試了多種佈局方法,但似乎無法使其發揮作用。最多的按鈕移動了東南方向的3/4,我不知道爲什麼。

任何幫助是極大的讚賞..

+0

你能給什麼佈局是您使用的經理,什麼佈局,你已經嘗試更詳細? – Qwerky

+0

容器面板使用重疊佈局,然後添加Label(其中包含圖像)和Button。然後,我嘗試使用button.setAlignmentX()來對齊按鈕,但它無法正常工作。 我也嘗試使用sprint佈局(因爲這是用於我的其他組件)但是我又得到了奇怪的效果。就像它無法移過某個位置一樣。 – user1224534

+0

爲了儘快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

3

有很多使用不同的佈局管理器可能的解決方案。我不知道OverlayLayout,但我喜歡WindowBuilder Pro(免費):https://developers.google.com/java-dev-tools/wbpro/以獲得有關Swing設計的幫助。

使用它,我寫了一個SpringLayout實現你的問題(SpringLayout似乎是一個痛苦處理沒有GUI生成器)。

JPanel panel = new JPanel(); 
SpringLayout sl_panel = new SpringLayout(); 
panel.setLayout(sl_panel); 

JButton button = new JButton("Comments"); 
sl_panel.putConstraint(SpringLayout.SOUTH, button, 0, SpringLayout.SOUTH, panel); 
sl_panel.putConstraint(SpringLayout.EAST, button, 0, SpringLayout.EAST, panel); 
panel.add(button); 

JScrollPane scrollPane = new JScrollPane(); 
sl_panel.putConstraint(SpringLayout.NORTH, scrollPane, 5, SpringLayout.NORTH, panel); 
sl_panel.putConstraint(SpringLayout.WEST, scrollPane, 3, SpringLayout.WEST, panel); 
sl_panel.putConstraint(SpringLayout.SOUTH, scrollPane, 3, SpringLayout.SOUTH, panel); 
sl_panel.putConstraint(SpringLayout.EAST, scrollPane, 3, SpringLayout.EAST, panel); 
panel.add(scrollPane); 

JLabel lblNewLabel = new JLabel(); 
lblNewLabel.setIcon(new ImageIcon(foo.class.getResource("sSdA3.png"))); 
scrollPane.setViewportView(lblNewLabel); 

下面的代碼運行的圖片:

enter image description here

你可以看到按鈕(我的,不是你的圖片...)在滾動窗格之上浮動底部。我們可以調整上面的邊距,這樣按鈕不會浮動在滾動條的頂部,但這只是爲了向您顯示它在z軸上的位置。

+0

感謝您修復圖片鏈接@Bart。我不是無圖像用戶限制的粉絲。 :-) –

+0

不客氣,納撒尼爾。 –

+0

謝謝Nathaniel,使用SpringLayout我已經成功定位了按鈕: [Screenshot](http://i.stack.imgur.com/Pscza.jpg) 但是,我現在有另一個問題。當我使用下一個/上一個按鈕查看圖像隊列中的下一個/上一個圖像時,我使用以下方法重繪標籤: 'code'picLabel.setIcon(new ImageIcon(path));'code' 發生這種情況時,圖標將正確更改爲新圖像,但按鈕消失。我猜想新的圖像被塗在按鈕上。我該如何解決這個問題? – user1224534

1

我會建議您使用GridBagLayout而不是默認的佈局。使用GridBagLayout可以控制一切。
這裏是來幫助你的鏈接:在SOUTH位置how to use GridBagLayout

+0

謝謝,我會看看並報告回來。 – user1224534

1

我會說與在EAST位置添加的按鈕BorderLayout一個JPanel,並把該面板在主容器(有BorderLayout)。

所以,你得到:

------------- 
|   | 
|   | 
| Image | 
|   | 
|-----------| 
|______Button 
+0

謝謝,我將會看到什麼解決 – user1224534

1

檢查SpringLayout。它允許您將元素放置在距離組件的北,西,東或南一定距離處。您的代碼將是這樣的:

SpringLayout layout = new SpringLayout(); 
setLayout(layout); 
... 
add(_button); 
... 
layout.putConstraint(SpringLayout.EAST, _button, -20, SpringLayout.EAST, this); 
layout.putConstraint(SpringLayout.SOUTH, _button, -20, SpringLayout.SOUTH, this); 
3

在內容窗格CENTER位置圖像添加第一JPanel和簡單的設置佈局的JPanel的以FlowLayout.RIGHT並添加您JButton給它,現在添加此JPanelPAGE_END內容窗格的BorderLayout的位置。看看下面這個例子

import java.awt.*; 
import java.awt.event.*; 
import javax.imageio.ImageIO; 
import javax.swing.*; 

public class ApplicationCloseExample 
{ 
    private Image image; 
    private static final String HTML = 
     "<html>" + 
     "<style type'text/css'>" + 
     "body, html { padding: 0px; margin: 0px; }" + 
     "</style>" + 
     "<body>" + 
     "<img src='http://pscode.org/media/starzoom-thumb.gif'" + 
     " width=320 height=240>" + 
     ""; 

    private void displayGUI() 
    { 
     final JFrame frame = new JFrame("Application Close Example"); 

     frame.addWindowListener(new WindowAdapter() 
     { 
      public void windowClosing(WindowEvent we) 
      { 
       int result = JOptionPane.showConfirmDialog(
           frame, "Do you want to Exit ?" 
           , "Exit Confirmation : ", JOptionPane.YES_NO_OPTION); 
       if (result == JOptionPane.YES_OPTION)    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       else if (result == JOptionPane.NO_OPTION) 
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
      } 
     }); 

     JPanel contentPane = new JPanel(); 
     contentPane.setOpaque(true); 
     JLabel label = new JLabel(HTML); 
     contentPane.add(label); 

     JPanel bottomPanel = new JPanel(); 
     bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5)); 
     JButton button = new JButton("Comment"); 
     bottomPanel.add(button); 

     frame.getContentPane().add(contentPane, BorderLayout.CENTER); 
     frame.getContentPane().add(bottomPanel, BorderLayout.PAGE_END); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new ApplicationCloseExample().displayGUI(); 
      } 
     }); 
    } 
} 

這裏是輸出:

RIGHT JBUTTON

3

您使用OverlayLayout提及;你想要按鈕實際上重疊的圖像?

如果這對您不重要,請使用其他好建議之一,因爲它們更簡單。但是如果你真的想要按鈕重疊圖像,這裏有一個解決方案:使用JLayeredPane分層兩個JPanel s,依次佈置按鈕和圖像。不幸的是,JLayeredPane沒有佈局管理器,所以無論何時調整JLayeredPane的大小,都需要添加組件偵聽器來調整JPanel的大小。

這裏是一個SSCCE:

public class SSCCE { 

    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Container contentPane = frame.getContentPane(); 
     contentPane.setLayout(new BorderLayout()); 

     final JLayeredPane layeredPane = new JLayeredPane(); 
     contentPane.add(layeredPane,BorderLayout.CENTER); 

     final JPanel btnPane = new JPanel(new GridBagLayout()); 
     btnPane.setOpaque(false); 

     JButton btn = new JButton("Comment"); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.weightx = 1.0; 
     gbc.weighty = 1.0; 
     gbc.anchor = GridBagConstraints.SOUTHEAST; 
     btnPane.add(btn,gbc); 

     final JPanel lblPane = new JPanel(new GridBagLayout()); 
     lblPane.setBackground(Color.CYAN); 

     JLabel lbl = new JLabel("No Image Available"); 
     gbc = new GridBagConstraints(); 
     gbc.weightx = 1.0; 
     gbc.weighty = 1.0; 
     gbc.anchor = GridBagConstraints.CENTER; 
     lblPane.add(lbl,gbc); 

     layeredPane.add(btnPane,0); 
     layeredPane.add(lblPane,1); 
     layeredPane.addComponentListener(new ComponentAdapter() { 
      @Override 
      public void componentResized(ComponentEvent e) { 
       lblPane.setBounds(0, 0, layeredPane.getWidth(), layeredPane.getHeight()); 
       btnPane.setBounds(0, 0, layeredPane.getWidth(), layeredPane.getHeight()); 
      } 
     }); 


     frame.setSize(300,200); 
     frame.setVisible(true); 
    } 

}