无法定位按钮或JLabel

时间:2017-04-09 15:23:27

标签: java swing layout-manager

我刚接触使用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);
    }
}

1 个答案:

答案 0 :(得分:3)

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

话虽如此,我不建议你这样做!虽然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());
    }
}

会创建类似的东西:

https://github.com/driftyco/ionic-native