使用带有 GridBagConstraints 的 GridBagLayout 摆动
Swing using GridBagLayout with GridBagConstraints
我制作的第一个 java GUI 应用程序。
基本上,我想要实现的目标看起来很简单。
这是一个简单的水平布局,首先包含一个简单的 JLabel、一个 JTextArea、JButton 和至少另一个 JLabel。
我制作了一张简单的图片来说明:
澄清一下,我不希望文本区域和底部标签具有固定大小,而是要填充它们所具有的 space,并且在其他组件之间也有一些小填充。
这是我到目前为止所做的代码,正如您在上图中看到的那样,这还不是我想要实现的目标:
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。关于底部组件,想知道将静态结果显示为大标签而不是不可编辑的文本区域是否更好...找不到哪个更 "right" 要做。
上面作为问题粘贴的代码中有两件事对我来说似乎是错误的(恕我直言)。
weightx/weighty
都具有值 1.0
,但应该是
在 weighty
方面有所不同,因为每个组件都应该
在父容器上获取不同的长度。
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
来显示结果肯定是一个合适的解决方案。
这是输出:
我制作的第一个 java GUI 应用程序。
基本上,我想要实现的目标看起来很简单。 这是一个简单的水平布局,首先包含一个简单的 JLabel、一个 JTextArea、JButton 和至少另一个 JLabel。
我制作了一张简单的图片来说明:
澄清一下,我不希望文本区域和底部标签具有固定大小,而是要填充它们所具有的 space,并且在其他组件之间也有一些小填充。
这是我到目前为止所做的代码,正如您在上图中看到的那样,这还不是我想要实现的目标:
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。关于底部组件,想知道将静态结果显示为大标签而不是不可编辑的文本区域是否更好...找不到哪个更 "right" 要做。
上面作为问题粘贴的代码中有两件事对我来说似乎是错误的(恕我直言)。
weightx/weighty
都具有值1.0
,但应该是 在weighty
方面有所不同,因为每个组件都应该 在父容器上获取不同的长度。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
来显示结果肯定是一个合适的解决方案。
这是输出: