我已经完美地设置了菜单(中心框),但我不知道如何放置标签。目前发生的情况是标签位于菜单选项下方,并且菜单选项被推到右侧。
Here is what I want to happen:
And here is what is happening:
目前我的盒子集中在:
this.setAlignmentX(Component.CENTER_ALIGNMENT);
我尝试使用以下方法对我的标签执行相同的操作:
this.setAlignmentX(Component.BOTTOM_ALIGNMENT);
this.setAlignmentY(Component.LEFT_ALIGNMENT);
这什么也没做。
抱歉,这张图太糟糕了,我用 MS Paint 花了大约 20 秒就把它画出来了。
这是标签的重要部分
public Label(String text)
{
this.setHorizontalTextPosition(JLabel.CENTER);
this.setVerticalTextPosition(JLabel.CENTER);
this.setHorizontalAlignment(0);
}
这是我创建盒子布局的地方:
pnlMain.setLayout(new BoxLayout(pnlMain, BoxLayout.Y_AXIS));
Edit:这是我的 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");
}
}