Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 访问并更改放置在JDialog上的组件的大小_Java_Swing_User Interface_Layout Manager_Jdialog - Fatal编程技术网

Java 访问并更改放置在JDialog上的组件的大小

Java 访问并更改放置在JDialog上的组件的大小,java,swing,user-interface,layout-manager,jdialog,Java,Swing,User Interface,Layout Manager,Jdialog,我创建了一个modalJDialog(正确地说,是JDialog的子项),并在用户单击JFrame上的jb按钮时将其设置为可见。为了确保JDialog上的内容垂直居中,我重写了setVisible()方法,并在调用super.setVisible(true)之前执行了一些操作。这里的问题是,如果对话框设置为模态,则在调用setVisible(true)之前,放置在对话框上的任何组件的大小都不是0。setVisible()也会阻止执行 有没有关于如何绕过/解决此问题的建议/提示 示例代码: pub

我创建了一个modalJDialog(正确地说,是JDialog的子项),并在用户单击JFrame上的jb按钮时将其设置为可见。为了确保JDialog上的内容垂直居中,我重写了setVisible()方法,并在调用
super.setVisible(true)
之前执行了一些操作。这里的问题是,如果对话框设置为模态,则在调用setVisible(true)之前,放置在对话框上的任何组件的大小都不是0。setVisible()也会阻止执行

有没有关于如何绕过/解决此问题的建议/提示

示例代码:

public class SampleDialog extends JDialog {

    protected JPanel contentPane;

    public SampleDialog() {
        super();

        setLayout(new BorderLayout());
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        setModal(true);

        JPanel headerPane = new JPanel();
        headerPane.setBackground(Color.GREEN);
        add(headerPane, BorderLayout.NORTH);

        contentPane = new JPanel();
        contentPane.setBackground(Color.WHITE);
        add(contentPane, BorderLayout.CENTER);

        JPanel footerPane = new JPanel();
        footerPane.setBackground(Color.YELLOW);
        add(footerPane, BorderLayout.SOUTH);

        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        contentPane.add(new JLabel("Code"));
        contentPane.add(new JTextField());
        contentPane.add(new JLabel("Password"));
        contentPane.add(new JPasswordField());
        contentPane.add(new JButton("Login"));
    }

    @Override
    public void setVisible(boolean b) {
        /*
         * Get total height of all components added to 'contentPane'
         * Place Box.createVerticalStrut() before and after the 'contentPane' components,
         * so the input fields look like they are centered vertically
         * !!! Cannot determine size of any component because it is not rendered
         */
        super.setVisible(b);
    }
}

为了确保JDialog上的内容垂直居中,我已经覆盖了setVisible()

您不应该为此重写setVisible()

要使零部件居中,请使用适当的布局管理器

例如,要使组件垂直和水平居中,只需使用GridBagLayout即可:

JDialog dialog = new JDialog();
dialog.setLayout( new GrigBagLayout() );

JPanel panel = new JPanel(...);
panel.add(...);
dialog.add(panel, new GridBagConstraints());

您可以使用
migloayout
manager轻松创建您想要的布局:

package com.zetcode;

import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;

/*
 * Centering components on screen.
 * Author Jan Bodnar
 * Website zetcode.com
 */
class MyDialog extends JDialog {

    public MyDialog() {

        initDialog();
    }

    private void initDialog() {

        JLabel lbl1 = new JLabel("Code");
        JTextField field1 = new JTextField(15);

        JLabel lbl2 = new JLabel("Password");
        JPasswordField field2 = new JPasswordField(15);        

        JButton btn = new JButton("Login");

        createLayout(lbl1, field1, lbl2, field2, btn);

        setTitle("Login");

        setLocationRelativeTo(null);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        setModal(true);
    }

    private void createLayout(JComponent... arg) {

        setLayout(new MigLayout("wrap, align 50% 50%", "[center]"));

        add(arg[0]);
        add(arg[1]);
        add(arg[2]);
        add(arg[3]);
        add(arg[4]);

        pack();
    }
}

public class MigLayoutCenterEx extends JFrame {

    public MigLayoutCenterEx() {

        initUI();
    }

    private void initUI() {

        JPanel pnl = new JPanel();

        JButton btn = new JButton("Show dialog");
        btn.addActionListener((ActionEvent e) -> {

            JDialog dialog = new MyDialog();
            dialog.setVisible(true);
        });

        pnl.add(btn);
        add(pnl);

        setSize(400, 300);
        setTitle("MigLayout example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            MigLayoutCenterEx ex = new MigLayoutCenterEx();
            ex.setVisible(true);
        });
    }
}
截图:


请显示相关代码,最好是一个有效的,也许是你想要的和得到的图像。请注意,代码应该以代码格式文本的形式发布在问题中,而不是链接中。祝你好运另外,您的问题表明,您可能试图绕过Swing对组件的自然大小和定位,通常使用系统比使用系统更好,但如果没有您的帮助,我无法确定。此外,是的,在GUI呈现之前,GUI中的任何内容都不会改变大小
pack()
setVisible(true)
将为您呈现内容。而且,是的,模式对话框会在显示调用代码后阻止调用代码。