14/08/2018, 11:20
Tạo Word Counter sử dụng Java Swing
Chúng ta có thể phát triển bộ đếm ký tự (Word Counter) với sự giúp đỡ của String, AWT/Swing với Event Handling. Dưới đây là code để phát triển Word Counter: String text="Day la Word Counter tool"; String words[]=text.split("s"); int length=words.length;//tra ve tong so tu int ...
Chúng ta có thể phát triển bộ đếm ký tự (Word Counter) với sự giúp đỡ của String, AWT/Swing với Event Handling. Dưới đây là code để phát triển Word Counter:
String text="Day la Word Counter tool";
String words[]=text.split("s");
int length=words.length;//tra ve tong so tu
int clength=text.length();//tra ve tong so ky tu voi khoang trong space
Dưới đây là swing code để đếm từ và ký tự:
import java.awt.event.*;
import javax.swing.*;
public class WCC extends JFrame implements ActionListener{
JTextArea ta;
JButton b1,b2;
WCC(){
super("Word Character Counter - code24h");
ta=new JTextArea();
ta.setBounds(50,50,300,200);
b1=new JButton("Word");
b1.setBounds(50,300,100,30);
b2=new JButton("Character");
b2.setBounds(180,300,100,30);
b1.addActionListener(this);
b2.addActionListener(this);
add(b1);add(b2);add(ta);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String text=ta.getText();
if(e.getSource()==b1){
String words[]=text.split("s");
JOptionPane.showMessageDialog(this,"Tong so tu: "+words.length);
}
if(e.getSource()==b2){
JOptionPane.showMessageDialog(this,"Tong so ky tu voi khoang trong: "+text.length());
}
}
public static void main(String[] args) {
new WCC();
}
}