JTextField의 입력에 몇 개의 단어가 있는지 코드하는 방법을 찾는 데 가장 힘든 시간을 보내고 있습니다. 내가 얼마나 많은 단어가 있는지 알아내는 방법을 찾아 냈다. 나는 그 점을 분명히 할 수있을 것이다. 고마워 여기 내 코드가있어!JTextField의 텍스트 입력에 얼마나 많은 단어가 있는지 알아 내기
String test = "um dois tres quatro cinco ";
String [] splitted = test.trim().split("\\p{javaSpaceChar}{1,}");
System.out.println(splitted.length);
// 출력 : 당신이 문자열을 문자열 배열 단어에 침입 그래서 배열의 길이를 읽을 생성 split를 사용할 수있는 InputText]를에 그 같은 텍스트의 작은 조각에 대한
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("");
}
}
기본적인 문제가 더 나은 것으로 요약 할 수있다, "문자열의 단어 수를 계산하는 방법", 그 코드의 나머지 부분은 그 문제를 해결하는 것과 거의 관련이 없으므로 먼저 해당 주제에 대한 연구를 제안하십시오. – MadProgrammer
정규 표현식을 조사해야합니다. 그들은 당신이하고자하는 일을하기에 매우 유용합니다. – luckydog32