如何使按钮居中并使标题居中?

How can I center buttons and top center a title?

我正在尝试将这两个 StartBack 按钮居中,但任何时候我都会尝试 BoxLayout.SetLocationSwingConstant.CENTER.setVerticalAlignment.setHorizonatalAlignment 不起作用。任何人都可以帮助我如何设置 middle-center 中的两个按钮和框架 top-center 中的 'Snake' 标题?谢谢。

package snake;

public class Start {
    public static void main(String[] args) {
        startScreen startFrame = new startScreen();
    }
}

  class startScreen extends JFrame {
      // constructor
      public startScreen() {
          // fonts
          Font snakeTitleFont = new Font("Arial", Font.BOLD, 50);
          Font buttonFont = new Font("Arial", Font.CENTER_BASELINE, 20);

          // text
          JLabel snakeTitle = new JLabel("Snake", SwingConstants.CENTER);
          snakeTitle.setFont(snakeTitleFont);
          add(snakeTitle);

          // start button
          JButton startButton = new JButton("Start");
          startButton.setBackground(Color.MAGENTA);
          startButton.setOpaque(true);
          startButton.setBorderPainted(false);
          startButton.setFont(buttonFont);

          this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.LINE_AXIS));

          add(startButton);

          // action listener for start btn
          startButton.addActionListener(new ActionListener() {
              // once this is clicked on, it should call the GUI
              @Override
              public void actionPerformed(ActionEvent e) {
                  new Frame();
                  dispose(); // closes the old form after start is clicked
              }
          });

          // back button
          JButton backButton = new JButton("Back");
          backButton.setLayout(null);
          backButton.setBackground(Color.YELLOW);
          backButton.setOpaque(true);
          backButton.setBorderPainted(false);
          backButton.setFont(buttonFont);
          backButton.setBounds(getBounds());
          this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.LINE_AXIS));

          add(backButton);

          // action listener for start btn
          startButton.addActionListener(new ActionListener() {
              // once this is clicked on, it should call the GUI
              @Override
              public void actionPerformed(ActionEvent e) {
                  new Frame();
                  dispose(); // closes the old form after start is clicked
              }
          });

          this.setVisible(true);
          this.setSize(800, 500);
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setResizable(false);
          this.setLocationRelativeTo(null);
      }
  }

快速浏览一下here. Best fits for your application would be either the BoxLayout or the FlowLayout

对于各自的使用领域,它们都是非常非常强大的工具。

除此之外,请尝试查看 Java 的布局教程。您应该始终在 Frames 上使用“getContentPane()”方法,并将布局和添加组件应用到 "ContentPane"!

您可以按如下方式使用BorderLayout

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class Start {
    public static void main(String[] args) {
        StartScreen startFrame = new StartScreen();
    }
}

class StartScreen extends JFrame {
    // Constructor
    public StartScreen() {
        // Fonts
        Font snakeTitleFont = new Font("Arial", Font.BOLD, 50);
        Font buttonFont = new Font("Arial", Font.CENTER_BASELINE, 20);

        // Text
        JLabel snakeTitle = new JLabel("Snake", SwingConstants.CENTER);
        snakeTitle.setFont(snakeTitleFont);
        add(snakeTitle, BorderLayout.NORTH);

        // Start button
        JButton startButton = new JButton("Start");
        startButton.setBackground(Color.MAGENTA);
        startButton.setOpaque(true);
        startButton.setBorderPainted(false);
        startButton.setFont(buttonFont);

        // ActionListener for Start button
        startButton.addActionListener(new ActionListener() {
            // once this is clicked on, it should call the GUI
            @Override
            public void actionPerformed(ActionEvent e) {
                new Frame();
                // closes the old form after start is clicked
                dispose();
            }
        });

        // Back button
        JButton backButton = new JButton("Back");
        backButton.setLayout(null);
        backButton.setBackground(Color.YELLOW);
        backButton.setOpaque(true);
        backButton.setBorderPainted(false);
        backButton.setFont(buttonFont);

        // Panel for Start and Back buttons
        JPanel panel = new JPanel();
        panel.add(startButton);
        panel.add(backButton);
        add(panel, BorderLayout.CENTER);

        this.setVisible(true);
        this.setSize(800, 500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
    }
}

请记住,您不会拘泥于单一布局。事实上,很难找到可以用单一布局解决的(复杂)布局解决方案。

相反,使用 multiple/compound 布局,例如...

这仅使用 BorderLayout 来管理标题和按钮面板,并使用 GridBagLayout 来管理按钮本身(默认情况下,它将按钮垂直和水平居中)

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());

            JLabel title = new JLabel("Snake");
            title.setHorizontalAlignment(JLabel.CENTER);
            add(title, BorderLayout.NORTH);

            JPanel buttonsPane = new JPanel(new GridBagLayout());
            buttonsPane.setBorder(new EmptyBorder(50, 50, 50, 50));

            buttonsPane.add(makeButton("Start"));
            buttonsPane.add(makeButton("Back"));

            add(buttonsPane);
        }

        protected JButton makeButton(String text) {
            JButton btn = new JButton(text);
            return btn;
        }


    }

}