简体   繁体   English

如何在中间的Java中对齐按钮

[英]how to align buttons in the middle java

I am a beginner in Java, but somehow have a bit of knowledge but still beyond. 我是Java的初学者,但是以某种方式拥有一定的知识,但还不止于此。 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. 您应该查看Swing Layouts,了解一堆不同的布局管理器,这些管理器允许您以多种不同的方式放置组件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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