2014-01-05 57 views
2

我已經完美地設置了菜單(中心框),但我不知道如何定位標籤。目前發生的情況是標籤正在菜單選項下方,而菜單選項則被推向右側。如何使用BoxLayout來做到這一點?

以下是我想發生: enter image description here

這裏是正在發生的事情: enter image description here

目前,我有中心我的箱子:

this.setAlignmentX(Component.CENTER_ALIGNMENT); 

而且我已經嘗試使用我的標籤做同樣的事情:

this.setAlignmentX(Component.BOTTOM_ALIGNMENT); 
this.setAlignmentY(Component.LEFT_ALIGNMENT); 

不做任何事情。

對不起,圖是如此糟糕,我把它繪製在MS塗料大約20秒。

這裏是標籤

public Label(String text) 
{ 

    this.setHorizontalTextPosition(JLabel.CENTER); 
    this.setVerticalTextPosition(JLabel.CENTER); 
    this.setHorizontalAlignment(0); 
} 

的重要組成部分,這裏是我創建的BoxLayout:

pnlMain.setLayout(new BoxLayout(pnlMain, BoxLayout.Y_AXIS)); 

編輯:這裏是我的JFrame擴展類裏面的主要功能。上面的功能只是創建面板,按鈕和標籤。

public Window() 
{ 
    //Create the JFrame 
    super("Tank Trouble"); 
    this.pack(); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setVisible(true); 

    //Changes the frame size to your screen size 
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); 
    int x = (int) (dimension.getWidth()); 
    int y = (int) (dimension.getHeight()); 
    setSize(x,y); 
    setResizable(false); 
    //GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this); //Makes the application go fullscreen 

    getContentPane().add(pnlMaster); 

    pnlMaster.add(pnlMenu, "Main Menu"); 
    pnlMaster.add(pnlOptions, "Options"); 
    pnlMaster.add(pnlGame, "Game"); 
    pnlMaster.add(pnlMenu2); 

    switchTo("Main Menu"); 

    pnlOptions.setLayout(new BoxLayout(pnlOptions, BoxLayout.Y_AXIS)); 

    Box box = Box.createVerticalBox(); 
    box.add(Window.playS); 
    box.add(Box.createVerticalStrut(20)); 
    box.add(Window.playM); 
    box.add(Box.createVerticalStrut(20)); 
    box.add(Window.options); 
    box.add(Box.createVerticalStrut(20)); 
    box.add(Window.language); 
    box.add(Box.createVerticalStrut(20)); 
    box.add(Window.exit); 
    box.add(Box.createVerticalStrut(20)); 

    pnlMenu.add(box); 
    pnlMenu.add(new JPanel()); 
    pnlMenu.add(new JPanel()); 
    pnlMenu.add(new JPanel()); 
    pnlMenu.add(new JPanel()); 

    pnlMenu2.add(Window.lblVersion); 

    System.out.println("Window class loaded"); 

} 

而且這裏是我的菜單類目前的樣子(這是以前處理一切與他們的除外創建按鈕和標籤)。

package menu; 

import main.Window; 

public class Menu 
{ 
    public Menu() 
    { 
    Listener listener = new Listener(); 

    //Add ActionListeners 
    Window.exit.addActionListener(listener); 
    Window.playS.addActionListener(listener); 
    Window.playM.addActionListener(listener); 
    Window.options.addActionListener(listener); 
    Window.language.addActionListener(listener); 
    Window.btnBack.addActionListener(listener); 

    System.out.println("Menu class loaded"); 
} 
} 
+0

這可能你需要看看['JLabel.setHorizo​​ntalAlignment(int)'](http://docs.oracle.com/javase/7/文檔/ API /的javax /擺動/ JLabel.html#setHorizo​​ntalAlignment%28int%29)。否則,請發佈[SSCCE](http://sscce.org/)。 –

+1

關於如何在教程中使用BoxLayout(擺動標籤wiki中引用的聖經),包括如何解決對齊問題 – kleopatra

+0

等等,您在談論哪個標籤?我看到一個MenuOption,但除了「版本」之外沒有另一個標籤,但是沒有提及相關的問題。 –

回答

4

您可以使用LayoutManagers的正確組合獲得所需的結果。不要只限於一個。充分利用組合/嵌套佈局帶來的優勢。

enter image description here

下面是我用

  • 一個BoxLayout爲中心按鈕
  • 一個GridLayout(1, 5)的面板包括一切,但BUTTOM左按鈕,這是在自己
  • 的技術
  • FlowLayoutLEADING對齊
  • 所有包裹在JFrame默認BorderLayout

請參見下面的程序

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

public class BoxLayoutDemo { 

    JButton button1 = new JButton("Button1"); 
    public BoxLayoutDemo() { 
     JPanel pane = new JPanel(new GridLayout(1, 5)); 

     Box box = Box.createVerticalBox(); 
     box.add(new JButton("Button 1")); 
     box.add(Box.createVerticalStrut(20)); 
     box.add(new JButton("Button 2")); 
     box.add(Box.createVerticalStrut(20)); 
     box.add(new JButton("Button 3")); 
     box.add(Box.createVerticalStrut(20)); 
     box.add(new JButton("Button 4")); 
     box.add(Box.createVerticalStrut(20)); 
     box.add(new JButton("Button 5")); 
     box.add(Box.createVerticalStrut(20)); 

     pane.add(new JPanel()); 
     pane.add(new JPanel()); 
     pane.add(box); 
     pane.add(new JPanel()); 
     pane.add(new JPanel()); 

     JPanel pane2 = new JPanel(new FlowLayout(FlowLayout.LEADING)); 
     pane2.add(new JButton("ButtonButton")); 

     JFrame frame = new JFrame("GridBag Box"); 
     frame.add(pane, BorderLayout.CENTER); 
     frame.add(pane2, BorderLayout.SOUTH); 
     frame.pack(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 


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

免責聲明:原諒框架的標題。我首先想到的結合GridBagLayoutBox,但我做到這一點的方式更容易。現在我已經注意到它太懶了改變標題。


對於那些誰說我做了什麼上面是有點的hackish(加入空的面板),這也許是,你也可以在頂部面板和底部面板採用了BorderLayout,並加入到含有面板一個首選的大小,它會給你類似的結果

public BoxLayoutDemo() { 
     JPanel pane = new JPanel(); 

     Box box = Box.createVerticalBox(); 
     box.add(new JButton("Button 1")); 
     box.add(Box.createVerticalStrut(20)); 
     box.add(new JButton("Button 2")); 
     box.add(Box.createVerticalStrut(20)); 
     box.add(new JButton("Button 3")); 
     box.add(Box.createVerticalStrut(20)); 
     box.add(new JButton("Button 4")); 
     box.add(Box.createVerticalStrut(20)); 
     box.add(new JButton("Button 5")); 
     box.add(Box.createVerticalStrut(20)); 

     pane.add(box); 

     JPanel pane2 = new JPanel(new FlowLayout(FlowLayout.LEADING)); 
     pane2.add(new JButton("ButtonButton")); 

     JPanel panel = new JPanel(new BorderLayout()){ 
      public Dimension getPreferredSize() { 
       return new Dimension(400, 260); 
      } 
     }; 
     panel.add(pane, BorderLayout.CENTER); 
     panel.add(pane2, BorderLayout.SOUTH); 

     JFrame frame = new JFrame("Slitting using different layouts"); 
     frame.add(panel); 
     frame.pack(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
+0

由於我的JFrame是JFrame的一個擴展類(這是什麼「子類」是?),並且由於我使用的是一種「佈局佈局」,因此我在修改代碼時遇到了一些問題太。當我將面板添加到主面板時,我已經使用了'pnlMaster.add(pnlMenu,「主菜單」)',並且add方法只能像這樣或者如何使用BorderLayout參數進行操作。對不起,如果這沒有任何意義,但我有點強調,事實上一切都已經打破了(沒有任何東西正在出現)。我會盡快更新我的代碼。 –

+0

您需要了解BorderLayout的工作原理。您需要爲您添加的_every_組件分配不同的位置(即BorderLayout.NORTH)。在具有該佈局的單個容器中不能使用多次。 –

+0

如果不是太長,也許你想發佈你的代碼。 –