Andrew Eggers
Andrew Eggers

Reputation: 31

Set position of buttons

I am having issues with alignment. Below I have posted the code and a picture of my current Jframe.

Code:

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);
}

And this is the current Jframe:

Current JFrame

I am trying to figure out how to move the ScreenShot and Settings buttons to the right side rather than the left where they are currently located.

Upvotes: 1

Views: 291

Answers (1)

Usagi Miyamoto
Usagi Miyamoto

Reputation: 6299

Try something like this:

menuBar.add(fileMenu);
menuBar.add(Box.createHorizontalGlue());
menuBar.add(screenshot);
menuBar.add(settings);

As per Java Tutorial: How to Use Menus / Customizing Menu Layout

Upvotes: 2

Related Questions