【问题标题】:Swing using GridBagLayout with GridBagConstraintsSwing 使用 GridBagLayout 和 GridBagConstraints
【发布时间】:2015-08-02 18:15:11
【问题描述】:

我正在制作的第一个 java GUI 应用程序。

基本上,我想要实现的目标看起来很简单。 这是一个简单的水平布局,首先包含一个简单的 JLabel、一个 JTextArea、JButton 和至少另一个 JLabel。

这是我为说明而制作的简单图片:

澄清一下,我不希望文本区域和底部标签具有固定大小,而是填充它们的空间,并且在其他组件之间也有一些小填充。

这是我目前制作的代码,如上图所示,这还不是我想要实现的目标:

public class QueryPanel extends JPanel{
    private JLabel headerLabel;

    private String defaultString = "Insert query here...";
    private boolean isTextFieldClicked = false;
    private JTextArea queryTextArea;
    private JScrollPane queryScrollPane;

    private JButton executeQueryButton;

    private JTextArea resultTextArea;
    private JScrollPane resultScrollPane;

    public QueryPanel(String headerText){
        GridBagLayout gridLayout = new GridBagLayout();
        setLayout(gridLayout);

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.HORIZONTAL;
        headerLabel = new JLabel();
        headerLabel.setText(headerText);
        constraints.gridy = 0;
        add(headerLabel, constraints);

        queryTextArea = new JTextArea(defaultString);
        queryScrollPane = new JScrollPane(queryTextArea);
        constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.weighty = 1.0;
        constraints.weightx = 1.0;
        constraints.gridy = 1;
        add(queryScrollPane, constraints);

        executeQueryButton = new JButton("Execute Query");
        constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.gridy = 2;
        add(executeQueryButton, constraints);

        resultTextArea = new JTextArea();
        resultTextArea.setEditable(false);
        resultScrollPane = new JScrollPane(resultTextArea);
        constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.weighty = 1.0;
        constraints.weightx = 1.0;
        constraints.gridy = 3;
        add(resultScrollPane, constraints);
    }
}

附:关于底部组件,想知道将静态结果显示为大标签而不是不可编辑的文本区域是否更好......找不到哪个更“正确”。

【问题讨论】:

  • resultTextArea = new JTextArea(); 可以通过设置预期文本的列号和行号来建议首选大小。这可以在构造函数中完成。一般提示: 1) 为了尽快获得更好的帮助,请发帖 minimal reproducible exampleShort, Self Contained, Correct Example。 2) 以最小尺寸提供 ASCII 艺术或 GUI 的预期布局的简单绘图,如果可调整大小,则具有更大的宽度和高度。

标签: java swing layout layout-manager gridbaglayout


【解决方案1】:

在上面粘贴为问题(恕我直言)的代码中,我觉得有两个问题。

  1. weightx/weighty 都有值1.0,它应该是 在weighty 方面有所不同,因为每个组件都假定 在父容器上获取​​不同的长度。
  2. gbc.fill 设置为 HORIZONTAL,这意味着在调整大小 parent,组件只会在水平方向调整大小。 对我来说,如果组件将在 两个方向,而不是一个方向,以获得良好的视觉外观

这里试试这个例子:

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

public class GridBagLayoutExample {

    private static final int GAP = 5;
    private GridBagConstraints gbc;

    private JTextArea insertQueryTextArea;
    private JTextArea resultTextArea;
    private JScrollPane scroller;
    private JButton executeQueryButton;

    public GridBagLayoutExample () {
        gbc = new GridBagConstraints ();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.insets = new Insets ( GAP, GAP, GAP, GAP );
        gbc.fill = GridBagConstraints.BOTH;
    }

    private void displayGUI () {
        JFrame frame = new JFrame ( "" );
        frame.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );

        JPanel contentPane = getPanel ();
        contentPane.setLayout ( new BorderLayout ( GAP, GAP) );

        JPanel containerPanel = getPanel ();
        containerPanel.setLayout ( new GridBagLayout () );
        addComponent ( 0, 0, 1, 1, 1.0, 0.1, containerPanel,
            new JLabel ( "Please inesrt your DML query here: ", JLabel.CENTER ) );
        insertQueryTextArea = getTextArea ();
        scroller = new JScrollPane ();
        scroller.setViewportView ( insertQueryTextArea );
        addComponent ( 0, 1, 1, 1, 1.0, 0.4, containerPanel, scroller );
        JPanel buttonPanel = getPanel ();
        executeQueryButton = new JButton ( "Execute Query" );
        buttonPanel.add ( executeQueryButton );
        addComponent ( 0, 2, 1, 1, 1.0, 0.1, containerPanel, buttonPanel );
        resultTextArea = getTextArea ();
        scroller = new JScrollPane ();
        scroller.setViewportView ( resultTextArea );
        addComponent ( 0, 3, 1, 1, 1.0, 0.4, containerPanel, scroller );

        contentPane.add ( containerPanel, BorderLayout.CENTER );

        frame.setContentPane ( contentPane );
        frame.pack ();
        frame.setLocationByPlatform ( true );
        frame.setVisible ( true );
    }

    private void addComponent ( int gridx, int gridy,
                                    int gridwidth, int gridheight,
                                    double weightx, double weighty,
                                    JComponent container, JComponent component ) {
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.gridwidth = gridwidth;
        gbc.gridheight = gridheight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;

        container.add ( component, gbc );
    }

    private JTextArea getTextArea () {
        JTextArea tArea = new JTextArea ( 10, 10 );
        tArea.setLineWrap ( true );
        tArea.setWrapStyleWord ( true );

        return tArea;
    }

    private JPanel getPanel () {
        JPanel panel = new JPanel ();
        panel.setOpaque ( true );
        panel.setBorder ( BorderFactory.createEmptyBorder ( GAP, GAP, GAP, GAP ) );

        return panel;
    }

    public static void main ( String [] args ) {
        Runnable runnable = new Runnable () {
            @Override
            public void run () {
                new GridBagLayoutExample ().displayGUI ();
            }
        };
        EventQueue.invokeLater ( runnable );
    }
}
关于底部组件,想知道将静态结果显示为大标签而不是不可编辑的文本区域是否更好......找不到哪个更“正确”。

如果滚动JTextArea 的内容是实际设计背后的想法,那么显示结果的JTextArea 肯定是一个合适的解决方案。

这是输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 2017-07-13
    • 2021-06-21
    相关资源
    最近更新 更多