gpt4 book ai didi

java - 使用带有 GridBagConstraints 的 GridBagLayout 进行 Swing

转载 作者:行者123 更新时间:2023-11-30 03:19:07 26 4
gpt4 key购买 nike

我正在制作的第一个 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);
}
}

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

最佳答案

在上面作为问题粘贴的代码中,有两件事对我来说似乎是错误的(恕我直言)。

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

这里试试这个例子:

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 );
}
}
Regarding the bottom component, wondering if it's better to show static results as a large label instead of a non-editable text area... couldn't find which is more "right" to do.

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

这是输出:

GRIDBAGLAYOUT_EXAMPLE

关于java - 使用带有 GridBagConstraints 的 GridBagLayout 进行 Swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31775049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com