gpt4 book ai didi

Java Swing 。 ScrolPane 内的 TextField 不完全可见

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

我有你在图片中看到的结构。

对于这两个面板,都使用了 GridBagLayout。问题是滚动 Pane 内的文本字段并不完全可见。父面板仅在按钮可见时拉伸(stretch),但当滚动条出现时,它只是与文本字段重叠。有没有一个简单的解决方案来解决这个问题(不想处理设置自定义/首选/最小高度)?

面板结构:

enter image description here

问题:

enter image description here

好的,这是一个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/

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