Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- class Exp1 extends JFrame{
- JLabel tx1;
- JTextField anotimp;
- public Exp1(String titlu){
- super(titlu);
- setSize(400,100);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Container x = getContentPane();
- x.setLayout(new FlowLayout());
- tx1 = new JLabel("");
- anotimp = new JTextField(10);
- JButton afis = new JButton("Afiseaza");
- x.add(anotimp);
- x.add(afis);
- x.add(tx1);
- setVisible(true);
- afis.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent event) {
- if (anotimp.getText().equals("")){
- JOptionPane.showMessageDialog(null,"Nu ati introdus niciun nr");
- }else{
- try{
- String sir = String.valueOf(anotimp.getText());
- char litera;
- int nr_vocale = 0;
- int nr_consoane = 0;
- for (litera = 'A'; litera <= 'Z'; litera++) // se afiseaza alfabetul
- switch (litera)
- {
- case 'A' :
- case 'E' :
- case 'I' :
- case 'O' :
- case 'U' : nr_vocale++; // in toate cele 5 cazuri variabila nr_vocale se incrementeaza cu 1
- break;
- default: nr_consoane++; //daca apare un alt caz diferit de cele 5 de sus, atunci variabila nr_consoane se incrementeaza cu 1
- };
- tx1.setText("Vocale="+String.valueOf(nr_vocale)" ,Consoane="+String.valueOf(nr_consoane));
- }catch (Exception e){
- JOptionPane.showMessageDialog(null,"Ati introdus caractere neadmesibile!!!");
- }
- }
- }
- });
- }
- } class Executie{
- public static void main(String args[ ]){
- new Exp1("Patratul unui numar");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement