Boa tarde, estou criando um programa no NetBeans que calcula a Equação do 2º grau, através de um form!
Até o momento tudo tranquilo, minha dúvida é a seguinte: Caso o campo que se refere ao valor de A seja igual a 0 apareça um JOptionPane com a mensagem: “Valor de A deve ser diferente de 0!” e o cursor volte para o campo onde será inserido o valor de A!
Outra dúvida, caso os campos dos valores de A, B e C, estejam em branco, apareça um JOptionPane com a mensagem: “Digite os valores de A, B e C!”.
Segue abaixo o código da janela para análise:
double vla,vlb,vlc;
private void bt_calcularMouseClicked(java.awt.event.MouseEvent evt) {
vla = Double.parseDouble(txt_vla.getText());
if(vla != 0){
vlb = Double.parseDouble(txt_vlb.getText());
vlc = Double.parseDouble(txt_vlc.getText());
}else{JOptionPane.showMessageDialog(null,"Valor de A não pode ser 0");}
double delta = (vlb* vlb) - (4*(vla*vlc));
JOptionPane.showMessageDialog(null, "O valor de DELTA é: " + delta);
double x1,x2;
if (delta > 0){
JOptionPane.showMessageDialog(null,"Teremos 2 raizes!");
x1 = (-vlb + (Math.sqrt(delta)))/(2*vla);
x2 = (-vlb - (Math.sqrt(delta)))/(2*vla);
lb_x1.setText("%.2f" + x1);
lb_x2.setText("%.2f" + x2);
}else if (delta == 0){
JOptionPane.showMessageDialog(null,"Teremos 1 raiz!");
x1 = (-vlb + (Math.sqrt(delta)))/(2*vla);
x2 = (-vlb - (Math.sqrt(delta)))/(2*vla);
lb_x1.setText("%.2f" + x1);
lb_x2.setText("%.2f" + x2);
}else{
JOptionPane.showMessageDialog(null, "Não existem raizes");
txt_vla.setText("");
txt_vlb.setText("");
txt_vlc.setText("");
lb_x1.setText("");
lb_x2.setText("");
}
if("".equals(txt_vla) && "".equals(txt_vlb) && "".equals(txt_vla)){
System.out.println("DIGITE OS VALORES DE A, B e C");
} else {
}
}
private void txt_vlaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txt_vlbActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void bt_calcularActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public void limpatudo() {
txt_vla.setText("");
txt_vlb.setText("");
txt_vlc.setText("");
lb_x1.setText("");
lb_x2.setText("");
}
private void bt_limparMouseClicked(java.awt.event.MouseEvent evt) {
limpatudo();
}
private void bt_sairMouseClicked(java.awt.event.MouseEvent evt) {
int selectedOption = JOptionPane.showConfirmDialog(null,"Deseja Sair Realmente?", "Sair",JOptionPane.YES_NO_OPTION);
if (selectedOption == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
/**
* @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(T10_2grau.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(T10_2grau.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(T10_2grau.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(T10_2grau.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 T10_2grau().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bt_calcular;
private javax.swing.JButton bt_limpar;
private javax.swing.JButton bt_sair;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel lb_x1;
private javax.swing.JLabel lb_x2;
private javax.swing.JTextField txt_vla;
private javax.swing.JTextField txt_vlb;
private javax.swing.JTextField txt_vlc;
// End of variables declaration
}
Desde já agradeço a colaboração de todos!
abraço