0% found this document useful (0 votes)
16 views11 pages

Calculadora Miniteste5 Samito

The document is a Java class for a basic calculator GUI application named 'tela_principal'. It includes buttons for digits, operations, and functionalities to perform arithmetic calculations. The class initializes the GUI components and handles button actions to update the display and perform calculations based on user input.

Uploaded by

Samito Warrota
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views11 pages

Calculadora Miniteste5 Samito

The document is a Java class for a basic calculator GUI application named 'tela_principal'. It includes buttons for digits, operations, and functionalities to perform arithmetic calculations. The class initializes the GUI components and handles button actions to update the display and perform calculations based on user input.

Uploaded by

Samito Warrota
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to
change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this
template
*/

/**
*
* @author pc
*/
public class tela_principal extends javax.swing.JFrame {

/**
* Creates new form tela_principal
*/
public tela_principal() {
initComponents();
}
double numero1, numero2;
String operacao;
/**
* 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">

private void initComponents() {

jPanel1 = new javax.swing.JPanel();


virgula = new javax.swing.JButton();
igual = new javax.swing.JButton();
bt0 = new javax.swing.JButton();
bt4 = new javax.swing.JButton();
bt2 = new javax.swing.JButton();
bt3 = new javax.swing.JButton();
bt5 = new javax.swing.JButton();
bt6 = new javax.swing.JButton();
bt7 = new javax.swing.JButton();
bt1 = new javax.swing.JButton();
bt9 = new javax.swing.JButton();
soma = new javax.swing.JButton();
subtracao = new javax.swing.JButton();
on = new javax.swing.JButton();
clear = new javax.swing.JButton();
divisao = new javax.swing.JButton();
multiplicacao = new javax.swing.JButton();
bt8 = new javax.swing.JButton();
jTresultado = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setForeground(new java.awt.Color(204, 255, 255));


jPanel1.setOpaque(false);

virgula.setText(",");
virgula.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
virgulaActionPerformed(evt);
}
});

igual.setText("=");
igual.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
igualActionPerformed(evt);
}
});

bt0.setText("0");
bt0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt0ActionPerformed(evt);
}
});

bt4.setText("4");
bt4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt4ActionPerformed(evt);
}
});

bt2.setText("2");
bt2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt2ActionPerformed(evt);
}
});

bt3.setText("3");
bt3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt3ActionPerformed(evt);
}
});

bt5.setText("5");
bt5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt5ActionPerformed(evt);
}
});

bt6.setText("6");
bt6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt6ActionPerformed(evt);
}
});

bt7.setText("7");
bt7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt7ActionPerformed(evt);
}
});

bt1.setText("1");
bt1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt1ActionPerformed(evt);
}
});

bt9.setText("9");
bt9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt9ActionPerformed(evt);
}
});

soma.setText("+");
soma.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
somaActionPerformed(evt);
}
});

subtracao.setText("-");
subtracao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
subtracaoActionPerformed(evt);
}
});

on.setText("ON");
on.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
onActionPerformed(evt);
}
});

clear.setText("C");
clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearActionPerformed(evt);
}
});

divisao.setText("/");
divisao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
divisaoActionPerformed(evt);
}
});

multiplicacao.setText("x");
multiplicacao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
multiplicacaoActionPerformed(evt);
}
});

bt8.setText("8");
bt8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt8ActionPerformed(evt);
}
});

jTresultado.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

javax.swing.GroupLayout jPanel1Layout = new


javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(17, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout
.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.Gro
upLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(bt0,
javax.swing.GroupLayout.PREFERRED_SIZE, 118,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentP
lacement.RELATED)
.addComponent(virgula,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(bt1,
javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentP
lacement.RELATED)
.addComponent(bt2,
javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentP
lacement.RELATED)
.addComponent(bt3,
javax.swing.GroupLayout.PREFERRED_SIZE, 56,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(bt4,
javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentP
lacement.RELATED)
.addComponent(bt5,
javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentP
lacement.RELATED)
.addComponent(bt6,
javax.swing.GroupLayout.PREFERRED_SIZE, 56,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(bt7,
javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentP
lacement.RELATED)
.addComponent(bt8,
javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentP
lacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(bt9,
javax.swing.GroupLayout.PREFERRED_SIZE, 56,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement
.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.Gro
upLayout.Alignment.TRAILING, false)
.addComponent(subtracao,
javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE,
56, Short.MAX_VALUE)
.addComponent(soma,
javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(igual,
javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(on, javax.swing.GroupLayout.PREFERRED_SIZE,
56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement
.RELATED)
.addComponent(clear,
javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement
.RELATED)
.addComponent(divisao,
javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement
.RELATED)
.addComponent(multiplicacao,
javax.swing.GroupLayout.PREFERRED_SIZE, 56,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jTresultado))
.addGap(24, 24, 24))
);
jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(14, Short.MAX_VALUE)
.addComponent(jTresultado, javax.swing.GroupLayout.PREFERRED_SIZE,
52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout
.Alignment.BASELINE)
.addComponent(on, javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE,
42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(divisao, javax.swing.GroupLayout.PREFERRED_SIZE,
42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(multiplicacao,
javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED
)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout
.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bt7, javax.swing.GroupLayout.PREFERRED_SIZE,
42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bt9, javax.swing.GroupLayout.PREFERRED_SIZE,
42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bt8, javax.swing.GroupLayout.PREFERRED_SIZE,
42, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(subtracao,
javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE,
42, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED
)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout
.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addComponent(bt4, javax.swing.GroupLayout.PREFERRED_SIZE,
42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement
.RELATED))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLa
yout.Alignment.TRAILING)
.addComponent(soma, javax.swing.GroupLayout.PREFERRED_SIZE,
39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.Gro
upLayout.Alignment.BASELINE)
.addComponent(bt5,
javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bt6,
javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout
.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.Gro
upLayout.Alignment.BASELINE)
.addComponent(bt2,
javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bt3,
javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bt1,
javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement
.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.Gro
upLayout.Alignment.BASELINE)
.addComponent(virgula,
javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bt0,
javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(igual, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(43, 43, 43)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 272,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(66, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void bt0ActionPerformed(java.awt.event.ActionEvent evt) {

// BOTAO ZERO
jTresultado.setText(jTresultado.getText()+"0");
}

private void igualActionPerformed(java.awt.event.ActionEvent evt) {

// IGUALIDADE
numero2=Double.parseDouble(jTresultado.getText());

switch (operacao)
{
case "soma":
jTresultado.setText(String.valueOf(caro(numero1 + numero2)));
break;
case "subtracao":
jTresultado.setText(String.valueOf(caro(numero1 - numero2)));
break;
case "multiplicacao":
jTresultado.setText(String.valueOf(caro(numero1 * numero2)));
break;
case "divisao":if(numero2==0){jTresultado.setText("INVALIDO");}
else{
jTresultado.setText(String.valueOf(caro(numero1 / numero2)));}
break;
}
}

private void virgulaActionPerformed(java.awt.event.ActionEvent evt) {

// virgula
if(!(jTresultado.getText().contains(","))){
jTresultado.setText(jTresultado.getText()+",");
}
}

private void bt4ActionPerformed(java.awt.event.ActionEvent evt) {

// BOTAO QUATRO
jTresultado.setText(jTresultado.getText()+"4");
}

private void bt2ActionPerformed(java.awt.event.ActionEvent evt) {

// BOTAO DOIS
jTresultado.setText(jTresultado.getText()+"2");
}

private void bt3ActionPerformed(java.awt.event.ActionEvent evt) {

// BOTAO TRES
jTresultado.setText(jTresultado.getText()+"3");
}

private void bt5ActionPerformed(java.awt.event.ActionEvent evt) {

// BOTAO 5
jTresultado.setText(jTresultado.getText()+"5");
}

private void bt6ActionPerformed(java.awt.event.ActionEvent evt) {

// BOTAO SEIS
jTresultado.setText(jTresultado.getText()+"6");
}

private void bt7ActionPerformed(java.awt.event.ActionEvent evt) {

// BOTAO SETE
jTresultado.setText(jTresultado.getText()+"7");
}

private void bt1ActionPerformed(java.awt.event.ActionEvent evt) {

// BOTAO UM
jTresultado.setText(jTresultado.getText()+"1");
}

private void bt9ActionPerformed(java.awt.event.ActionEvent evt) {

// BOTAO OITO
jTresultado.setText(jTresultado.getText()+"9");
}

private void somaActionPerformed(java.awt.event.ActionEvent evt) {

// SOMA
numero1=Double.parseDouble(jTresultado.getText());
jTresultado.setText("");
operacao="soma";
}

private void subtracaoActionPerformed(java.awt.event.ActionEvent evt) {

// SUBTRACAO
numero1=Double.parseDouble(jTresultado.getText());
jTresultado.setText("");
operacao="subtracao";
}

private void onActionPerformed(java.awt.event.ActionEvent evt) {

// ON
jTresultado.setText("");
}

private void clearActionPerformed(java.awt.event.ActionEvent evt) {

// LIMPAR
jTresultado.setText("");
}

private void divisaoActionPerformed(java.awt.event.ActionEvent evt) {

// DIVISAO
numero1=Double.parseDouble(jTresultado.getText());
jTresultado.setText("");
operacao="divisao";
}

private void multiplicacaoActionPerformed(java.awt.event.ActionEvent evt) {

// MULTIPLICACAO
numero1=Double.parseDouble(jTresultado.getText());
jTresultado.setText("");
jTresultado.setText("");

operacao="multiplicacao";
}

private void bt8ActionPerformed(java.awt.event.ActionEvent evt) {

// BOTAO OITO
jTresultado.setText(jTresultado.getText()+"8");
}

String caro(double jTresultado){


String retorno="";

retorno = Double.toString(jTresultado);
if(jTresultado%1==0){
retorno=retorno.substring(0, retorno.length()-2);
}
return retorno;
}

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(tela_principal.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(tela_principal.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(tela_principal.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(tela_principal.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new tela_principal().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton bt0;
private javax.swing.JButton bt1;
private javax.swing.JButton bt2;
private javax.swing.JButton bt3;
private javax.swing.JButton bt4;
private javax.swing.JButton bt5;
private javax.swing.JButton bt6;
private javax.swing.JButton bt7;
private javax.swing.JButton bt8;
private javax.swing.JButton bt9;
private javax.swing.JButton clear;
private javax.swing.JButton divisao;
private javax.swing.JButton igual;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTresultado;
private javax.swing.JButton multiplicacao;
private javax.swing.JButton on;
private javax.swing.JButton soma;
private javax.swing.JButton subtracao;
private javax.swing.JButton virgula;
// End of variables declaration
}

You might also like