JScrollPane 不适用于 JTextArea

JScrollPane does not work with JTextArea

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

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?