【问题标题】:MigLayout, aligning all components at center of JFrameMigLayout,在 JFrame 的中心对齐所有组件
【发布时间】:2015-08-29 11:49:28
【问题描述】:

如何在 JFrame 中心的 JPanel 上对齐我的组件。

这是我的代码以及我得到了什么

我想要这样


(来源: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); 


}

}

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

【问题讨论】:

    标签: java swing miglayout


    【解决方案1】:

    GridBagLayout 将自动居中组件。因此,将框架的布局设置为使用GridBagLayout,然后将您的登录面板添加到框架中:

    //getInstance().setContentPane(p);
    getInstance().setLayout( new GridBagLayout() );
    getInstance().add(p);
    

    编辑:

    但是,当我调整它的大小时,我能够在中心看到 loginPanel。

    看起来您正在向可见的 GUI 添加一个组件。所以需要手动调用布局管理器,重新绘制组件。

    getInstance().validate();
    getInstance().revalidate();
    getInstance().repaint();
    

    您的代码结构非常复杂。您应该阅读 Swing 教程以获得更好的示例,并且您不会遇到这个问题。也许从How to Use Labels 中的LabelDemo 开始。

    如果您需要交换面板的能力,请查看 How to Use CardLayout 上的 Swing 教程中的部分,该部分专为此目的而设计。

    【讨论】:

    • 当我第一次运行应用程序时,jframe 上什么也没有出现。但是,当我调整它的大小时,我能够在中心看到 loginPanel。为什么会这样?我想在不调整大小的情况下看到它
    • 这些工作正常。我的设计有问题。我会努力的,ty ;)
    【解决方案2】:

    我认为没有理由使用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% 约束。

    截图如下:

    【讨论】:

      猜你喜欢
      • 2014-09-18
      • 1970-01-01
      • 2013-11-21
      • 2013-12-26
      • 1970-01-01
      • 2015-09-20
      • 2014-02-19
      • 2023-03-13
      • 2016-04-03
      相关资源
      最近更新 更多