[英]Word count for a scrollable JTextArea (scrollPane) word count
I have been trying to make a program where data inputted into a JTextArea, would then be counted and then displayed on a JLabel the number of words, after a button is clicked. 我一直在尝试制作一个程序,在该程序中,单击按钮后,将对输入到JTextArea中的数据进行计数,然后在JLabel上显示单词数。
However the code I have tried to use, just shows the value of 1 everytime. 但是我尝试使用的代码每次都只显示1的值。
Any idea why? 知道为什么吗? ` import java.awt.Dimension; `import java.awt.Dimension; import java.awt.GridBagConstraints; 导入java.awt.GridBagConstraints; import java.awt.GridBagLayout; 导入java.awt.GridBagLayout; import java.awt.event.ActionEvent; 导入java.awt.event.ActionEvent; import java.awt.event.ActionListener; 导入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);
}
}
Just to put it in context, my JTextArea is on a seperate panel / class to the panel containing the button and JLabel. 只是将其放在上下文中,我的JTextArea位于包含按钮和JLabel的面板的单独面板/类中。
Every time I run the program and click the button, after inputting some words the value is always one even if the text box is empty. 每次我运行该程序并单击该按钮时,在输入一些单词后,即使文本框为空,该值始终为1。
Well, if you create a new TextAPanel
and assign it to the textPanel
variable - then when you try to get input you get nothing, because the newly created panel doesn't contain anything. 好吧,如果您创建一个新的TextAPanel
并将其分配给textPanel
变量-那么当您尝试获取输入时,您将一无所获,因为新创建的面板不包含任何内容。 If textPanel
is a field in your class, you should just remove the textPanel = new TextAPanel();
如果textPanel
是您类中的一个字段,则应该删除textPanel = new TextAPanel();
line and it should work fine. 行,它应该工作正常。 If you get a NullPointerException, it means you forgot to initialize it earlier in the code, you should probably do it in the constructor. 如果收到NullPointerException,则意味着您忘记了在代码中更早地对其进行初始化,您可能应该在构造函数中进行初始化。
EDIT: OK, I made it work, the code is below. 编辑:确定,我使它工作,下面的代码。 I have no idea how your program compiled, because in your TextAPanel class you had TextAreaPanel()
method/constructor, which caused compilation error in my Eclipse. 我不知道您的程序如何编译,因为在您的TextAPanel类中,您有TextAreaPanel()
方法/构造函数,这在Eclipse中导致了编译错误。 Anyway, I got this working, you'll need to complete it, but this should get you started: 无论如何,我已经完成了这项工作,您需要完成此工作,但这应该可以帮助您开始:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.