简体   繁体   English

如何在JTextArea上添加JScrollPane

[英]How to add JScrollPane on JTextArea

I am going to make a Simple Notepad. 我要制作一个简单的记事本。 I have used JTextArea for writing some text in it and I want scrollbar on JTextArea . 我已经使用JTextArea在其中编写了一些文本,我想在JTextArea scrollbar I have written few lines of code. 我写了几行代码。

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();
    }
}

I'm not sure where the issue lies. 我不确定问题出在哪里。

There are three problems here : 这里有三个问题:

1) You add the area to the JScrollPane , before area is initialized. 1)在初始化area之前,将area添加到JScrollPane

So you end up with a JScrollPane containing a null component. 所以最终得到一个包含null组件的JScrollPane

To fix this, instantiate area before adding it to the JScrollPane . 要解决此问题,请在将area添加到JScrollPane之前实例化area

2) You add area to the JFrame , then you add the JScrollPane containing area . 2)您将area添加到JFrame ,然后添加包含JScrollPanearea

This is wrong, a Component can't be added several times. 这是错误的, Component不能多次添加。 The last addition will win, so you end up with your JFrame containing a mix between a JTextArea , and a JScrollPane now containing null . 最后一个添加将获胜,因此您最终得到的JFrame包含JTextArea和现在包含nullJScrollPane之间的混合。

To fix this, juste remove add(area); 为了解决这个问题,juste删除add(area); .

3) You call setVisible too early 3)你太早调用setVisible

You should call setVisible(true) , only when all components have been added. 只有在添加了所有组件后,才应调用setVisible(true)

The following code shows the according modifications to the two relevant methods (comments have been added for changes) : 以下代码显示了对两个相关方法的相应修改(已为更改添加了注释):

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");
}

You added the area to the scroll pane before instantiating the area. 在实例化区域之前,您已将区域添加到滚动窗格。 Just switch scroll and area instantiation lines. 只需切换滚动和区域实例化行。

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

Should be enough 应该够了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM