java 中找不到 JTextArea 的自动换行不可用

word wrap for JTextArea not available not found in java

我正在创建记事本,并且我已经像在记事本中一样提供了自动换行选项

但是当我写的时候

textArea.setLineWrap(true);

比它给我的错误如图所示

cannot Find Symbol
Symbol: method setLineWrap(boolean)
Location: Variable textArea of type TextArea

即使我按“.”下拉菜单用于 textArea,但不显示 setLineWrap 布尔方法 到目前为止,这是我的代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test3;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

class Test3 extends JFrame{
    private final JMenu Format;
    private final JMenuItem Word;  
    private final TextArea textArea = new TextArea("", 0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
    public Test3(){
                setLayout(new FlowLayout());    //Default layout
            
            JMenuBar menubar=new JMenuBar();
            setJMenuBar(menubar);
            this.getContentPane().setLayout(new BorderLayout());
            this.getContentPane().add(textArea);
            Format=new JMenu("Format");
            Word=new JMenuItem("Word wrap");
            Format.add(Word);
            menubar.add(Format);
            event1 e1 =new event1 ();
            Word.addActionListener(e1);
    }
            public class event1 implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e) {
            //textArea.setLineWrap(true);
            //textArea.setWrapStyleWord(true);
        }
    }   
public static void main(String []args){
        Test3 t=new Test3();
        t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.setTitle("Notepad");
        t.setVisible(true);
        t.setSize(1280,786);
    
  }
}

而不是使用 TextArea 使用 JTextArea.

class test2{
    private final JMenu Format;
    private final JMenuItem Word;  
    private final JTextArea textArea = new JTextArea("", 0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
    test2(){
       setLayout (new FlowLayout());    //Default layout

        JMenuBar menubar=new JMenuBar();
        setJMenuBar(menubar);
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(textArea);
        format=new JMenu("Format");
        Word=new JMenuItem("Word wrap");
        format.add(Word);
        menubar.add(format);
        event1 e1 =new event1 ();
        Word.addActionListener(e18);
        }
        public class event18 implements ActionListener{

       @Override
    public void actionPerformed(ActionEvent e) {
     textArea.setLineWrap(true);
     textArea.setWrapStyleWord(true);
    }
    }   
public static void main(String []args){
        Test2 t=new Test2();
        t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.setTitle("Notepad");
        t.setVisible(true);
        t.setSize(1280,786);

  }
}

我稍微修改了你的代码。 我使用了 JTextArea 而不是 TextArea 和一个 JScrollPane 来包装 JTextArea

class Test3 extends JFrame {

    private final JMenu Format;
    private final JMenuItem Word;
    private final JTextArea textArea = new JTextArea("", 0, 0);

    public Test3() {
        setLayout(new FlowLayout());    //Default layout
        JScrollPane scroll = new JScrollPane (textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        JMenuBar menubar = new JMenuBar();
        setJMenuBar(menubar);
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(scroll);
        Format = new JMenu("Format");
        Word = new JMenuItem("Word wrap");
        Format.add(Word);
        menubar.add(Format);
        event1 e1 = new event1();
        Word.addActionListener(e1);
    }

    public class event1 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            textArea.setLineWrap(true);
            textArea.setWrapStyleWord(true);
        }
    }

    public static void main(String[] args) {
        Test3 t = new Test3();
        t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.setTitle("Notepad");
        t.setVisible(true);
        t.setSize(1280, 786);

    }
}