如何将 JLabel 和 JButton 放置在 java 中 gui 窗口的中心

问题描述 投票:0回答:1

我正在用java练习GUI。我目前正在努力解决元素的放置问题。我希望 JLabel 位于 gui 窗口的中间,按钮位于 JLabel 的正下方。我想在序言中说,我是 Java 初学者,所以我可能会有一些失误,所以我也希望得到一些帮助/提示!

我尝试了不同的布局管理器(BoxLayout、GridLayout、BorderLayout 和 FlowLayout),但它们都不起作用。到目前为止,FlowLayout 是最接近的,但每当我运行代码时,JLabel 都会放置在顶部,并且看起来被切断,而按钮位于 gui 窗口的最底部。

这是我的代码的预览:

import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {

    public MyFrame(){
        //panel for label
        JPanel labelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

        //label for the intro
        JLabel introLabel = new JLabel();
        introLabel.setText("Welcome User");
        introLabel.setForeground(new Color(129, 245, 71));
        introLabel.setFont(new Font("Digital-7", Font.PLAIN, 24));
        introLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        labelPanel.add(introLabel);

        // panel for button
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

        //JButton
        JButton button = new JButton("click");
        button.setFocusable(false);
        buttonPanel.add(button);

        //main content panel
        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(labelPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        //set content of the pane
        this.setContentPane(mainPanel);

        //frame constructor
        this.setSize(500, 500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("encrypt your message");
        this.getContentPane().setBackground(new Color(0x151414)); // the gui bg is determined by the pane
        this.setResizable(false);

        //layout manager
        this.setVisible(true);
    }
}
java swing layout-manager
1个回答
0
投票

我倾向于做这样的事情。如果您想要堆叠东西,

BoxLayout
是您的朋友。另外,您可以以“速记”方式使用
GridBagLayout
来将内容集中在容器中:

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

public class MyFrame extends JFrame {

    public MyFrame() {
        // panel for label
        JPanel basePanell = new JPanel();
        basePanell.setLayout(new BoxLayout(basePanell, BoxLayout.Y_AXIS));

        // label for the intro
        JLabel introLabel = new JLabel();
        introLabel.setText("Welcome User");
        introLabel.setForeground(new Color(129, 245, 71));
        introLabel.setFont(new Font("Digital-7", Font.PLAIN, 24));
        introLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        basePanell.add(introLabel);

        // JButton
        JButton button = new JButton("click");
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        basePanell.add(button);

        // Improve appearance
        basePanell.setBorder(new EmptyBorder(10, 10, 10, 10));

        // Stunt to centre the base panel
        getContentPane().setLayout(new GridBagLayout());
        getContentPane().add(basePanell);

        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("encrypt your message");
        // getContentPane().setBackground(new Color(0x151414)); // the gui bg is
        // determined by the pane
        getContentPane().setBackground(new Color(0xAAAAAA)); // the gui bg is determined by the pane
        setResizable(false);

    }

    public static void main(String[] args) {
        try {
            EventQueue.invokeAndWait(() -> {
                MyFrame f = new MyFrame();
                f.setSize(500, 500);
                f.setVisible(true);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.