【问题标题】:Issues with JTextField size behavior using JGoodies.FormLayout使用 JGoodies.FormLayout 的 JTextField 大小行为问题
【发布时间】:2022-02-09 20:42:12
【问题描述】:

我正在尝试使用JGoodies.FormLayout 布置一个基本的 JPanel,但我遇到了一个问题,即我放置的 JTextField 组件保持在最小尺寸,而不管我为解决该问题所做的任何事情。废话不多说,

Program.java:

package example;

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import example.gui.*;

public class Program {

    private static JTabbedPane tabbedViewControl;
    private static TabPanel someTabPanel;

    private static void createAndShowGUI() {
        JFrame mainFrame = new JFrame("Stack Overflow Example");
        mainFrame.setResizable(true);
        mainFrame.setSize(1200, 800);
        mainFrame.setTitle("Example");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        mainFrame.getContentPane().setLayout(new GridLayout(1, 1, 0, 0));
        tabbedViewControl = new JTabbedPane(JTabbedPane.TOP);
        someTabPanel = new TabPanel();
        tabbedViewControl.addTab("Example Tab", someTabPanel);
        mainFrame.getContentPane().add(tabbedViewControl);

        //Display the window.
        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(() -> {
            createAndShowGUI();
        });
    }
}

example.gui.TabPanel.java:

package example.gui;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class TabPanel extends JPanel {

    private static Border       textFieldBorderStyle;
    private static ButtonGroup  userChoiceButtonGroup;
    private static PanelBuilder builder;
    private static FormLayout   layout;
    private static JLabel       descriptionLabel;
    private static JLabel       userChoiceLabel;
    private static JLabel       nameLabel;
    private static JLabel       data2Label;
    private static JLabel       data1Label;
    private static JRadioButton userChoice1RadioButton;
    private static JRadioButton userChoice3RadioButton;
    private static JRadioButton userChoice4RadioButton;
    private static JRadioButton userChoice2RadioButton;
    private static JRadioButton userChoice5RadioButton;
    private static JTextArea    descriptionField;
    private static JTextField   nameField;
    private static JTextField   data2Field;
    private static JTextField   data1Field;
    private static JPanel       subPanel;

    public TabPanel() {
        super(new GridBagLayout());
        initComponents();
    }

    private void initComponents() {
        nameLabel               = new JLabel("Name");
        nameField               = new JTextField();
        descriptionLabel        = new JLabel("Description");
        descriptionField        = new JTextArea("");
        data1Label             = new JLabel("Data No. 1");
        data1Field             = new JTextField();
        data2Label           = new JLabel("Data No. 2");
        data2Field           = new JTextField();
        userChoiceLabel        = new JLabel("User choices");
        userChoiceButtonGroup  = new ButtonGroup();
        userChoice1RadioButton     = new JRadioButton("Choice I");
        userChoice3RadioButton     = new JRadioButton("Choice II");
        userChoice4RadioButton     = new JRadioButton("Choice III");
        userChoice2RadioButton = new JRadioButton("Choice IV");
        userChoice5RadioButton = new JRadioButton("Choice V");

        subPanel = constructPanel();
        this.add(subPanel);
    }

    private JPanel constructPanel() {
        layout = new FormLayout(
            "r:p, 3dlu, l:max(1in;p), 7dlu, r:p, 3dlu, l:p, 3dlu, l:p, 3dlu, l:p",
            "p, 3dlu, p, 3dlu, p, 9dlu, p, 3dlu, p, 3dlu, p"
        );
        builder = new PanelBuilder(layout);
        CellConstraints cc = new CellConstraints();

        textFieldBorderStyle = nameField.getBorder();
        descriptionField.setBorder(textFieldBorderStyle);

        userChoiceButtonGroup.add(userChoice1RadioButton);
        userChoiceButtonGroup.add(userChoice3RadioButton);
        userChoiceButtonGroup.add(userChoice4RadioButton);
        userChoiceButtonGroup.add(userChoice2RadioButton);
        userChoiceButtonGroup.add(userChoice5RadioButton);

        builder.addSeparator("Name & Description", cc.xyw(1, 1, 3));
        builder.add(nameLabel,        cc.xy  (1, 3));
        builder.add(nameField,        cc.xy  (3, 3));
        builder.add(descriptionLabel, cc.xy  (1, 5));
        builder.add(descriptionField, cc.xywh(3, 5, 1, 4));
        builder.addSeparator("Other Data for the User to Enter", cc.xyw(5, 1, 5));
        builder.add(data1Label,             cc.xy(5,  3));
        builder.add(data1Field,             cc.xy(7,  3));
        builder.add(data2Label,           cc.xy(5,  5));
        builder.add(data2Field,           cc.xy(7,  5));
        builder.add(userChoiceLabel,        cc.xy(5,  7));
        builder.add(userChoice2RadioButton, cc.xy(7,  7));
        builder.add(userChoice5RadioButton, cc.xy(7,  9));
        builder.add(userChoice3RadioButton,     cc.xy(7, 11));
        builder.add(userChoice4RadioButton,     cc.xy(9,  7));
        builder.add(userChoice1RadioButton,     cc.xy(9,  9));

        return builder.getPanel();
    }
}

结果如下:

理想情况下,我希望发生三件事,而不是以上:

  1. PanelBuilder 返回的 JPanel 占据其父级的全部尺寸
  2. JTextFields 和 JTextArea 具有合理的宽度(而不是调整大小*)
  3. 这两个方面更成比例 - 我不期望 50%/50%,但我不希望右侧将左侧挤压到边界上

我在这方面做了大量的功课,但大多想出以下非结果:

  1. 尝试在 SO 上进行搜索主要会产生“本机”AWT 布局系统的问题
  2. 通过JGoodies Forms whitepaper,同时让我使用max(constantSize;preferredSize),仅协助调整分隔符的大小,并没有做任何事情:JTextFields

那么,简而言之,有没有办法强制 JTextFields 在FormLayout 中占据其列的给定宽度?

【问题讨论】:

    标签: java swing layout-manager jtextfield jgoodies


    【解决方案1】:

    事情就是这样,最好用边框来放组件,

    //create title border so later on you can set it to your panel
    TitledBorder topBorder = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Title message To the border");// you can use more or border type in BorderFactory.class
    

    然后你放

    // initialize the frame layout 
    frame.setLayout(new BorderLayout(20, 30));
    

    你可以定义为foreach边框。

    //create panel and set panel layout depend on whatever you want to choose
    //then set panels border to that TitledBorder you have created before
    JPanel topPanel= new JPanel();
    topPanel.setLayout(new GridLayout(2, 3, 10, 10)); // grid layout can be managed easier and better 
    topPanel.setBorder(topBorder); // you have defined topBorder before
    

    最后

    // initialize textfiled and add it to toppanel 
    JTextField yourFiled = new JTextField();
    topPanel.add(messageLabel);
    

    您可以在 JTextFiled 中为您想要的字段定义所有属性,也可以跳过创建 TitledBorder 的第一步,但我总是这样做,因为它更好、更有条理

    【讨论】:

      猜你喜欢
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      相关资源
      最近更新 更多