使用GridBagLayout和GridBagConstraints进行Swing

时间:2015-08-02 18:15:12

标签: java swing layout layout-manager gridbaglayout

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

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

这是一张简单的照片,我用它来说明: enter image description here

为了澄清,我不希望文本区域和底部标签具有固定的大小,但要填充它们所拥有的空间,并且还要在其他组件之间有一些小的填充。

这是我到目前为止所编写的代码,如上图所示,它还不是我想要实现的目标:

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);
    }
}

P.S。关于底部组件,想知道将静态结果显示为大标签而不是不可编辑的文本区域是否更好......无法找到更多"对"要做。

1 个答案:

答案 0 :(得分:2)

在我上面粘贴的代码(恕我直言)中,对我来说似乎有些不对劲。

  1. weightx/weighty两者的值都应为1.0weighty而不同,因为每个组件都是假设的 在父容器上获取​​不同的长度。
  2. gbc.fill设置为HORIZONTAL,这意味着调整大小 父级,组件只会在水平方向上调整大小。 对我来说,如果组件扩展,那将更合适 两个方向,而不仅仅是一个方向,以获得良好的视觉效果
  3. 这里尝试这个例子:

    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将是一个合适的解决方案。

    这是输出:

    GRIDBAGLAYOUT_EXAMPLE