2012-11-18 294 views
2

嘿,我正在嘗試將面板內的所有面板對齊到較大面板的左側。如何將jPanel中的所有jPanel對齊到左邊?

這裏是什麼,我現在面臨着一個畫面:(!包含所有其他面板中的一個 - 我要叫它主)

Box panel layout

對於主面板 我使用的是創造它時,下面的代碼:

JPanel panel = new JPanel(); 
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 

對於每一個在它的面板,我也使用BoxLayout的,但我已經嘗試過類似的事情[的JPanel] .setAlig每個面板上的nmentX(Component.LEFT_ALIGNMENT),但似乎不起作用。

任何幫助,將不勝感激!

:)

編輯:對於「框的尺寸(米):」標籤,有沒有對齊,要它包含在面板頂部的方法嗎?它在它自己的面板中。

編輯:一旦固定:下面

JPanel problem once fixed

解決方案,謝謝大家的幫助:)

+0

順便說一下,我知道它看起來愚蠢的時刻,每個小組有一個邊界。我只把它們放在那裏,所以我實際上可以看到面板的位置:) –

+0

使用'GridLayout'或'GridBagLayout'。看看[佈局管理器的視覺指南](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)和[使用佈局管理器](http://docs.oracle.com)。 com/javase/tutorial/uiswing/layout/using.html)獲取更多信息 – MadProgrammer

+0

旁註:您是否在使用'JLabel'來設置結果? –

回答

5

你有很多的可能性,取決於你的所有需求。

所有我在這裏所做的是使用複合面板,並使用GridBagLayout調整佈局

enter image description here

public class BadLayout03 { 

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

    public BadLayout03() { 
     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 MasterPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class MasterPane extends JPanel { 

     public MasterPane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 1; 
      gbc.weightx = 1; 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.insets = new Insets(4, 4, 4, 4); 
      gbc.anchor = GridBagConstraints.WEST; 
      add(new DimensionsPane(), gbc); 
      gbc.gridy++; 
      add(new ColorPane(), gbc); 
      gbc.gridy++; 
      add(new ReinforementPane(), gbc); 
      gbc.gridy++; 
      add(new SealableTopPane(), gbc); 
      gbc.gridy++; 
      add(new CardGradePane(), gbc); 
      gbc.gridy++; 
      add(new QuantityPane(), gbc); 
      gbc.gridy++; 
      add(new OrderPricePane(), gbc); 
     } 

    } 

    public class DimensionsPane extends JPanel { 

     public DimensionsPane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 1; 
      gbc.weightx = 1; 
      gbc.anchor = GridBagConstraints.WEST; 
      add(new JLabel("Dimensions of box (m):"), gbc); 

      gbc.gridx = 1; 
      gbc.gridy = 0; 
      gbc.anchor = GridBagConstraints.EAST; 
      add(new JLabel("Length: 33.0"), gbc); 
      gbc.gridy++; 
      add(new JLabel("Width: 3.0"), gbc); 
      gbc.gridy++; 
      add(new JLabel("Height: 3.0"), gbc); 
     } 

    } 

    public class ColorPane extends JPanel { 

     public ColorPane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 
      gbc.weightx = 1; 
      gbc.anchor = GridBagConstraints.WEST; 
      add(new JLabel("Colour :"), gbc); 

      gbc.gridx = 1; 
      gbc.gridy = 0; 
      gbc.anchor = GridBagConstraints.EAST; 
      add(new JLabel("0"), gbc); 
     } 

    } 

    public class ReinforementPane extends JPanel { 

     public ReinforementPane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 
      gbc.weightx = 1; 
      gbc.anchor = GridBagConstraints.WEST; 
      add(new JLabel("Reinforcement :"), gbc); 

      gbc.gridx = 1; 
      gbc.gridy = 0; 
      gbc.anchor = GridBagConstraints.EAST; 
      add(new JLabel("None"), gbc); 
     } 

    } 

    public class SealableTopPane extends JPanel { 

     public SealableTopPane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 
      gbc.weightx = 1; 
      gbc.anchor = GridBagConstraints.WEST; 
      add(new JLabel("Selable top :"), gbc); 

      gbc.gridx = 1; 
      gbc.gridy = 0; 
      gbc.anchor = GridBagConstraints.EAST; 
      add(new JLabel("No"), gbc); 
     } 

    } 

    public class CardGradePane extends JPanel { 

     public CardGradePane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 
      gbc.weightx = 1; 
      gbc.anchor = GridBagConstraints.WEST; 
      add(new JLabel("Grade of card:"), gbc); 

      gbc.gridx = 1; 
      gbc.gridy = 0; 
      gbc.anchor = GridBagConstraints.EAST; 
      add(new JLabel("1"), gbc); 
     } 

    } 

    public class QuantityPane extends JPanel { 

     public QuantityPane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 
      gbc.weightx = 1; 
      gbc.anchor = GridBagConstraints.WEST; 
      add(new JLabel("Quantity:"), gbc); 

      gbc.gridx = 1; 
      gbc.gridy = 0; 
      gbc.anchor = GridBagConstraints.EAST; 
      add(new JLabel("1"), gbc); 
     } 

    } 

    public class OrderPricePane extends JPanel { 

     public OrderPricePane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 
      gbc.weightx = 1; 
      gbc.anchor = GridBagConstraints.WEST; 
      add(new JLabel("Order price:"), gbc); 

      gbc.gridx = 1; 
      gbc.gridy = 0; 
      gbc.anchor = GridBagConstraints.EAST; 
      add(new JLabel("$558.9"), gbc); 
     } 

    } 

} 
+0

這一個去收藏夾。 :) –

+0

啊太棒了!感謝所有的幫助@MadProgrammer,絕對使用這個:)非常感謝 –

3

您可以使用MigLayout

import java.awt.Color; 
import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import net.miginfocom.swing.MigLayout; 

public class PanelAlignment extends JFrame { 
    JPanel contentPane = new JPanel(); 
    JPanel firstPanel = new JPanel(); 
    JPanel secondPanel = new JPanel(); 
    JPanel thirdPanel = new JPanel(); 

    JLabel dimOfBox, rein, seaTop; 

    public PanelAlignment() { 
     contentPane.setLayout(new MigLayout()); 

     firstPanel.add(dimOfBox = new JLabel("Dimensions of box (m): ")); 
     firstPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
     contentPane.add(firstPanel,"wrap"); 

     secondPanel.add(rein = new JLabel("Reinforcement: ")); 
     secondPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
     contentPane.add(secondPanel,"wrap"); 

     thirdPanel.add(dimOfBox = new JLabel("Sealable top: ")); 
     thirdPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
     contentPane.add(thirdPanel,"wrap"); 
     add(contentPane); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       PanelAlignment pa = new PanelAlignment(); 
       pa.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       pa.pack(); 
       pa.setVisible(true); 
      } 
     }); 
    } 

} 

,你會得到類似這個:

enter image description here

我最近回答了類似的問題here

祝你好運! :)

+0

這是一個信息豐富的答案!感謝您抽出寶貴的時間爲我創建,不幸的是,在大學裏我們無法下載Java的額外功能;我不得不下載MigLayout:/ 雖然很好的答案,我會與我的講師聯繫,看看我是否可以使用它,再次感謝,如果我沒有收到不同的答案,我會標記此一個作爲答案:) 謝謝 –

+0

順便說一句,我使用JLabels設置結果,這是一個壞主意嗎? (第一次使用Java GUI!) –

+0

@OmarQassem是奧馬爾!使用'JLabel'來設置結果是一個壞主意!如果您的目標是保護您的值不受編輯,您可以使用'JTextField'並調用setEditable(false);方法。如果您不能使用MigLayout,請考慮接受MadProgrammers的回答。祝你好運! –