Pagina 2 di 2 primaprima 1 2
Visualizzazione dei risultati da 11 a 20 su 20
  1. #11
    Se ti puo' interessare qui ho del codice molto semplice,
    ti basta mettere l'handle per ogni JButton e aggiungere
    gli ActionListener per le funzionalita' di calcolo.
    Qui ti fornisco solo la GUI molto semplice.

    codice:
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.GridLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    /**
     *
     * @author mau
     */
    public class Calculator extends JFrame {
        private JTextField inputField;    
        JPanel gridPanel;
        
        
        
        public Calculator() {
            super("Calculator");
            
            inputField = new JTextField();    
            gridPanel = new JPanel();
            gridPanel.setLayout(new GridLayout(4, 4, 3, 3));
            
            gridPanel.add(new JButton("7"));
            gridPanel.add(new JButton("8"));
            gridPanel.add(new JButton("9"));
            gridPanel.add(new JButton("/"));
            gridPanel.add(new JButton("4"));
            gridPanel.add(new JButton("5"));
            gridPanel.add(new JButton("6"));
            gridPanel.add(new JButton("*"));
            gridPanel.add(new JButton("1"));
            gridPanel.add(new JButton("2"));
            gridPanel.add(new JButton("3"));
            gridPanel.add(new JButton("-"));
            gridPanel.add(new JButton("0"));
            gridPanel.add(new JButton("."));
            gridPanel.add(new JButton("="));
            gridPanel.add(new JButton("+"));
            
            Container content = getContentPane();
            
            content.add(inputField, BorderLayout.NORTH);
            content.add(gridPanel, BorderLayout.CENTER);
            
            pack();
            setVisible(true);
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            Calculator app = new Calculator();
            app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        
    }
    Nulla, ma e' sempre qualcosa.

  2. #12
    ma questa è la struttura :>
    l'ho già fatta la struttura eccola:


  3. #13
    Bhe io ci ho provato
    Ciao.
    Nulla, ma e' sempre qualcosa.

  4. #14
    Puoi creare una classe interna che implementa
    ActionListener, nel metodo ActionPerformed viene passato un oggetto ActionEvent che con il metodo getSource() ti fornisce l'handle del componente che ha generato l'evento.
    E se ti va ti fornisco il codice.
    Nulla, ma e' sempre qualcosa.

  5. #15
    Puoi implementare anche KeyListener per fornire funzionalita' con la tastiera anziche' col mouse.
    Nulla, ma e' sempre qualcosa.

  6. #16
    action listener è implementato sia per i numeri sia per gli operatori, vorrei sapere come implementare keylistener :>

  7. #17
    Ti fornisco del codice molto semplice, vedi se ti e' utile.

    codice:
    public class KeyDemo extends JFrame implements KeyListener {
        private String line1 = "", line2 = "", line3 = "";
        private JTextArea textArea;
    
        
        /** Creates a new instance of KeyDemo */
        public KeyDemo() {
            super("Demostrating Keystroke Event");
            //imposta una JTextArea
            textArea = new JTextArea(10, 15);
            textArea.setText("Press any on the keyboard...");
            textArea.setEnabled(false);
            textArea.setDisabledTextColor(Color.GRAY);
            getContentPane().add(textArea);
            
            addKeyListener(this);
            
            Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
            setBounds(center.x - 350/2, center.y -100/2, 350, 100);
            setVisible(true);
            
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            KeyDemo appliction = new KeyDemo();
            appliction.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        
        public void keyTyped(java.awt.event.KeyEvent e) {
            
            line1 = "Key typed: " + e.getKeyText(e.getKeyCode());
            setLine2and3(e);
        }
    
        public void keyReleased(java.awt.event.KeyEvent e) {
            
            line1 = "key released: " + e.getKeyText(e.getKeyCode());
            setLine2and3(e);
        }
    
        public void keyPressed(java.awt.event.KeyEvent e) {
            if(e.getKeyCode() == 87)
                System.out.println("Premuto W");
           
            line1 = "key pressed: " + e.getKeyText(e.getKeyCode());
            setLine2and3(e);
            
        }
    
        private void setLine2and3 (KeyEvent e) {
            System.out.println(e.getKeyCode());
            line2 = "This key is " + (e.isActionKey() ? "" : "not ") + "an action key";
            String temp = e.getKeyModifiersText(e.getModifiers());
            line3 = "Modifier keys pressed: " + (temp.equals("") ? "none " : temp);
            textArea.setText(line1 + "\n" + line2 + "\n" + line3 + "\n");
        }
    
       
        
    }
    Nulla, ma e' sempre qualcosa.

  8. #18
    mi da 13 errori in compilazione :O

  9. #19
    E ti credo io se non importi i package!!
    Scusa ogni volta sono convinto che fosse esplicito
    importare i package utilizzando i tool automatici...
    codice:
    import java.awt.Color;
    import java.awt.GraphicsEnvironment;
    import java.awt.Point;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    Nulla, ma e' sempre qualcosa.

  10. #20
    che idiota VVoVe:
    grazie 1000!

    poi me lo studio
    ora esco!
    ciaoooooooooo

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 © 2026 vBulletin Solutions, Inc. All rights reserved.