簡體   English   中英

如何將JPanel高度設置為自動

[英]How to set JPanel height to auto

我不使用GUI編輯器手動創建布局,我需要將高度設置為自動,以便用戶可以看到整個表單。 我將GridLayout包裝到FlowLayout中,因此輸入不會拉伸。 創建這樣的布局的最佳實踐是什么。 有人可以修改它,使整個表單可見或使用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
        }
    }

}

不使用包裝方法設置首選尺寸

不使用包裝方法設置首選尺寸

使用包裝方法設置首選尺寸

使用包裝方法設置首選尺寸

將擴展狀態設置為最大化,但更新按鈕不完全可見。 如何設置JPanel的高度以使表單完全可見?

設置擴展狀態以最大化

我希望JFrame在最大化屏幕中顯示。

萬分感謝。

我需要將高度設置為自動,以便用戶可以看到整個表單。

你用:

pack();
setVisible(true);

pack()方法將使框架成為添加到框架的所有組件的首選大小。

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

不要使用setPreferredSize(...)。 每個Swing組件將根據文本,字體等確定自己的首選大小。

編輯:

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

上面的行似乎引起了問題。 您要指定5行,但我想框架上沒有足夠的空間容納5行。

嘗試:

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

我使用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