Sou bemm novato em Java e estou tentando montar uma calculadora, a minha intenção é digitar um numero na caixa X_ outro número na caixa Y_ e outro numero em porcentagem na caixa Z_ , porem, o calculo deve ser da seguinte forma:
( X_ + Y_ ) + Z_ / 100 = RESULTADO_
Vou substituir as letras por numero para ficar mais claro:
X = 10
Y = 5
Z = 60%
(10 + 5 ) + 60%
Ou seja, deve resolver o que está dentro do parêntese primeiro e com o resultado do parêntese somar com a porcentagem:
(10 + 5 ) + 60% =
15 + 60% =
24
Esse é o código que tentei escrever, mas não está dando nada certo:
import javafx.scene.input.KeyCode;
import static javafx.scene.input.KeyCode.X;
/*
- 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.
*/
public class Calculadora extends javax.swing.JFrame {
double n1, n2 , n3 , n4 ,total, totalok;
/**
* Creates new form Calculadora
*/
public Calculadora() {
initComponents();
}
/**
* 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() {
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
jMenu3 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenu4 = new javax.swing.JMenu();
jMenu5 = new javax.swing.JMenu();
jLabel1 = new javax.swing.JLabel();
jMenu6 = new javax.swing.JMenu();
jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenu7 = new javax.swing.JMenu();
jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();
jPanel1 = new javax.swing.JPanel();
jTabbedPane1 = new javax.swing.JTabbedPane();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
x_ = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
Y_ = new javax.swing.JTextField();
Z_ = new javax.swing.JTextField();
RESULTADO_ = new javax.swing.JTextField();
LIMPAR_ = new javax.swing.JButton();
CALCULAR_ = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu8 = new javax.swing.JMenu();
jMenu1.setText("jMenu1");
jMenu2.setText("jMenu2");
jMenu3.setText("jMenu3");
jMenuItem1.setText("jMenuItem1");
jMenu4.setText("jMenu4");
jMenu5.setText("jMenu5");
jLabel1.setText("CALCULADORA ML");
jMenu6.setText("jMenu6");
jCheckBoxMenuItem1.setSelected(true);
jCheckBoxMenuItem1.setText("jCheckBoxMenuItem1");
jMenuItem2.setText("jMenuItem2");
jMenu7.setText("jMenu7");
jRadioButtonMenuItem1.setSelected(true);
jRadioButtonMenuItem1.setText("jRadioButtonMenuItem1");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel2.setText("CUSTO");
jLabel3.setText("FIXO ML");
jLabel4.setText("MARGEM");
x_.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
x_ActionPerformed(evt);
}
});
jLabel5.setText("RESULTADO");
Y_.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Y_ActionPerformed(evt);
}
});
Z_.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Z_ActionPerformed(evt);
}
});
RESULTADO_.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RESULTADO_ActionPerformed(evt);
}
});
LIMPAR_.setText("LIMPAR");
LIMPAR_.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
LIMPAR_ActionPerformed(evt);
}
});
CALCULAR_.setText("CALCULAR");
CALCULAR_.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CALCULAR_ActionPerformed(evt);
}
});
jMenu8.setText("File");
jMenuBar1.add(jMenu8);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(LIMPAR_)
.addGap(88, 88, 88))
.addGroup(layout.createSequentialGroup()
.addGap(59, 59, 59)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(RESULTADO_, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(x_, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Y_, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Z_, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(CALCULAR_, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(53, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(x_, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(Y_, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(Z_, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(RESULTADO_, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(27, 27, 27)
.addComponent(CALCULAR_, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(LIMPAR_)
.addGap(7, 7, 7))
);
pack();
}// </editor-fold>
private void x_ActionPerformed(java.awt.event.ActionEvent evt) {
n1 = Double.parseDouble(x_.getText());
//CUSTO DO PRODUTO
}
private void Y_ActionPerformed(java.awt.event.ActionEvent evt) {
// FIXO MERCADO LIVRE
n2 = Double.parseDouble(Y_.getText());
}
private void Z_ActionPerformed(java.awt.event.ActionEvent evt) {
// MARGEM DE LUCRO
n3 = Double.parseDouble(Z_.getText());
}
private void RESULTADO_ActionPerformed(java.awt.event.ActionEvent evt) {
// RESULTADO
/* n1 = Double.parseDouble(x.getText());
n2 = Double.parseDouble(Y.getText());
total = n2 + n1;
RESULTADO_.setText(String.valueOf(total));*/
}
private void LIMPAR_ActionPerformed(java.awt.event.ActionEvent evt) {
x_.setText("");
Y_.setText("");
Z_.setText("");
RESULTADO_.setText("");

}
private void CALCULAR_ActionPerformed(java.awt.event.ActionEvent evt) {
n1 = Double.parseDouble(x_.getText());
n2 = Double.parseDouble(Y_.getText());
n3 = Double.parseDouble(Z_.getText());
total = ( (n1 + n2 )+ n3 )/100 ;
RESULTADO_.setText(String.valueOf(total));
}
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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Calculadora.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new Calculadora().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JButton CALCULAR_;
private javax.swing.JButton LIMPAR_;
private javax.swing.JTextField RESULTADO_;
private javax.swing.JTextField Y_;
private javax.swing.JTextField Z_;
private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenu jMenu5;
private javax.swing.JMenu jMenu6;
private javax.swing.JMenu jMenu7;
private javax.swing.JMenu jMenu8;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JPanel jPanel1;
private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTextField x_;
// End of variables declaration
}
Deve ser muito simples fazer para quem sabe, mas serio não estou conseguindo.
