Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. class Exp1 extends JFrame{
  5. JLabel tx1;
  6. JTextField anotimp;
  7. public Exp1(String titlu){
  8. super(titlu);
  9. setSize(400,100);
  10. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11. Container x = getContentPane();
  12. x.setLayout(new FlowLayout());
  13. tx1 = new JLabel("");
  14. anotimp = new JTextField(10);
  15. JButton afis = new JButton("Afiseaza");
  16. x.add(anotimp);
  17. x.add(afis);
  18. x.add(tx1);
  19. setVisible(true);
  20. afis.addActionListener(new ActionListener() {
  21. @Override
  22. public void actionPerformed(ActionEvent event) {
  23. if (anotimp.getText().equals("")){
  24. JOptionPane.showMessageDialog(null,"Nu ati introdus niciun nr");
  25. }else{
  26. try{
  27. String sir = String.valueOf(anotimp.getText());
  28. char litera;
  29. int nr_vocale = 0;
  30. int nr_consoane = 0;
  31.  
  32. for (litera = 'A'; litera <= 'Z'; litera++) // se afiseaza alfabetul
  33. switch (litera)
  34. {
  35. case 'A' :
  36. case 'E' :
  37. case 'I' :
  38. case 'O' :
  39. case 'U' : nr_vocale++; // in toate cele 5 cazuri variabila nr_vocale se incrementeaza cu 1
  40. break;
  41. default: nr_consoane++; //daca apare un alt caz diferit de cele 5 de sus, atunci variabila nr_consoane se incrementeaza cu 1
  42. };
  43. tx1.setText("Vocale="+String.valueOf(nr_vocale)" ,Consoane="+String.valueOf(nr_consoane));
  44.  
  45. }catch (Exception e){
  46. JOptionPane.showMessageDialog(null,"Ati introdus caractere neadmesibile!!!");
  47. }
  48. }
  49. }
  50. });
  51. }
  52. } class Executie{
  53. public static void main(String args[ ]){
  54. new Exp1("Patratul unui numar");
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement