我遇到对齐问题。下面我发布了代码和我当前Jframe的图片。
代码:
public void initUI() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFrame.setDefaultLookAndFeelDecorated(true);
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
SimpleDateFormat sdf = new SimpleDateFormat("h:mm:ss a");
String date = sdf.format(new Date());
frame = new JFrame("" + ClientSettings.SERVER_NAME + " | " +checkDay() + " - " + date);
frame.setLayout(new BorderLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel gamePanel = new JPanel();
gamePanel.setLayout(new BorderLayout());
gamePanel.add(this);
gamePanel.setPreferredSize(new Dimension(765, 503));
initMenubar();
frame.getContentPane().add(gamePanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true); // can see the client
frame.setResizable(false); // resizeable frame
init();
} catch (Exception e) {
e.printStackTrace();
}
}
public void initMenubar() {
JMenu fileMenu = new JMenu("Links");
String[] mainButtons = new String[] { "Forums", "-", "Exit" };
for (String name : mainButtons) {
JMenuItem menuItem = new JMenuItem(name);
if (name.equalsIgnoreCase("-")) {
fileMenu.addSeparator();
} else if(name.equalsIgnoreCase("Forums")) {
JMenu forumsMenu = new JMenu("Forums");
fileMenu.add(forumsMenu);
JMenuItem runeServer = new JMenuItem("Rune-Server");
runeServer.addActionListener(this);
forumsMenu.add(runeServer);
} else {
menuItem.addActionListener(this);
fileMenu.add(menuItem);
}
}
JMenuBar menuBar = new JMenuBar();
JMenuBar jmenubar = new JMenuBar();
JMenu settings = new JMenu("Settings");
settings.setActionCommand("Settings");
settings.addActionListener(this);
JButton screenshot = new JButton("Screenshot");
screenshot.setActionCommand("Screenshot");
screenshot.addActionListener(this);
frame.add(jmenubar);
menuBar.add(fileMenu);
menuBar.add(screenshot);
menuBar.add(settings);
frame.getContentPane().add(menuBar, BorderLayout.NORTH);
}
这是当前的Jframe:
我试图弄清楚如何将ScreenShot和设置按钮移动到右侧而不是它们当前所在的左侧。
答案 0 :(得分:2)
尝试这样的事情:
menuBar.add(fileMenu);
menuBar.add(Box.createHorizontalGlue());
menuBar.add(screenshot);
menuBar.add(settings);