无法定位按钮或 JLabel

Can't position Buttons or JLabels

我是 Java 中使用 GUI 的新手,我在移动文本和按钮时遇到问题。无论我为按钮或任何其他 JLabel 提供什么坐标,它都不会移动,我想知道如何修复它,以便我可以将组件放置在 JPanel 上我想要的任何位置

public class IntroPage  extends JFrame {

public static void main(String[] args) {

        IntroPage main = new IntroPage();
        main.setVisible(true);
    }

private JPanel contentPane;

    public IntroPage (){

        //make sure the program exits when the frame closes
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Welcome");
        contentPane = new JPanel();
        setSize(400,700);

        //This will center the JFrame in the middle of the screen
        setLocationRelativeTo(null);

        //Welcome Page stuff :D 
        JLabel ApplauseLabel = new JLabel("Welcome to U.X.Dot.X");
        ApplauseLabel.setFont(new Font("Gill Sans MT", Font.PLAIN, 30));
        ApplauseLabel.setLocation(100, 50);
        contentPane.add(ApplauseLabel);

        JLabel slogan = new JLabel("Register below");
        slogan.setFont(new Font("Gill Sans MT", Font.PLAIN, 15));
        slogan.setLocation(100, 400);  
        contentPane.add(slogan);      

        //FacebookSignUp.
        JButton FBbutton = new JButton("Login With FaceBook");
        FBbutton.setBackground(Color.BLUE);
        FBbutton.setSize(50,50);
        FBbutton.setLocation(20, 40);
        FBbutton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //Add JPanel to go to FB API. Much later
            }
        });
        contentPane.add(FBbutton);

        add(contentPane);

        //make sure the JFrame is visible
        setVisible(true);
    }
}

您忽略了 contentPane JPanel 的布局管理器。了解它默认使用 FlowLayout,并且会忽略您的 setLocation 和 setBounds 语句。要让 JPanel 接受绝对定位,您必须通过 contentPane.setLayout(null) 给它一个空布局。

话虽如此,我不建议你这样做!虽然空布局、setLocation(...)setBounds(...) 似乎对 Swing 新手来说是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,您将 运行 遇到的困难就越多使用它们。它们不会在 GUI 调整大小时调整组件的大小,它们是皇家 来增强或维护的,当放置在滚动窗格中时它们会完全失效,当在所有平台或不同的屏幕分辨率上查看时它们看起来很糟糕来自原来的

例如下面的 GUI

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;

import javax.swing.*;

public class IntroPage2 extends JPanel {
    public static final String TITLE = "Welcome to U.X.Dot.X";
    private JLabel welcomeLabel = new JLabel(TITLE, SwingConstants.CENTER);
    private JButton fbButton = new JButton("Login With Facebook");

    public IntroPage2() {
        fbButton.setBackground(Color.BLUE);
        fbButton.setForeground(Color.CYAN);
        welcomeLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 30));
        int wlGap = 20;
        welcomeLabel.setBorder(BorderFactory.createEmptyBorder(wlGap, wlGap, wlGap, wlGap));

        JLabel registerBelowLabel = new JLabel("Register Below");
        registerBelowLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 15));
        JPanel centralPanel = new JPanel(new GridBagLayout());
        centralPanel.setPreferredSize(new Dimension(300, 600));
        centralPanel.add(registerBelowLabel);

        JPanel topPanel = new JPanel(new BorderLayout());
        topPanel.add(fbButton, BorderLayout.LINE_START);
        topPanel.add(welcomeLabel, BorderLayout.CENTER);

        setLayout(new BorderLayout());
        int ebGap = 8;
        setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
        add(topPanel, BorderLayout.PAGE_START);
        add(centralPanel, BorderLayout.CENTER);
    }

    private static void createAndShowGui() {
        IntroPage2 mainPanel = new IntroPage2();

        JFrame frame = new JFrame("Welcome");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

会创建如下内容: