我有你在图片中看到的结构。
对于这两个面板,都使用了 GridBagLayout。问题是滚动窗格内的文本字段并不完全可见。父面板仅会拉伸以使按钮可见,但当滚动条出现时,它只会与文本字段重叠。有没有简单的解决方案来解决这个问题(不想处理设置自定义/首选/最小高度)?
面板结构:
问题 :
好的,这是一个 SSCCE
public class Main {
JFrame frame;
private JPanel mainPanel;
private JButton button1;
private JButton button2;
private JTextField someTextTextField;
public static void main(String[] args) {
Main main = new Main();
main.show();
}
private void show() {
frame = new JFrame("Frame");
frame.setContentPane(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
{
$$$setupUI$$$();
}
/**
* Method generated by IntelliJ IDEA GUI Designer
* >>> IMPORTANT!! <<<
* DO NOT edit this method OR call it in your code!
*
* @noinspection ALL
*/
private void $$$setupUI$$$() {
mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
mainPanel.setPreferredSize(new Dimension(209, 30));
final JPanel panel1 = new JPanel();
panel1.setLayout(new GridBagLayout());
GridBagConstraints gbc;
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 0.5;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
mainPanel.add(panel1, gbc);
button1 = new JButton();
button1.setText("Button");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel1.add(button1, gbc);
button2 = new JButton();
button2.setText("Button");
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel1.add(button2, gbc);
final JScrollPane scrollPane1 = new JScrollPane();
scrollPane1.setAlignmentX(0.0f);
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.5;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
mainPanel.add(scrollPane1, gbc);
someTextTextField = new JTextField();
someTextTextField.setText("some text");
scrollPane1.setViewportView(someTextTextField);
}
/**
* @noinspection ALL
*/
public JComponent $$$getRootComponent$$$() {
return mainPanel;
}
}