gpt4 book ai didi

java - JScrollPane 不适用于 JTextArea

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

我创建了一个小弹出窗口。我有 2 个输入,它们是 JTextField 和 JTextArea。我将 JScrollPane 添加到 JTextArea。当我运行程序时,会看到滚动 Pane 。但我无法使用它。我分享图像的外观。我想我必须增加高度,但我不知道什么高度

enter image description here

public class AddCommentDialog extends JDialog implements ShowableDialog, ActionListener {

private static final long serialVersionUID = 1L;
private static JTextField inputTitle = new JTextField();
private static JTextArea inputComment = new JTextArea();
private Rectangle rectangle = new Rectangle();
private JLabel lblTitle = new JLabel("Başlık: ");
private JLabel lblComment = new JLabel("Yorum: ");
private JButton buttonSave = new JButton(" Kaydet ");
private JButton buttonCancel = new JButton(" İptal ");

// Kullanıcı tarafından girilen başlık ve yorum
private String title, comment;


/**
* The constructor that sets the fields and the dialog.
*/
public AddCommentDialog() {

super(ImageViewerGui.getMainFrame(), "Yorum Ekle", true);

JPanel panel = new JPanel(new BorderLayout(5, 5));
// the input text fields
JPanel northContainer = new JPanel(new BorderLayout(7, 7));

JPanel labelPanel = new JPanel(new BorderLayout(5, 5));
JPanel fieldPanel = new JPanel(new BorderLayout(5, 5));

// the buttons
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));

buttonSave.addActionListener(this);
buttonCancel.addActionListener(this);
inputTitle.setPreferredSize(new Dimension(250, 25));
inputComment.setPreferredSize(new Dimension(250, 75));
buttonSave.setPreferredSize(new Dimension(90, 25));
buttonCancel.setPreferredSize(new Dimension(90, 25));

// Satır bitince alt satıra inmesi için
inputComment.setLineWrap(true);
JScrollPane scrollCommentArea = new JScrollPane(inputComment);
scrollCommentArea.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

try {

labelPanel.add(lblTitle, BorderLayout.NORTH);
labelPanel.add(lblComment, BorderLayout.CENTER);

fieldPanel.add(inputTitle, BorderLayout.NORTH);
fieldPanel.add(scrollCommentArea, BorderLayout.CENTER);
buttonPanel.add(buttonCancel);
buttonPanel.add(buttonSave);

northContainer.add(labelPanel, BorderLayout.WEST);
northContainer.add(fieldPanel, BorderLayout.CENTER);
panel.add(northContainer, BorderLayout.NORTH);
panel.add(buttonPanel, BorderLayout.SOUTH);
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
inputTitle.setText("");
inputComment.setText("");
add(panel);
this.setResizable(true);

} catch (Exception e) {
e.printStackTrace();
}
}

最佳答案

static 会成为潜在问题,但 inputComment.setPreferredSize(new Dimension(250, 75)); 绝对是您的核心问题的一部分。请改用 setRowssetColumns

inputComment.setRows(4);
inputComment.setColumns(38);

基本问题是,JScrollPane 依赖于组件的首选大小来确定它是否应该显示滚动条(或者它们是否实际执行任何操作)

另见 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

关于java - JScrollPane 不适用于 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32517462/

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