import java.awt.BorderLayout;
public class CharacterCounter extends JFrame {
private JPanel contentPane;
private JTextField input;
private JTextArea output;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CharacterCounter frame = new CharacterCounter();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CharacterCounter() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 274, 258);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblText = new JLabel("Text");
lblText.setBounds(34, 17, 70, 15);
contentPane.add(lblText);
input = new JTextField();
input.setBounds(77, 12, 164, 25);
contentPane.add(input);
input.setColumns(10);
JButton btnCalculate = new JButton("Calculate");
btnCalculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text = input.getText();
text = text.toLowerCase();
char [] character = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
int [] freq = new int [character.length];
for ( int i = 0; i < text.length(); i++ ){
for ( int j = 0; j < character.length; j++ ){
if ( text.charAt(i) == character [j]) {
freq[j]++;
}
}
}
for ( int i = 0; i < freq.length; i++ ){
if (freq[i] >= 1) {
output.append(character[i]+"="+ freq[i]+ "\n");
}
}
}
}
);
btnCalculate.setBounds(141, 46, 100, 25);
contentPane.add(btnCalculate);
JTextArea output = new JTextArea();
output.setBounds(34, 83, 93, 133);
contentPane.add(output);
}
}