import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Ecuacion2do extends JFrame {
private static final long serialVersionUID = 1L;
Double A;
Double B;
Double C;
JLabel lblA,lblB,lblC,lblx1,lblx2,lblvA,lblvB,lblvC,lblvDet;
JTextField txtA,txtB,txtC;
JButton btnAceptar, btnCancelar;
public Ecuacion2do( ) {
setTitle("Solucion");
setSize(250,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
dibujarIntefaz( ); }
public void dibujarIntefaz( ) {
getContentPane( ).setLayout(new GridLayout(7,2));
lblA = new JLabel("Valor de A");
getContentPane( ).add(lblA);
txtA = new JTextField();
getContentPane( ).add(txtA);
lblB = new JLabel("Valor de B");
getContentPane( ).add(lblB);
txtB = new JTextField( );
getContentPane( ).add(txtB);
lblC = new JLabel("Valor de C");
getContentPane( ).add(lblC);
txtC = new JTextField();
getContentPane( ).add(txtC);
lblx1 = new JLabel("x1 = ");
getContentPane( ).add(lblx1);
lblx2 = new JLabel("x2 = ");
getContentPane( ).add(lblx2);
lblvA = new JLabel("vA");
getContentPane( ).add(lblvA);
lblvB = new JLabel("vB");
getContentPane( ).add(lblvB);
lblvC = new JLabel("vC");
getContentPane( ).add(lblvC);
lblvDet = new JLabel("vDet");
getContentPane( ).add(lblvDet);
btnAceptar = new JButton("Resolver");
getContentPane( ).add(btnAceptar);
btnCancelar = new JButton("Cancelar");
getContentPane( ).add(btnCancelar);
btnCancelar.addActionListener(new java.awt.event.ActionListener( ) {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0); } });
btnAceptar.addActionListener(new java.awt.event.ActionListener( ) {
public void actionPerformed(java.awt.event.ActionEvent e) {
String msj=" ";
if(txtA.getText().isEmpty( ) ) {
msj +="Falta el valor de A\n"; }
if(txtB.getText().isEmpty( ) ) {
msj +="Falta el valor de B\n"; }
if(txtC.getText().isEmpty( ) ) {
msj +="Falta el valor de C\n"; }
A = Double.parseDouble(txtA.getText( ) );
B = Double.parseDouble(txtB.getText( ) );
C = Double.parseDouble(txtC.getText( ) );
lblvA.setText("A = " + txtA.getText( ) );
lblvB.setText("B = " +txtB.getText( ) );
lblvC.setText("C = " +txtC.getText( ) );
lblvDet.setText("Det = " + determinante( ) );
if (msj.isEmpty( )) {
A = Double.parseDouble(txtA.getText( ) );
B = Double.parseDouble(txtB.getText( ) );
C = Double.parseDouble(txtC.getText( ) );
Double miSol[ ] = Solucion( );
if (miSol[0] == 100.0) {
lblx1.setText("Sin solucion");
lblx2.setText("Sin solucion"); }
else {
lblx1.setText("X1 = " + miSol[0].toString( ) );
lblx2.setText("X2 = " + miSol[1].toString( ) ); } }
else {
javax.swing.JOptionPane.showMessageDialog(null, msj); } } });
}
public Double determinante( ) {
return (B * B - 4 * A * C); }
public Double[ ] Solucion() {
Double Sol[ ] = new Double[2];
if (determinante( ) == 0) {
Sol[0] = -B/2*A;
Sol[1] = Sol[0]; }
if (determinante() > 0) {
Sol[0] = (-B + Math.sqrt(determinante( ) ) );
Sol[0] = Sol[0]/(2*A);
Sol[1] = (-B - Math.sqrt(determinante( ) ) );
Sol[1] = Sol[1]/(2*A); }
if (determinante() < 0) {
Sol[0] = 100.0;
Sol[1] = Sol[0]; }
return Sol; }
public static void main(String args[ ]) {
new Ecuacion2do( ).setVisible(true); } }