我最難找到如何編寫JTextField的輸入中有多少單詞,我有一組清晰的輸入按鈕,以及一次我弄清楚如何找出有多少字,我也可以清楚。謝謝你們這裏是我的代碼!查找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("");
}
}
您的問題基本可以更好地總結爲, 「如何計算字符串中的字數」,我建議首先對該主題進行一些研究,因爲其餘代碼與解決該問題無關 – MadProgrammer
您應該查看正則表達式。他們對做你正在做的事非常有用。 – luckydog32