Visualizzazione dei risultati da 1 a 10 su 10
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2011
    Messaggi
    40

    problema gestione evento menuitem

    vorrei che quando clicco su about si aprisse una nuova finestra con scritto ciò che ho impostato nel metodo public void finestra()

    codice

    codice:
    import java.awt.event.*; 
    import java.awt.*; 
    import javax.swing.*; 
    import javax.swing.JMenuBar; 
    //import java.swing.Graphics; 
    
    public class prova extends Frame implements ActionListener 
    { 
      JButton pulsante1=new JButton("Converti"); 
      JButton pulsante2=new JButton("+"); 
      JButton pulsante3=new JButton("-"); 
      JButton pulsante4=new JButton("Complemento a 2 +"); 
      JButton pulsante5=new JButton("Complemento a 2 -");
      
      TextField testo1=new TextField(); 
      TextField testo2=new TextField(); 
      TextField testo3=new TextField();  
      TextField testo4=new TextField();
     
      CheckboxGroup cbg = new CheckboxGroup(); 
      JMenuItem ab=new JMenuItem("About"); 
      MenuBar barra=new MenuBar(); 
      Menu file= new Menu("File"); 
      Menu about= new Menu("?");
     
      public prova( ) 
     { 
       setLayout(null); 
       JLabel lab=new JLabel(); 
       JLabel lab1=new JLabel();  
       JLabel lab2=new JLabel(); 
       this.add(lab); 
       this.add(lab1); 
       this.add(lab2); 
    
       //file.add(inte); 
       //file.add(raz); 
       //cbg.add(new Checkbox("one")); 
       //cbg.add(new Checkbox("two")); 
       //cbg.add(new Checkbox("three")); 
    
       about.add(ab); 
       barra.add(file); 
       barra.add(about); 
    
       setMenuBar(barra); 
       this.add(pulsante1); 
       this.add(pulsante2); 
       this.add(pulsante3); 
       this.add(pulsante4); 
       this.add(pulsante5); 
       this.add(testo1); 
       this.add(testo2); 
       this.add(testo3); 
       this.add(testo4); 
    
       pulsante1.setBounds(180,105,90,30); 
       pulsante2.setBounds(50,165,60,30); 
       pulsante3.setBounds(50,225,60,30); 
       pulsante4.setBounds(50,295,150,30); 
       pulsante5.setBounds(250,295,150,30); 
       testo1.setBounds(220,55,60,30); 
       testo2.setBounds(50,105,100,30); 
       testo3.setBounds(200,195,100,30); 
       testo4.setBounds(170,345,100,30); 
    
       lab.setText("Inserisci un numero intero positivo"); 
       lab.setBounds(10,44,205,40); 
       lab1.setText("Notazione modulo-segno"); 
       lab1.setBounds(30,130,205,40); 
       lab2.setText("Complemento alla base"); 
       lab2.setBounds(30,250,205,40); 
       lab.setForeground(Color.black); 
       lab1.setForeground(Color.black);  
       lab2.setForeground(Color.black); 
    
       pulsante1.addActionListener(this); 
       pulsante2.addActionListener(this);  
       pulsante3.addActionListener(this); 
       pulsante4.addActionListener(this); 
       pulsante5.addActionListener(this); 
       testo1.addActionListener(this); 
       testo4.addActionListener(this); 
       testo2.addActionListener(this); 
       testo3.addActionListener(this); 
    
       this.addWindowListener(new MioAscoltaWin()); 
       setTitle("Binary Calculator 1.0 by H"); 
       setBackground(Color.gray); 
       setLocation(200,100); 
       setSize(500,500); 
       setVisible(true); 
     } 
     public void finestra( ) 
     { 
       JLabel testo=new JLabel(); 
       testo.setText("01Converter versione 2.0"); 
       testo.setText(" 2010-2011 Created by H"); 
       testo.setBounds(50,50,205,40); 
     } 
     public static void main(String []s) 
     { 
       prova p=new prova(); 
     } 
     public void actionPerformed(ActionEvent e) 
     { 
        String c=e.getActionCommand(); 
        String f=testo1.getText(); 
        int i; 
        long resto; 
        long aInt=Long.parseLong(f); 
        i=0; 
        if(e.getSource()==pulsante1) 
        { 
           StringBuffer sb = new StringBuffer(); 
           while(aInt>0) 
           { 
              resto=aInt%2;   
              sb.append(resto); 
              aInt=aInt/2; 
           } 
         testo2.setText(sb.reverse().toString()); 
     } 
       if(e.getSource()==pulsante2)  
       { 
         StringBuffer sb = new StringBuffer(); 
         while(aInt>0) 
         { 
            resto=aInt%2; 
            sb.append(resto); 
            aInt=aInt/2; 
         } 
         testo3.setText("0"+sb.reverse().toString()); 
       } 
       if(e.getSource()==pulsante3) 
       { 
          StringBuffer sb = new StringBuffer(); 
          while(aInt>0) 
          { 
            resto=aInt%2; 
            sb.append(resto); 
            aInt=aInt/2; 
          } 
          testo3.setText("1"+sb.reverse().toString()); 
       } 
       if(e.getSource()==pulsante4) 
       { 
          StringBuffer sb = new StringBuffer(); 
          while(aInt>0) 
          { 
            resto=aInt%2; 
            sb.append(resto); 
            aInt=aInt/2; 
          } 
          testo4.setText("0"+sb.reverse().toString()); 
       } 
       if(e.getSource()==pulsante5) 
       { 
          int found; 
          StringBuffer sb = new StringBuffer(); 
          while(aInt>0) 
          { 
            resto=aInt%2; 
            sb.append(resto); 
            aInt=aInt/2;
          } 
          String a=sb.toString(); 
          int length = a.length(); 
          found=0; 
          char vet[]=a.toCharArray(); 
          for(i=0;i<length;i++) 
          { 
             if(vet[i]=='1' && found==0)
             { 
               vet[i]='1'; found=1;
             } 
             else 
             { 
                if(vet[i]=='0' && found==0) 
                         vet[i]='1'; 
                else 
                { 
                    if(vet[i]=='0' && found==1) 
                       vet[i]='1'; 
                    else 
                    { 
                       if(vet[i]=='1' && found==1) vet[i]='0'; 
                    } 
                } 
             }  
        } 
        String g=String.valueOf(vet); 
        StringBuffer sbr=new StringBuffer(g); 
        String fv=new StringBuffer(g).reverse().toString(); 
        testo4.setText("1"+fv); 
        /* if(tasto.....) 
           { 
              float num=numero da textFiled; 
              int n=(int)num; 
              float new=num-n; 
              int i; 
              for(i=0;i<23;i++) 
              { 
                   new=new*2; 
                   if((int)new==0) 
                       metti 0 
                   if((int)new==1) 
                      metti 1 
                   new=new-1; } 
       */ } 
       if(e.getActionCommand().compareTo("About")==0) 
       { 
          finestra(); 
       } 
      }  
    }

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: problema gestione evento menuitem

    Originariamente inviato da hteo
    vorrei che quando clicco su about si aprisse una nuova finestra con scritto ciò che ho impostato nel metodo public void finestra()
    In finestra() vedo solo che istanzi un JLabel e imposti delle proprietà. Se vuoi creare una nuova finestra ... devi crearla (!), aggiungerci dei componenti, rendere visibile la finestra, ecc....

    Poi stai "mixando" componenti AWT e Swing. Non farlo. O usi solo componenti AWT o solo Swing.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2011
    Messaggi
    40
    ho aggiunto delle istruzioni al metodo finestra()
    ora il metodo è così:


    public void finestra()
    {

    JLabel testo=new JLabel();

    JFrame f=new JFrame();

    setLayout(null);
    this.add(testo);

    testo.setText("01Converter versione 2.0");
    testo.setText("2010-2011 Created by H");
    testo.setBounds(50,50,205,40);
    testo.setForeground(Color.black);

    f.setBackground(Color.gray);
    f.setLocation(200,100);
    f.setSize(120,120);
    f.setVisible(true);

    }
    lasciando stare che ci sn componenti AWT e swing come mai quando schiaccio about nn succede nulla????
    sembra che non prenda il fire sull'item

  4. #4
    Utente di HTML.it
    Registrato dal
    Jan 2011
    Messaggi
    40
    ho aggiunto delle istruzioni al metodo finestra()
    ora il metodo è così:


    public void finestra()
    {

    JLabel testo=new JLabel();

    JFrame f=new JFrame();

    setLayout(null);
    this.add(testo);

    testo.setText("01Converter versione 2.0");
    testo.setText("2010-2011 Created by H");
    testo.setBounds(50,50,205,40);
    testo.setForeground(Color.black);

    f.setBackground(Color.gray);
    f.setLocation(200,100);
    f.setSize(120,120);
    f.setVisible(true);

    }
    lasciando stare che ci sn componenti AWT e swing come mai quando schiaccio about nn succede nulla????
    sembra che non prenda il fire sull'item

  5. #5
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da hteo
    come mai quando schiaccio about nn succede nulla????
    Hai registrato un ActionListener sul 'ab' (il JMenuItem di "About") ??

    Dal codice postato inizialmente mi pare di no.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  6. #6
    Utente di HTML.it
    Registrato dal
    Jan 2011
    Messaggi
    40
    ora l'ho registrato..ci sn diversi problemi:
    -la finestra si apre solo se prima converto qualcosa
    -le cose che voglio scrivere nella finestra "about" vengono scritte nella finestra principale

  7. #7
    Utente di HTML.it
    Registrato dal
    Jan 2011
    Messaggi
    40
    ho risolto il problema delle scritte,ora c'è solo quello della finestra che si apre solo dopo la conversione

  8. #8
    Utente di HTML.it
    Registrato dal
    Jan 2011
    Messaggi
    40
    ho risolto il problema delle scritte,ora c'è solo quello della finestra che si apre solo dopo la conversione

  9. #9
    Utente di HTML.it
    Registrato dal
    Jan 2011
    Messaggi
    40
    ho risolto il problema delle scritte,ora c'è solo quello della finestra che si apre solo dopo la conversione

  10. #10
    Utente di HTML.it
    Registrato dal
    Jan 2011
    Messaggi
    40
    non ho ancora risolto,come mai la finestra about si apre solo dopo una conversione?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.