JTextArea的自动换行在java中找不到

时间:2015-01-01 10:45:21

标签: jtextarea

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

但是当我写作



textArea.setLineWrap(true);




比它给我的错误显示

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

即使我按下'。'和textdown的下拉,但它没有显示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);
    
  }
}




2 个答案:

答案 0 :(得分:0)

而不是使用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);

  }
}

答案 1 :(得分:0)

我已经改变了你的代码。 而不是TextArea我使用了JTextAreaJScrollPane包装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);

    }
}