简体   繁体   中英

how to align buttons in the middle java

I am a beginner in Java, but somehow have a bit of knowledge but still beyond. I wanted to ask , how do align buttons in this main menu I just created. The buttons are somehow aligned horizontally.

This is my code :

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;



class mainmenu extends JFrame 
{


    JButton b1;
    JLabel l1;
    JButton b2;

    public mainmenu() {


        setResizable(false);    
        setLocation(100, 100);
        setSize(500, 500);
        setVisible(true);

        setLayout(new BorderLayout());

        JLabel background=new JLabel(new ImageIcon("C:\\Users\\Master Boat\\Desktop\\PH\\BG HOROR.png"));

        add(background);

        background.setLayout(new FlowLayout());

        l1=new JLabel("Here is a button");
        b1=new JButton("I am a button");
        b2=new JButton(" EXIT! ");
        b1.addActionListener(new btnFunc());


        background.add(l1);
        background.add(b1);
        background.add(b2);
    }

    public void armaged() {
        add(new gamesamplingbeta());

    }

    public static void main(String args[]) 
    {
        new mainmenu();
    }
    public class btnFunc implements ActionListener {

        public void actionPerformed (ActionEvent e) {


        }   


    }

    public class btnFunc2 implements ActionListener {

        public void actionPerformed2 (ActionEvent e) {

            System.exit(1);
        }
    }
}

You should take a look at Swing Layouts for a whole bunch of different layout managers that allow you to position your components in a bunch of different ways.

The one I believe you should be using for this question is the Box Layout if you would like your buttons centered vertically.

Here is an example of one.

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

public class MainFrame
{
    JFrame mainFrame = new JFrame("Main Frame");
    JPanel mainPanel = new JPanel();
    JLabel label1 = new JLabel("Vertical Buttons");
    JButton button1 = new JButton("Button 1");
    JButton button2 = new JButton("Button 2");

    public MainFrame()
    {
        label1.setAlignmentX(Component.CENTER_ALIGNMENT);
        button1.setAlignmentX(Component.CENTER_ALIGNMENT);
        button2.setAlignmentX(Component.CENTER_ALIGNMENT);

        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.add(label1);
        mainPanel.add(button1);
        mainPanel.add(button2);
        mainFrame.add(mainPanel);
        mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        mainFrame.pack();
        mainFrame.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(MainFrame::new);
    }
}

If you would like to add spacing in between any of the components, use a Rigid Area like so

container.add(component);
container.add(Box.createRigidArea(new Dimension(100, 100));
container.add(component1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM