简体   繁体   English

如何将JPanel高度设置为自动

[英]How to set JPanel height to auto

I create layout manually not using GUI editor, I need to set height to auto so the entire form visible to user. 我不使用GUI编辑器手动创建布局,我需要将高度设置为自动,以便用户可以看到整个表单。 I wrap GridLayout into FlowLayout so input is not stretch. 我将GridLayout包装到FlowLayout中,因此输入不会拉伸。 What's the best practices to create layout like this. 创建这样的布局的最佳实践是什么。 Could someone modify it so the entire form visible or using GridLayout but width and height of input in the form can be set ? 有人可以修改它,使整个表单可见或使用GridLayout但可以设置表单中输入的宽度和高度吗?

package helpdesk;

import java.awt.*;
import javax.swing.*;

/**
 *
 * @author Joko Wandiro
 */
public class Layout extends Base {

    JPanel panel_form_edit = new JPanel();
    FlowLayout layout_form_edit_outer = new FlowLayout(FlowLayout.LEFT);
    JPanel panel_form_edit_outer = new JPanel();
    // Form - Edit
    JLabel label_name_update = new JLabel("Name", JLabel.LEFT);
    JTextField input_id_update = new JTextField();
    JTextField input_name_update = new JTextField();
    JButton btn_update = new JButton("Update");

    public Layout() {
        setLookAndFeel();
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Main Layout
        GridLayout layout = new GridLayout(5, 1, 10, 10);
        setLayout(layout);
        // Form - Edit
        label_name_update.setPreferredSize(new Dimension(400, 20));
        input_name_update.setPreferredSize(new Dimension(400, 20));
        input_id_update.setVisible(false);
        GridLayout layout_form_edit = new GridLayout(4, 1, 10, 10);
        panel_form_edit.setLayout(layout_form_edit);
        panel_form_edit.add(input_id_update);
        panel_form_edit.add(label_name_update);
        panel_form_edit.add(input_name_update);
        panel_form_edit.add(btn_update);
        panel_form_edit_outer.setLayout(layout_form_edit_outer);
        panel_form_edit_outer.add(panel_form_edit);
        add(panel_form_edit_outer);
        panel_form_edit.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        panel_form_edit_outer.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Layout app = new Layout();
    }

    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
            );
        } catch (Exception exc) {
            // ignore error
        }
    }

}

Without set preferred size with pack method 不使用包装方法设置首选尺寸

不使用包装方法设置首选尺寸

Set preferred size with pack method 使用包装方法设置首选尺寸

使用包装方法设置首选尺寸

Set extended state to maximize but update button is not fully visible. 将扩展状态设置为最大化,但更新按钮不完全可见。 How to set height of JPanel so form is fully visible ? 如何设置JPanel的高度以使表单完全可见?

设置扩展状态以最大化

I want JFrame display in maximize screen. 我希望JFrame在最大化屏幕中显示。

Thanks a ton. 万分感谢。

I need to set height to auto so the entire form visible to user. 我需要将高度设置为自动,以便用户可以看到整个表单。

You use: 你用:

pack();
setVisible(true);

The pack() method will make the frame the preferred size of all the components added to the frame. pack()方法将使框架成为添加到框架的所有组件的首选大小。

label_name_update.setPreferredSize(new Dimension(400, 20));
input_name_update.setPreferredSize(new Dimension(400, 20));

Don't use setPreferredSize(...). 不要使用setPreferredSize(...)。 Each Swing component will determine its own preferred size based on the text, font etc. 每个Swing组件将根据文本,字体等确定自己的首选大小。

Edit: 编辑:

GridLayout layout_form_edit = new GridLayout(5, 1, 10, 10);

The above line appears to cause the problem. 上面的行似乎引起了问题。 You are specifying 5 rows but I guess there is not enough space on the frame for 5 rows. 您要指定5行,但我想框架上没有足够的空间容纳5行。

Try: 尝试:

GridLayout layout_form_edit = new GridLayout(0, 1, 10, 10);

I use JScrollPane to make JFrame scrollable so the entire form is visible. 我使用JScrollPane使JFrame可滚动,以便整个窗体可见。

package helpdesk;

import java.awt.*;
import javax.swing.*;

/**
 *
 * @author Joko Wandiro
 */
public class Layout extends Base {

    JPanel panel_form_edit = new JPanel();
    FlowLayout layout_form_edit_outer = new FlowLayout(FlowLayout.LEFT);
    JPanel panel_form_edit_outer = new JPanel();
    // Form - Edit
    JLabel label_name_update = new JLabel("Name", JLabel.LEFT);
    JTextField input_id_update = new JTextField();
    JTextField input_name_update = new JTextField();
    JButton btn_update = new JButton("Update");

    public Layout() {
        setLookAndFeel();
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Main Layout
        GridLayout layout = new GridLayout(5, 1, 10, 10);
        setLayout(layout);
        // Form - Edit
        label_name_update.setPreferredSize(new Dimension(400, 20));
        input_name_update.setPreferredSize(new Dimension(400, 20));
        input_id_update.setVisible(false);
        GridLayout layout_form_edit = new GridLayout(4, 1, 10, 10);
        panel_form_edit.setLayout(layout_form_edit);
        panel_form_edit.add(input_id_update);
        panel_form_edit.add(label_name_update);
        panel_form_edit.add(input_name_update);
        panel_form_edit.add(btn_update);
        panel_form_edit_outer.setLayout(layout_form_edit_outer);
        panel_form_edit_outer.add(panel_form_edit);
        add(panel_form_edit_outer);
        // Set window or jframe scrollable
        Container container = getContentPane();
        JScrollPane scroll = new JScrollPane(container);
        setContentPane(scroll);
        setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Layout app = new Layout();
    }

    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
            );
        } catch (Exception exc) {
            // ignore error
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM