【问题标题】:Word count for a scrollable JTextArea (scrollPane) word count可滚动 JTextArea (scrollPane) 字数的字数
【发布时间】:2016-03-13 11:42:46
【问题描述】:

我一直在尝试制作一个程序,其中输入到 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 位于包含按钮和 JLabel 的面板的单独面板/类上。

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

【问题讨论】:

  • 你在点击按钮时创建new TextAPanel();。为什么
  • 你的意思是textPanel吗,如果是这样,那是因为文本框在另一个类中并且要访问它,如果有意义的话,我需要创建一个局部变量然后获得访问权。
  • 检查 System.out.println("".split("\\s+").length); 是 1。实际上文本是空的
  • 所以在顶部我有一个实例变量:Private TextPanel TextAPanel()
  • 我没明白你的意思,你能解释一下吗。

标签: java jlabel jtextarea


【解决方案1】:

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

编辑:好的,我成功了,代码如下。我不知道你的程序是如何编译的,因为在你的 TextAPanel 类中你有TextAreaPanel() 方法/构造函数,这导致了我的 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);

    }

}

【讨论】:

  • 我已经删除了它,我确实得到了一个 NullPointerException,但我现在已经正确初始化它,但是字数仍然是 1,即使它是空白的。
  • 好吧,这就是 split() 的工作原理 - 尝试运行 'String input = "";System.out.println(input.split("\\s+").length);'
  • 试过了,还是不行。我以前使用过 split(),所以我知道我没有做错。我认为是转换,我从文本框中获取输入,这是问题所在。
  • “不起作用”是什么意思?我说的是,当你尝试拆分一个空字符串时,你会得到一个大小为 1 的数组。当你的文本框是空的并且你在上面调用 getText() 时,你会得到一个空字符串。当您尝试拆分此字符串时,您会得到一个大小为 1 的数组,这是使用 split() 时的正常行为。我也觉得很奇怪,但这不是错误。如果你真的关心空输入,你可以在尝试拆分之前检查输入是否为空。
  • 对不起,回复晚了,当它是空白的时候它也不能正常工作,所以我认为我在我的其他代码中做错了,如果它应该工作的话。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多