我有你在图片中看到的结构。
对于这两个面板,都使用了 GridBagLayout。 问题是滚动 Pane 内的文本字段并不完全可见。 父面板仅在按钮可见时拉伸(stretch),但当滚动条出现时,它只是与文本字段重叠。 有没有一个简单的解决方案来解决这个问题(不想处理设置自定义/首选/最小高度)?
面板结构:
问题:
好的,这是一个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;
}
最佳答案
尝试使用 JTextArea
而不是 JTextField
。如果您不想设置自定义/首选/最小尺寸,您可以使用 JTextArea.setRows(int rows)
方法。希望对您有所帮助。
关于Java Swing 。 ScrolPane 内的 TextField 不完全可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12156268/