2012-12-13 82 views
0

我一直在玩與周圍的卡片佈局演示:搖擺流佈局性

http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/CardLayoutDemoProject/src/layout/CardLayoutDemo.java

我想建立這樣一個GUI,但我與流佈局掙扎,我發現其他屬性根據需要幫助空間的東西,但我不能讓他們與這個例子一起工作。有人可以告訴我如何將下面的屬性應用到演示流程佈局中:

public FlowLayout(int alignment, int horizontalGap, int verticalGap) 

任何幫助都會很棒。

+0

它是什麼,你想達到什麼目的? – MadProgrammer

+0

只需控制元素之間的間距及其對齊即可。我希望能夠在所有東西之間建立一個統一的差距,因爲它們都堆疊在一起。 – mao

+0

@mao參數名稱說明你想要什麼。如果你想對齊相同寬度的元素,使用GridLayout。 – ntalbs

回答

3

有時候,你只需要進行試驗......

enter image description here

public class TestFlow { 

    public static void main(String[] args) { 
     new TestFlow(); 
    } 

    public TestFlow() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new GridLayout(0, 1)); 
      add(createPane(FlowLayout.LEFT, 5, 5)); 
      add(createPane(FlowLayout.CENTER, 15, 15)); 
      add(createPane(FlowLayout.RIGHT, 20, 20)); 
      add(createPane(FlowLayout.LEADING, 0, 0)); 
      add(createPane(FlowLayout.TRAILING, 5, 5)); 
     } 

     protected JPanel createPane(int alignment, int hGap, int vGap) { 

      JPanel panel = new JPanel(new FlowLayout(alignment, hGap, vGap)); 
      panel.setBorder(new LineBorder(Color.GRAY)); 
      panel.add(new JLabel("Left")); 
      panel.add(new JLabel("Middle")); 
      panel.add(new JLabel("Right")); 

      return panel; 

     } 

    } 

}