JTextFieldの入力に含まれる単語の数をコーディングする方法を見つけるのに最も苦労しています。明確な入力ボタンを設定しました。単語の数を見つける方法を見つけたら、次のようにします。それもクリアできるでしょう。みんなありがとうここに私のコードがあります!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CopyTextPanel extends JPanel
{
private JTextField input;
private JLabel output, inlabel, outlabel;
private JButton compute, clear;
private JPanel panel;
public CopyTextPanel()
{
inlabel = new JLabel("Input Text: ");
outlabel = new JLabel("Text Statistics Results: ");
input = new JTextField (" ", 25);
output = new JLabel();
compute = new JButton("Compute Statistics");
compute.addActionListener (new ButtonListener());
clear = new JButton("Clear Text");
clear.addActionListener (new ButtonListener());
panel = new JPanel();
output.setPreferredSize (new Dimension(550, 30));
panel.setPreferredSize (new Dimension(620, 100));
panel.setBackground(Color.gray);
panel.add(inlabel);
panel.add(input);
//panel.add(outlabel);
//panel.add(output);
panel.add(compute);
panel.add(clear);
panel.add(outlabel);
panel.add(output);
setPreferredSize (new Dimension(700, 150));
setBackground(Color.cyan);
add(panel);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource()==compute)
{
{
output.setText (input.getText());
}
}
else
input.setText("");
}
}
inputTextにあるような小さなテキストの場合、splitを使用して、文字列が単語に分割された文字列配列を生成し、配列の長さを読み取ることができます。
String test = "um dois tres quatro cinco ";
String [] splitted = test.trim().split("\\p{javaSpaceChar}{1,}");
System.out.println(splitted.length);
//出力5
だから、あなたの入力のために:
String inputText = input.getText();
String [] splitted = inputText.trim().split("\\p{javaSpaceChar}{1,}");
int numberOfWords = splitted.length;
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加