如何在 JTextArea 上添加 JScrollPane

How to add JScrollPane on JTextArea

我要做一个简单的记事本。我已经使用 JTextArea 在其中写了一些文本,我想在 JTextArea 上使用 scrollbar。我写了几行代码。

package project.notepad;

import javax.swing.*;
import java.awt.*;

public class Notepad extends JFrame {
    private JTextArea area;
    private JMenu filemenu;
    private JMenu editmenu;
    private JMenu formatmenu;
    private JMenu helpmenu;
    private JScrollPane scroll;

    private JMenuBar menubar;
    private JMenuItem newmenuitem;
    private JMenuItem openmenuitem;
    private JMenuItem savemenuitem;
    private JMenuItem exitmenuitem;


    public Notepad() {
        initComponents();
        setComponents();

        setTitle("Simple Notepad");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(true);
        setSize(600, 600);
        setJMenuBar(menubar);


        menubar.add(filemenu);
        menubar.add(editmenu);
        menubar.add(formatmenu);
        menubar.add(helpmenu);
        filemenu.add(newmenuitem);
        filemenu.add(openmenuitem);
        filemenu.add(savemenuitem);
        filemenu.add(exitmenuitem);

        add(area);
        add(scroll);
    }


    public final void initComponents() {
        scroll = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        area = new JTextArea();
        menubar = new JMenuBar();
        filemenu = new JMenu("    File");
        editmenu = new JMenu("    Edit");
        formatmenu = new JMenu("    Format");
        helpmenu = new JMenu("    Help");
        newmenuitem = new JMenuItem("    New");
        openmenuitem = new JMenuItem("    Open");
        savemenuitem = new JMenuItem("    Save");
        exitmenuitem = new JMenuItem("    Exit");
    }

    public final void setComponents() {
        area.setSize(600, 600);
        area.setBackground(Color.WHITE);
    }


    public static void main(String[] args) {
        new Notepad();
    }
}

我不确定问题出在哪里。

这里存在三个问题:

1) 在初始化 area 之前将 area 添加到 JScrollPane

所以你最终得到一个包含 null 组件的 JScrollPane

要解决此问题,请先实例化 area,然后再将其添加到 JScrollPane

2) 您将 area 添加到 JFrame,然后添加包含 areaJScrollPane

这是错误的,一个Component不能加几次。最后一次添加将获胜,因此您最终得到 JFrame 包含 JTextArea 和现在包含 nullJScrollPane 的混合。

要解决此问题,只需删除 add(area); .

3)你叫setVisible太早了

您应该调用 setVisible(true),只有当所有组件都已添加时。

以下代码显示了对两个相关方法的相应修改(更改已添加注释):

public Notepad() {
    initComponents();
    setComponents();

    setTitle("Simple Notepad");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setResizable(true);
    setSize(600, 600);
    setJMenuBar(menubar);

    menubar.add(filemenu);
    menubar.add(editmenu);
    menubar.add(formatmenu);
    menubar.add(helpmenu);
    filemenu.add(newmenuitem);
    filemenu.add(openmenuitem);
    filemenu.add(savemenuitem);
    filemenu.add(exitmenuitem);

    //add(area); // remove this, the textarea is already added to the scrollpane
    add(scroll);

    // set the frame visible, only once all components have been added
    setVisible(true);
}

public final void initComponents() {

    area = new JTextArea(); // instantiate the textarea, before adding to the scrollpane
    scroll = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    menubar = new JMenuBar();
    filemenu = new JMenu("    File");
    editmenu = new JMenu("    Edit");
    formatmenu = new JMenu("    Format");
    helpmenu = new JMenu("    Help");
    newmenuitem = new JMenuItem("    New");
    openmenuitem = new JMenuItem("    Open");
    savemenuitem = new JMenuItem("    Save");
    exitmenuitem = new JMenuItem("    Exit");
}

您在实例化区域之前将区域添加到滚动窗格。只需切换滚动和区域实例化行。

area = new JTextArea();    
scroll = new JScrollPane (area,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);   

应该够了