java - MigLayout,将所有组件对齐 JFrame 的中心

标签 java swing miglayout

如何将 JPanel 中的组件对齐到 JFrame 的中心。

这是我的代码和我得到的

a busy cat

我想要这样

a busy cat
(来源:hizliresim.com)

主机

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainFrame extends JFrame {


private static MainFrame instance = new MainFrame();

public static MainFrame getInstance() {
    return instance;
}

public static void switchToPanel(JPanel p) {
    getInstance().setContentPane(p);
    getInstance().validate();
}

private MainFrame() {
    setTitle("FavMovies");
    setSize(400, 400);
    setLocationRelativeTo(null);
    setVisible(true);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args) {
    MainFrame.switchToPanel(new LoginPanel());
}
}

登录面板

import javax.swing.*;
import net.miginfocom.swing.MigLayout;



public class LoginPanel extends JPanel {

// PROPERTIES

private JTextField inputUsername;
private JPasswordField inputPassword;

// CONSTRUCTOR

public LoginPanel() {

    setSize(400,400);

    // COMPONENTS

    inputUsername = new JTextField(10);
    inputPassword = new JPasswordField(10);
    JButton loginButton = new JButton("Login");
    JButton createButton = new JButton( "Create User");

    setLayout(new MigLayout());

    // first row
    add(new JLabel( "Username: "));
    add(inputUsername, "wrap 5");

    // second row
    add(new JLabel( "Password: "));
    add(inputPassword, "wrap 10");

    // final row        
    add(loginButton);
    add(createButton); 


}

}

我希望我能解释:(我希望我能解释:(我希望我能解释:(我希望我能解释:(

最佳答案

我认为没有理由使用GridBagLayout。这是一件微不足道的事情 在MigLayout中。

package com.zetcode;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;

/*
Centering components in MigLayout manager.
Author: Jan Bodnar
Website: zetcode.com
 */
public class MigLayoutMoviesEx extends JFrame {

    public MigLayoutMoviesEx() {

        initUI();
    }

    private void initUI() {

        JLabel lbl1 = new JLabel("User name");
        JLabel lbl2 = new JLabel("Password:");

        JTextField field1 = new JTextField(10);
        JTextField field2 = new JTextField(10);

        JButton btn1 = new JButton("Login");
        JButton btn2 = new JButton("Create user");

        createLayout(lbl1, field1, lbl2, field2, btn1, btn2);

        setTitle("MigLayout example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void createLayout(JComponent... arg) {

        setLayout(new MigLayout("align 50% 50%"));

        add(arg[0]);
        add(arg[1], "wrap");
        add(arg[2]);
        add(arg[3], "wrap");
        add(arg[4]);
        add(arg[5]);

        pack();
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            MigLayoutMoviesEx ex = new MigLayoutMoviesEx();
            ex.setVisible(true);
        });
    }
}

为了使组件居中,我们使用align 50% 50%约束。

这是屏幕截图:

Screenshot of the code example

关于java - MigLayout,将所有组件对齐 JFrame 的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30820502/

相关文章:

javafx - 在 JavaFX 中使用 MigLayout 时如何使舞台以正确的尺寸显示

java - 将自定义注释 Hook 到 Maven 中的 JUnit 测试器

java - Byte[] 和 java.lang.OutOfMemoryError 逐位读取文件

java - 如何在 Java 中将数据库 ID 添加为组合框索引?

java - 协助关键事件

java - 在 Java Swing 中使用 MigLayout

java - 编译时"Unchecked or unsafe operations"错误

Java 查找所有以字母开头的单词

Java Swing JFrame 布局

java - MigLayout "wrap"行为不一致