Está en la página 1de 4

UNIVERSIDAD DE LAS FUERZAS ARMADAS - ESPE

EXTENSIÓN LATACUNGA
NOMBRE: ASIGNATURA: CARRERA:
Esteban Herrera Programación II Electromecánica

NIVEL: PERIODO ACADÉMICO: FECHA DE ENVIO:


Segundo “A” Octubre 2015 - Febrero 2016 04 - 01 - 2016

TEMA: Interfaces Graficas en JAVA

Ejercicio:
Por medio de interfaces gráficas elabore, un programa que permita al usuario un ingreso de dos
numero los cuales se podrán sumar, restar, multiplicar y dividir, adicionalmente en la división
en caso de que el usuario introduzca como divisor el numero '0' salga un mensaje que informe
que "no es posible dividir para cero".

Código:
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.GridBagLayout;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import java.awt.TextField;
import java.awt.Button;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JEditorPane;

public class Principal extends JFrame {

private JPanel contentPane;


private JTextField t1;
private JTextField t2;
private JLabel lr;

public static void main(String[] args) {


EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Principal frame = new Principal();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public Principal() {

setIconImage(Toolkit.getDefaultToolkit().getImage(Principal.clas
s.getResource("/javax/swing/plaf/metal/icons/ocean/question.png")));
setTitle("Principal");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setForeground(Color.WHITE);
contentPane.setBackground(Color.LIGHT_GRAY);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JLabel L1 = new JLabel("CALCULADORA");


L1.setIcon(new
ImageIcon(Principal.class.getResource("/com/sun/java/swing/plaf/window
s/icons/Computer.gif")));
L1.setFont(new Font("Times New Roman", Font.BOLD, 20));
L1.setForeground(Color.YELLOW);
L1.setBounds(104, 11, 211, 32);
contentPane.add(L1);

JLabel lblIngreseNumero = new JLabel("Ingrese numero 1");


lblIngreseNumero.setFont(new Font("Times New Roman",
Font.BOLD | Font.ITALIC, 12));
lblIngreseNumero.setBounds(38, 67, 109, 14);
contentPane.add(lblIngreseNumero);

JLabel lblIngreseNumero_1 = new JLabel("Ingrese numero 2");


lblIngreseNumero_1.setFont(new Font("Times New Roman",
Font.BOLD | Font.ITALIC, 12));
lblIngreseNumero_1.setBounds(38, 111, 109, 14);
contentPane.add(lblIngreseNumero_1);

JButton btnSuma = new JButton("SUMA");


btnSuma.setBackground(Color.WHITE);
btnSuma.setForeground(Color.RED);
btnSuma.setFont(new Font("Times New Roman", Font.ITALIC,
11));
btnSuma.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());

int nr=n1+n2;
String resul=""+nr;
lr.setText(resul);

}
});
btnSuma.setBounds(24, 150, 139, 23);
contentPane.add(btnSuma);

JButton btnResta = new JButton("RESTA");


btnResta.setForeground(Color.RED);
btnResta.setFont(new Font("Times New Roman", Font.ITALIC,
11));
btnResta.setBackground(Color.WHITE);
btnResta.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int nr=n1-n2;
String resul=""+nr;
lr.setText(resul);

}
});
btnResta.setBounds(238, 150, 103, 23);
contentPane.add(btnResta);

JButton btnLimpiar = new JButton("LIMPIAR");


btnLimpiar.setFont(new Font("Times New Roman", Font.ITALIC,
11));
btnLimpiar.setForeground(Color.GREEN);
btnLimpiar.setBackground(Color.WHITE);
btnLimpiar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
t1.setText("");//no poner espacios
t2.setText("");
lr.setText("");
}
});
btnLimpiar.setBounds(301, 86, 89, 23);
contentPane.add(btnLimpiar);

t1 = new JTextField();
t1.setBounds(157, 64, 86, 20);
contentPane.add(t1);
t1.setColumns(10);

t2 = new JTextField();
t2.setBounds(157, 108, 86, 20);
contentPane.add(t2);
t2.setColumns(10);

JLabel lblResultado = new JLabel("RESULTADO");


lblResultado.setFont(new Font("Times New Roman", Font.BOLD
| Font.ITALIC, 12));
lblResultado.setBounds(38, 236, 97, 14);
contentPane.add(lblResultado);

lr = new JLabel("");
lr.setBounds(159, 236, 231, 14);
contentPane.add(lr);

JButton btnMultiplicacion = new JButton("MULTIPLICACION");


btnMultiplicacion.setForeground(Color.RED);
btnMultiplicacion.setBackground(Color.WHITE);
btnMultiplicacion.setFont(new Font("Times New Roman",
Font.ITALIC, 11));
btnMultiplicacion.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int nr=n1*n2;
String resul=""+nr;
lr.setText(resul);
}
});
btnMultiplicacion.setBounds(24, 184, 139, 23);
contentPane.add(btnMultiplicacion);

JButton btnDivision = new JButton("DIVISION");


btnDivision.setForeground(Color.RED);
btnDivision.setFont(new Font("Times New Roman",
Font.ITALIC, 11));
btnDivision.setBackground(Color.WHITE);
btnDivision.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
float n1 = Integer.parseInt(t1.getText());
float n2 = Integer.parseInt(t2.getText());
if(n2==0){
lr.setText("El divisor no debe ser 0");}
else{
float nr=n1/n2;
String resul=""+nr;
lr.setText(resul);}
}
});
btnDivision.setBounds(238, 184, 103, 23);
contentPane.add(btnDivision);
}
}
Consola:

También podría gustarte