我已經完美地設置了菜單(中心框),但我不知道如何定位標籤。目前發生的情況是標籤正在菜單選項下方,而菜單選項則被推向右側。如何使用BoxLayout來做到這一點?
以下是我想發生:
這裏是正在發生的事情:
目前,我有中心我的箱子:
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");
}
}
這可能你需要看看['JLabel.setHorizontalAlignment(int)'](http://docs.oracle.com/javase/7/文檔/ API /的javax /擺動/ JLabel.html#setHorizontalAlignment%28int%29)。否則,請發佈[SSCCE](http://sscce.org/)。 –
關於如何在教程中使用BoxLayout(擺動標籤wiki中引用的聖經),包括如何解決對齊問題 – kleopatra
等等,您在談論哪個標籤?我看到一個MenuOption,但除了「版本」之外沒有另一個標籤,但是沒有提及相關的問題。 –