2011-05-20 40 views
2

我想使用Java Swing進行佈局,如下圖所示。使用可變尺寸排列按鈕的可調整大小的擺動佈局

layout sketch http://tobias.braun-abstatt.de/files/forums/buttons_sketch.png

在左邊是其通過paintComponent()的方式時,窗口大小,所述圖形自動比例繪製一個JPanel。 (這個問題不是關於那個面板的,那個已經完成了。)

現在我需要在前面提到的JPanel右邊的一些按鈕(在Photoshop中爲圖畫添加黑框)。沿着CONSTANT_FACTOR * getHeight()的線計算頂部和底部的紅色區域的高度,其旁邊應該只有空的空間。在左側的每個隔間旁邊,應該有一組按鈕,排列在相應隔間的中央(請參閱藍色線條)。

包含按鈕的JPanel知道CONSTANT_FACTOR和隔間的數量,所以應該可以將這些信息提供給佈局管理器。

我最適合使用哪種佈局管理器來實現此佈局?我已閱讀了所有不同的佈局管理器,但我無法弄清楚在這種情況下哪個或哪些組合最適合。

回答

4

例如,通過使用不同的LayoutManager,一個非常容易和簡單的容器,時間不超過15-20分鐘:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.SwingUtilities; 

public class ThinLineFrame { 
    private JFrame frame = new JFrame(); 
    private JScrollPane scrollPane; 
    private JPanel panel = new JPanel(); 
    private JPanel panelNorth = new JPanel(); 
    private JPanel panelCenter = new JPanel(); 
    private JPanel panelCenterCh1 = new JPanel(); 
    private JPanel panelCenterCh2 = new JPanel(); 
    private JPanel panelCenterCh3 = new JPanel(); 
    private JPanel panelCenterCh4 = new JPanel(); 
    private JPanel panelCenterCh5 = new JPanel(); 
    private JPanel panelSouth = new JPanel(); 

    public ThinLineFrame() { 
     panelNorth.setBackground(Color.red.darker()); 
     panelNorth.setPreferredSize(new Dimension(80, 30)); 
     // 
     panelCenter.setBackground(Color.darkGray); 
     panelCenter.setLayout(new GridLayout(5, 1, 2, 2)); 
     // 
     panelCenterCh1.setLayout(new BorderLayout()); 
     JButton panelCenterCh1Button = new JButton(); 
     panelCenterCh1.add(panelCenterCh1Button, BorderLayout.CENTER); 
     // 
     JButton panelCenterCh2Button1 = new JButton(); 
     JButton panelCenterCh2Button2 = new JButton(); 
     panelCenterCh2.setLayout(new GridLayout(2, 1, 2, 2)); 
     panelCenterCh2.add(panelCenterCh2Button1); 
     panelCenterCh2.add(panelCenterCh2Button2); 
     // 
     JButton panelCenterCh3Button1 = new JButton(); 
     JButton panelCenterCh3Button2 = new JButton(); 
     panelCenterCh3.setLayout(new GridLayout(2, 1, 2, 2)); 
     panelCenterCh3.add(panelCenterCh3Button1); 
     panelCenterCh3.add(panelCenterCh3Button2); 
     // 
     JButton panelCenterCh4Button1 = new JButton(); 
     JButton panelCenterCh4Button2 = new JButton(); 
     panelCenterCh4.setLayout(new GridLayout(2, 1, 2, 2)); 
     panelCenterCh4.add(panelCenterCh4Button1); 
     panelCenterCh4.add(panelCenterCh4Button2); 
     // 
     panelCenterCh5.setLayout(new BorderLayout()); 
     JButton panelCenterCh5Button = new JButton(); 
     panelCenterCh5.add(panelCenterCh5Button, BorderLayout.CENTER); 
     // 
     panelCenter.add(panelCenterCh1); 
     panelCenter.add(panelCenterCh2); 
     panelCenter.add(panelCenterCh3); 
     panelCenter.add(panelCenterCh4); 
     panelCenter.add(panelCenterCh5); 
     // 
     panelSouth.setBackground(Color.red.darker()); 
     panelSouth.setPreferredSize(new Dimension(80, 30)); 
     // 
     panel.setLayout(new BorderLayout(2, 2)); 
     panel.add(panelNorth, BorderLayout.NORTH); 
     panel.add(panelCenter, BorderLayout.CENTER); 
     panel.add(panelSouth, BorderLayout.SOUTH); 
     // 
     scrollPane = new JScrollPane(panel); 
     frame.add(scrollPane, BorderLayout.CENTER); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setPreferredSize(new Dimension(80, 600)); 
     frame.setLocation(100, 150); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String args[]) { 
     SwingUtilities.invokeLater(new Runnable() { 

      public void run() { 
       ThinLineFrame dlg = new ThinLineFrame(); 
      } 
     }); 
    } 
} 
+0

+1好的。我實際上是在寫我的答案,但是我對[我在這裏描述的問題]感到惱火(http://stackoverflow.com/questions/6072956/how-to-correct-center-gridlayout-using-standard-java-佈局經理)你可以@mKorbel做些什麼? – Boro 2011-05-20 13:56:35

+0

謝謝。不過,我的主要問題仍然沒有解決。 1.如何使按鈕成爲固定尺寸,比如25x25,並且仍然保持隔間大小相同? button.setSize(25,25)完全沒有效果。 2.例如,如何將紅色區域的高度設置爲'0.05 * frame.getHeight()'?如果我在構造函數中這樣做,調整窗口大小時不會調整紅色區域的大小。 – tajmahal 2011-05-20 14:07:44

+0

同意,那麼最簡單的方法是使用GrigBagLaout(對於childs JPanels)並檢查此示例http://www.java2s.com/Tutorial/Java/0240__Swing/1480__GridBagConstraints.htm如何在JPanel中真正使用約束(fe JButton) .anchor = GridBagConstraints.CENTER; – mKorbel 2011-05-20 14:36:48

2

你應該嘗試尋找MigLayout。這是一個超級靈活的LayoutManager,它也非常簡單。

的代碼看起來是這樣的:

MigLayout layout = new MigLayout("flowy"); 
panel.setLayoutManager(layout); 
panel.add(button1); 
panel.adD(button2); 
etc.. 

嘗試增加調試,flowy到構造得到是怎麼回事的視覺理念。

+0

我可以告訴MigLayout使button1例如是按鈕2的0.05倍,因此在調整窗口大小時也保持這個比例? – tajmahal 2011-05-20 14:14:15

+0

是的,你可以。您可以設置相對於其他組件位置的位置。自從我觸及Swing代碼之後已經有一段時間了,但它絕對可能並且非常簡單。看一看MigLayout的欺騙手套,瞭解它的確切細節 – meverett 2011-05-20 14:29:31

1

GBC沒有錨,只是用普通的香草GridBagConstraints和首選大小。

中心的JButton固定大小:

import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 

import javax.swing.JButton; 
import javax.swing.JFrame; 

public class MainWithFixSize { 
    public static void main(String[] argv) throws Exception { 
     JFrame frame = new JFrame(); 
     Container container = frame.getContentPane(); 
     GridBagLayout gbl = new GridBagLayout(); 
     container.setLayout(gbl); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = 1; 
     gbc.gridy = 1; 
     JButton component = new JButton(); 
     component.setPreferredSize(new Dimension(25, 25)); 
     gbl.setConstraints(component, gbc); 
     container.add(component); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setPreferredSize(new Dimension(40, 90)); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    private MainWithFixSize() { 
    } 
} 
相關問題