可滚动 JTextArea (scrollPane) 的字数统计

Word count for a scrollable JTextArea (scrollPane) word count

我一直在尝试制作一个程序,其中输入到 JTextArea 的数据将被计算,然后在单击按钮后在 JLabel 上显示字数。

但是我尝试使用的代码每次都只显示 1 的值。

知道为什么吗? ` 导入 java.awt.Dimension; 导入 java.awt.GridBagConstraints; 导入 java.awt.GridBagLayout; 导入 java.awt.event.ActionEvent; 导入 java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class SPanel extends JPanel {

public SPanel(){
    final TextAPanel textPanel = new TextAPanel();

    final JLabel outputLabel = new JLabel();
    JButton click = new JButton ("Click");
    click.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            String word = textPanel.inputBox.getText();

            System.out.println("Test: " +word);

        }

    });

 }
}

  import java.awt.Dimension;
  import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;

 import javax.swing.JLabel;
 import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TextAPanel extends JPanel {
public JTextArea inputBox = new JTextArea(20,10);
public JScrollPane scrollPane = new JScrollPane(inputBox);
TextAreaPanel(){

    JLabel title = new JLabel("Please type in the box below:");

    inputBox.setLineWrap(true);

    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();

    gc.anchor = GridBagConstraints.NORTHWEST;
    gc.gridx = 0;
    gc.gridy = 0;

    add(title,gc);

    gc.gridx = 0;
    gc.gridy = 1;
    add(scrollPane, gc);


}

 }

只是为了把它放在上下文中,我的 JTextArea 在一个单独的面板上/class 到包含按钮和 JLabel 的面板。

每次我 运行 程序并单击按钮时,输入一些单词后,即使文本框为空,该值也始终为 1。

好吧,如果您创建一个新的 TextAPanel 并将其分配给 textPanel 变量 - 那么当您尝试获取输入时,您什么也得不到,因为新创建的面板不包含任何内容.如果 textPanel 是您的 class 中的一个字段,您应该只删除 textPanel = new TextAPanel(); 行,它应该可以正常工作。如果你得到一个 NullPointerException,这意味着你忘记在代码的前面初始化它,你应该在构造函数中进行。

编辑:好的,我成功了,代码如下。我不知道你的程序是如何编译的,因为在你的 TextAPanel class 中你有 TextAreaPanel() method/constructor,这导致我的 Eclipse 中出现编译错误。无论如何,我已经开始工作了,你需要完成它,但这应该让你开始:

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

@SuppressWarnings("serial")
public class SPanel extends JPanel {

    public SPanel() {
        final TextAPanel textPanel = new TextAPanel();
        this.add(textPanel);

        final JLabel outputLabel = new JLabel();
        JButton click = new JButton("Click");
        click.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                String word = textPanel.inputBox.getText();

                System.out.println("Test: " + word);
                System.out.println(word.split("\s+").length);
            }

        });

        this.add(click);

    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.add(new SPanel());
        f.setVisible(true);
    }

}

@SuppressWarnings("serial")
class TextAPanel extends JPanel {
    public JTextArea inputBox = new JTextArea(20, 10);
    public JScrollPane scrollPane = new JScrollPane(inputBox);

    TextAPanel() {

        JLabel title = new JLabel("Please type in the box below:");

        inputBox.setLineWrap(true);

        setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();

        gc.anchor = GridBagConstraints.NORTHWEST;
        gc.gridx = 0;
        gc.gridy = 0;

        add(title, gc);

        gc.gridx = 0;
        gc.gridy = 1;
        add(scrollPane, gc);

    }

}