gpt4 book ai didi

java - GridBagLayout 动态使用面板内的额外空间

转载 作者:行者123 更新时间:2023-11-30 04:18:26 25 4
gpt4 key购买 nike

我想创建 2 个嵌套边框布局,其中内部边框布局将面板保持在其中心。该面板应具有垂直流动,特定组件应增长并共同使用所有可用空间。最后,这个面板应该由 JScrollPane 包裹,因此只要不是所有内容都可以以其默认大小显示,它就会获得一个垂直滚动条。大致如下:

+-------------------------------+
| |
| +-----------------------+ |
| | don't grow | |
| +-----------------------+ |
| |
| +-----------------------+ |
| | | |
| | growy | |
| | | |
| +-----------------------+ |
| |
| +-----------------------+ |
| | don't grow | |
| +-----------------------+ |
| |
| +-----------------------+ |
| | | |
| | growy | |
| | | |
| +-----------------------+ |
| |
+-------------------------------+

这就是我走了多远:

enter image description here

提供以下 SSCCE:

public class SimpleMain {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//createAndShowMigGUI();
createAndShowSwingGUI();
}
});
}

private static void createAndShowSwingGUI() {
JFrame frame = new JFrame("Swing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

/* Frame */
frame.setLayout(new BorderLayout());

/* Outer Panel */
JPanel outerPanel = new JPanel(new BorderLayout());

outerPanel.add(new JTextField("inner north"), BorderLayout.NORTH);
outerPanel.add(new JTextField("inner south"), BorderLayout.SOUTH);
outerPanel.add(new JTextField("inner west"), BorderLayout.WEST);
outerPanel.add(new JTextField("inner east"), BorderLayout.EAST);

/* Text Area */
JTextArea area = new JTextArea("aHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd AskjdhuyawduyawdHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd AskjdhuyawduyawdHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd AskjdhuyawduyawdHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd AskjdhuyawduyawdHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd");
area.setLineWrap(true);
area.setWrapStyleWord(true);

/* Elements Container */
JPanel innerPanel = new JPanel(new GridBagLayout());

/* Grid Bag Constraints */
GridBagConstraints cons = new GridBagConstraints();
cons.fill = GridBagConstraints.HORIZONTAL;
cons.weightx = 1;
cons.gridx = 0;

/* Elements */
innerPanel.add(area, cons); // This component should use all available Y-space
innerPanel.add(new JRadioButton("Test hello"), cons); // Remain as-is
innerPanel.add(new JTextField("some text field..."), cons); // Remain as-is
innerPanel.add(new JTextField("some text field PUSH..."), cons); // This component should use all available Y-space

/* Add Inner Panel To Outer Panel */
outerPanel.add(new JScrollPane(innerPanel), BorderLayout.CENTER);

/* Add outer elements */
frame.getContentPane().add(new JTextField("outer north"), BorderLayout.NORTH);
frame.getContentPane().add(new JTextField("outer south"), BorderLayout.SOUTH);
frame.getContentPane().add(new JTextField("outer west"), BorderLayout.WEST);
frame.getContentPane().add(new JTextField("outer east"), BorderLayout.EAST);
frame.getContentPane().add(outerPanel, BorderLayout.CENTER);

frame.setSize(800, 600);
frame.setVisible(true);
frame.setLocation(40, 40);
}
}

最佳答案

我在使用 GridBagConstraints 时遇到问题对于 GridBagLayout 的所有组件,所以我为每个组件创建了约束。反正我不记得默认值。

更改包括添加权重并为 GridBagConstraints 设置适当的填充。 .

Swing GridBagLayout

已编辑以回复评论。

当窗口 ( JFrame ) 展开时, JTextArea与窗口中的其他组件一起展开。当窗口尺寸缩小时,JTextArea不会收缩。为了解决这个问题,我把 JTextAreaJScrollPane ,并删除并添加 JTextArea每次JFrame大小已更改。

这是代码。

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class SimpleMain {

protected static final Insets DEFAULT_INSETS = new Insets(0, 4, 4, 4);

protected static JTextArea area;

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// createAndShowMigGUI();
createAndShowSwingGUI();
}
});
}

private static void createAndShowSwingGUI() {
final JFrame frame = new JFrame("Swing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

/* Frame */
frame.setLayout(new BorderLayout());

/* Outer Panel */
JPanel outerPanel = new JPanel(new BorderLayout());

outerPanel.add(new JTextField("inner north"), BorderLayout.NORTH);
outerPanel.add(new JTextField("inner south"), BorderLayout.SOUTH);
outerPanel.add(new JTextField("inner west"), BorderLayout.WEST);
outerPanel.add(new JTextField("inner east"), BorderLayout.EAST);

area = setTextArea();

final JScrollPane scrollPane = new JScrollPane(area);

/* Elements Container */
final JPanel innerPanel = new JPanel(new GridBagLayout());

/* Elements */
int gridy = 0;

// This component should use all available Y-space
addComponent(innerPanel, scrollPane, 0, gridy++, 1, 1, 1.0D, 800.0D,
DEFAULT_INSETS, GridBagConstraints.LINE_START,
GridBagConstraints.BOTH);

// Remain as-is
JRadioButton helloButton = new JRadioButton("Test hello");
addComponent(innerPanel, helloButton, 0, gridy++, 1, 1, 1.0D, 1.0D,
DEFAULT_INSETS, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);

// Remain as-is
JTextField textField1 = new JTextField("some text field...");
addComponent(innerPanel, textField1, 0, gridy++, 1, 1, 1.0D, 1.0D,
DEFAULT_INSETS, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);

// This component should use all available Y-space
JTextField textField2 = new JTextField("some text field PUSH...");
addComponent(innerPanel, textField2, 0, gridy++, 1, 1, 1.0D, 800.0D,
DEFAULT_INSETS, GridBagConstraints.LINE_START,
GridBagConstraints.BOTH);

/* Add Inner Panel To Outer Panel */
outerPanel.add(new JScrollPane(innerPanel), BorderLayout.CENTER);

/* Add outer elements */
frame.getContentPane().add(new JTextField("outer north"),
BorderLayout.NORTH);
frame.getContentPane().add(new JTextField("outer south"),
BorderLayout.SOUTH);
frame.getContentPane().add(new JTextField("outer west"),
BorderLayout.WEST);
frame.getContentPane().add(new JTextField("outer east"),
BorderLayout.EAST);
frame.getContentPane().add(outerPanel, BorderLayout.CENTER);

frame.addWindowStateListener(new WindowStateListener() {
@Override
public void windowStateChanged(WindowEvent event) {
scrollPane.getViewport().remove(area);
area = setTextArea();
scrollPane.getViewport().add(area);
}
});

frame.setSize(800, 600);
frame.setLocation(40, 40);
frame.setVisible(true);
}

private static JTextArea setTextArea() {
/* Text Area */
JTextArea area = new JTextArea();
area.setText("aHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd "
+ "AskjdhuyawduyawdHJAskjdhuyawduyawd Askjdhuyawduyawd "
+ "Askjdhuyawduyawd AskjdhuyawduyawdHJAskjdhuyawduyawd "
+ "Askjdhuyawduyawd Askjdhuyawduyawd "
+ "AskjdhuyawduyawdHJAskjdhuyawduyawd Askjdhuyawduyawd "
+ "Askjdhuyawduyawd AskjdhuyawduyawdHJAskjdhuyawduyawd "
+ "Askjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd");

area.setLineWrap(true);
area.setWrapStyleWord(true);
return area;
}

private static void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight,
double weightx, double weighty, Insets insets, int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, weightx, weighty, anchor, fill, insets,
0, 0);
container.add(component, gbc);
}
}

关于java - GridBagLayout 动态使用面板内的额外空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17748026/

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