Docsity
Docsity

Prepara i tuoi esami
Prepara i tuoi esami

Studia grazie alle numerose risorse presenti su Docsity


Ottieni i punti per scaricare
Ottieni i punti per scaricare

Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium


Guide e consigli
Guide e consigli


Calcolatrice (codice in java) con componente grafico, Esercizi di Programmazione Java

Calcolatrice (codice in java) con componente grafico, java

Tipologia: Esercizi

2022/2023

In vendita dal 26/09/2023

carla-boscolo
carla-boscolo 🇮🇹

4.5

(13)

520 documenti

1 / 37

Toggle sidebar

Questa pagina non è visibile nell’anteprima

Non perderti parti importanti!

bg1
Calcolatrice
Calcolatrice.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package calcolatrice;
import static calcolatrice.NewJFrame.jLabelErrore;
/**
*
* @author
*/
public class Calcolatrice {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
NewJFrame calcoli = new NewJFrame();
calcoli.setVisible(true);
}
}
NewJFrame.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package calcolatrice;
import javax.swing.JTextField;
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Anteprima parziale del testo

Scarica Calcolatrice (codice in java) con componente grafico e più Esercizi in PDF di Programmazione Java solo su Docsity!

Calcolatrice

Calcolatrice.java

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor. / package calcolatrice; import static calcolatrice.NewJFrame.jLabelErrore; /*
  • @author / public class Calcolatrice { /*
  • @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here NewJFrame calcoli = new NewJFrame(); calcoli.setVisible(true); } }

NewJFrame.java

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor. */ package calcolatrice; import javax.swing.JTextField;
  • @author / public class NewJFrame extends javax.swing.JFrame { Valori v = new Valori(); public int contatoreOP = 0; /*
  • Creates new form NewJFrame / public NewJFrame() { initComponents(); JTextField jTextField11 = jTextField1; this.jLabelErrore.setVisible(false); } /*
  • This method is called from within the constructor to initialize the form.
  • WARNING: Do NOT modify this code. The content of this method is always
  • regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); jButton11 = new javax.swing.JButton(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jButton8 = new javax.swing.JButton(); jButton5 = new javax.swing.JButton(); jButton6 = new javax.swing.JButton(); jButton7 = new javax.swing.JButton(); jButton9 = new javax.swing.JButton(); jButton0 = new javax.swing.JButton(); jButtonSomma = new javax.swing.JButton(); jButtonUguale = new javax.swing.JButton(); jButtonMoltiplicazione = new javax.swing.JButton(); jButtonDivisione = new javax.swing.JButton(); jButtonSottrazione = new javax.swing.JButton(); jButtonVirgola = new javax.swing.JButton(); jButtonCancella = new javax.swing.JButton();

jButton8.setText("8"); jButton8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton8ActionPerformed(evt); } }); jButton5.setText("5"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); jButton6.setText("6"); jButton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton6ActionPerformed(evt); } }); jButton7.setText("7"); jButton7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton7ActionPerformed(evt); } }); jButton9.setText("9"); jButton9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton9ActionPerformed(evt); } }); jButton0.setText("0"); jButton0.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton0ActionPerformed(evt); } }); jButtonSomma.setText("+"); jButtonSomma.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonSommaActionPerformed(evt);

jButtonUguale.setText("="); jButtonUguale.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonUgualeActionPerformed(evt); } }); jButtonMoltiplicazione.setText("*"); jButtonMoltiplicazione.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonMoltiplicazioneActionPerformed(evt); } }); jButtonDivisione.setText("/"); jButtonDivisione.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonDivisioneActionPerformed(evt); } }); jButtonSottrazione.setText("-"); jButtonSottrazione.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonSottrazioneActionPerformed(evt); } }); jButtonVirgola.setText(","); jButtonVirgola.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonVirgolaActionPerformed(evt); } }); jButtonCancella.setText("C"); jButtonCancella.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonCancellaActionPerformed(evt); } }); jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N jLabel2.setText("Calcolatrice");

jButtonElevamentoN.setText("X^N"); jButtonElevamentoN.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonElevamentoNActionPerformed(evt); } }); jButtonReciproco.setText("1/N"); jButtonReciproco.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonReciprocoActionPerformed(evt); } }); jButtonFattoriale.setText("Fattoriale"); jButtonFattoriale.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonFattorialeActionPerformed(evt); } }); jButtonMem.setText("MEM"); jButtonMem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonMemActionPerformed(evt); } }); jButtonM.setText("M+"); jButtonM.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonMActionPerformed(evt); } }); jButtonSto.setText("STO"); jButtonSto.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonStoActionPerformed(evt); } }); jLabelErrore.setForeground(new java.awt.Color(255, 51, 51)); jLabelErrore.setText("Errore di sintassi"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jTextField1) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton3) .addComponent(jButton6)) .addGap(11, 11, 11) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jButtonMoltiplicazione, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonMem, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButtonUguale, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(2, 12, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jButton7) .addGap(18, 18, 18) .addComponent(jButton8) .addGap(18, 18, 18) .addComponent(jButton9) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButtonDivisione) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonM, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(jButton0, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jButtonVirgola, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonSomma) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonCancella, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(0, 0, Short.MAX_VALUE)))) .addGroup(layout.createSequentialGroup() .addGap(43, 43, 43) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(73, 73, 73) .addComponent(jLabelErrore, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))

.addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(19, 19, 19) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(jLabelErrore)) .addGap(18, 18, 18) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(38, 38, 38) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton7) .addComponent(jButton8) .addComponent(jButton9) .addComponent(jButtonDivisione) .addComponent(jButtonM)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton4) .addComponent(jButton5) .addComponent(jButtonMoltiplicazione) .addComponent(jButtonMem)) .addComponent(jButton6)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButtonSottrazione) .addComponent(jButton3) .addComponent(jButton2) .addComponent(jButton1) .addComponent(jButtonSto)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButtonVirgola) .addComponent(jButtonSomma) .addComponent(jButton0) .addComponent(jButtonCancella)) .addGap(27, 27, 27)

}//GEN-LAST:event_jButton2ActionPerformed private void jButtonSommaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSommaActionPerformed boolean isErrore; contatoreOP++; jTextField1.setText(v.SchermoOp("")); isErrore=v.Errori(contatoreOP); if(isErrore){ jLabelErrore.setVisible(true); contatoreOP--; }else{ v.Somma(jTextField1.getText()); } }//GEN-LAST:event_jButtonSommaActionPerformed private void jButtonUgualeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUgualeActionPerformed jTextField1.setText(v.Risultato(jTextField1.getText())); }//GEN-LAST:event_jButtonUgualeActionPerformed private void jButtonMoltiplicazioneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMoltiplicazioneActionPerformed boolean isErrore; contatoreOP++; jTextField1.setText(v.SchermoOp("")); isErrore=v.Errori(contatoreOP); if(isErrore){ jLabelErrore.setVisible(true); contatoreOP--; }else{ v.Moltiplicazione(jTextField1.getText()); } }//GEN-LAST:event_jButtonMoltiplicazioneActionPerformed private void jButtonDivisioneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDivisioneActionPerformed boolean isErrore;

contatoreOP++; jTextField1.setText(v.SchermoOp("")); isErrore=v.Errori(contatoreOP); if(isErrore){ jLabelErrore.setVisible(true); contatoreOP--; }else{ v.Divisione(jTextField1.getText()); } }//GEN-LAST:event_jButtonDivisioneActionPerformed private void jButtonSottrazioneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSottrazioneActionPerformed boolean isErrore; contatoreOP++; jTextField1.setText(v.SchermoOp("")); isErrore=v.Errori(contatoreOP); if(isErrore){ jLabelErrore.setVisible(true); contatoreOP--; }else{ v.Sottrazione(jTextField1.getText()); } }//GEN-LAST:event_jButtonSottrazioneActionPerformed private void jButtonVirgolaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVirgolaActionPerformed jTextField1.setText(v.Schermo(".")); }//GEN-LAST:event_jButtonVirgolaActionPerformed private void jButtonCancellaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancellaActionPerformed v.Cancella(); jTextField1.setText(""); contatoreOP--; }//GEN-LAST:event_jButtonCancellaActionPerformed private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed }//GEN-LAST:event_jTextField1ActionPerformed

v.TAN(jTextField1.getText()); }//GEN-LAST:event_jButtonTANActionPerformed private void jButtonRadiceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRadiceActionPerformed v.RADICE(jTextField1.getText()); }//GEN-LAST:event_jButtonRadiceActionPerformed private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed v.RADennesima(jTextField1.getText()); }//GEN-LAST:event_jButton10ActionPerformed private void jButtonElevamentoNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonElevamentoNActionPerformed v.elevaN(jTextField1.getText()); }//GEN-LAST:event_jButtonElevamentoNActionPerformed private void jButtonReciprocoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonReciprocoActionPerformed v.Reciproco(jTextField1.getText()); }//GEN-LAST:event_jButtonReciprocoActionPerformed private void jButtonFattorialeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFattorialeActionPerformed v.Fattoriale(jTextField1.getText()); }//GEN-LAST:event_jButtonFattorialeActionPerformed private void jButtonMemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMemActionPerformed v.Mem(jTextField1.getText()); //mette un valore in memoria }//GEN-LAST:event_jButtonMemActionPerformed private void jButtonMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMActionPerformed v.M(jTextField1.getText()); //somma al valore in memoria un numero }//GEN-LAST:event_jButtonMActionPerformed private void jButtonStoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStoActionPerformed jTextField1.setText(v.Sto()); //stampa valore in memoria }//GEN-LAST:event_jButtonStoActionPerformed /**

  • @param args the command line arguments

public static void main(String args[]) { /* Set the Nimbus look and feel / //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> / If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

  • For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html / try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level. SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level. SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level. SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level. SEVERE, null, ex); } //</editor-fold> / Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton0; private javax.swing.JButton jButton1; private javax.swing.JButton jButton10;