2014-12-07 37 views
7

下面是我有一個問題的我的Java應用程序GUI部分。如何刪除仍在使用流佈局的JPanel之間的填充?

enter image description here

什麼該GUI包括爲藍色的JPanel(容器)使用默認的FlowLayout作爲LayoutManager的包含含有兩個JPanels一個盒(以除去水平間隔或我可以使用setHgaps零爲此事而不是Box),每個包含一個JLabel。

這是我創建GUI部分的代碼。

private void setupSouth() { 

    final JPanel southPanel = new JPanel(); 
    southPanel.setBackground(Color.BLUE); 

    final JPanel innerPanel1 = new JPanel(); 
    innerPanel1.setBackground(Color.ORANGE); 
    innerPanel1.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT)); 
    innerPanel1.add(new JLabel("Good")); 

    final JPanel innerPanel2 = new JPanel(); 
    innerPanel2.setBackground(Color.RED); 
    innerPanel2.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT)); 
    innerPanel2.add(new JLabel("Luck!")); 

    final Box southBox = new Box(BoxLayout.LINE_AXIS); 
    southBox.add(innerPanel1); 
    southBox.add(innerPanel2); 

    myFrame.add(southPanel, BorderLayout.SOUTH); 
} 

我的問題是我將如何擺脫外面的JPanel(藍色的)和盒之間的垂直填充?

我知道這是填充,因爲我在Difference between margin and padding?上讀到「padding =從文本到邊框的元素周圍(內部)」。

這是行不通的,因爲這有因與間隙(空間)之間components.- How to remove JPanel padding in MigLayout?

我試過,但它也不能工作。 JPanel Padding in Java

+0

你的代碼是不完整的。你永遠不會將southBox添加到你的面板,也不會將你的innerPanel添加到你的南面板 – 2014-12-08 01:02:48

回答

11

可以只set the gapsFlowLayout,即

FlowLayout layout = (FlowLayout)southPanel.getLayout(); 
layout.setVgap(0); 

默認FlowLayout有一個5單元的水平和垂直間隔。水平在這種情況下無關緊要,因爲BorderLayout正在水平拉伸面板。

或者簡單地用新的FlowLayout初始化面板。這將是同樣的結果。

new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0)); 

編輯:

「我試過了,沒有工作。」

對我的作品......

enter image description here       enter image description here

       設置差距↑                        不設置間隙↑

import java.awt.*; 
import javax.swing.*; 

public class Test { 

    public void init() { 
     final JPanel southPanel = new JPanel(); 
     FlowLayout layout = (FlowLayout)southPanel.getLayout(); 
     layout.setVgap(0); 
     southPanel.setBackground(Color.BLUE); 

     final JPanel innerPanel1 = new JPanel(); 
     innerPanel1.setBackground(Color.ORANGE); 
     innerPanel1.add(new JLabel("Good")); 

     final JPanel innerPanel2 = new JPanel(); 
     innerPanel2.setBackground(Color.RED); 
     innerPanel2.add(new JLabel("Luck!")); 

     final Box southBox = new Box(BoxLayout.LINE_AXIS); 
     southBox.add(innerPanel1); 
     southBox.add(innerPanel2); 

     southPanel.add(southBox); // <=== You're also missing this 

     JFrame myFrame = new JFrame(); 
     JPanel center = new JPanel(); 
     center.setBackground(Color.yellow); 
     myFrame.add(center); 
     myFrame.add(southPanel, BorderLayout.SOUTH); 
     myFrame.setSize(150, 100); 
     myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     myFrame.setLocationByPlatform(true); 
     myFrame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable(){ 
      public void run() { 
       new Test().init(); 
      } 
     }); 
    } 
} 

注:始終發佈可運行的例子(如我做了)更好幫幫我。你說它不起作用,但它總是對我有用,所以如果沒有一些可以運行並演示問題的代碼,我們如何知道自己做錯了什麼?

+0

我試過了,沒有工作,因爲組件之間存在差距 – committedandroider 2014-12-08 00:44:11

+0

請發佈一個演示此問題的可運行示例 – 2014-12-08 00:53:30