CALCULADORA EN AWT
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CALCULADORA extends JFrame{
// Atributos
JLabel L1, L2, L3, L4;
JTextField T1;
JTextField T2;
JButton B1, B2, B3, B4, B5, B6;
// Crear los atributos (inicializarlos)
public CALCULADORA(){
L1 = new JLabel("Calculadora");
L2 = new JLabel("Ingrese el primer numero: ");
L3 = new JLabel("Ingrese el segundo numero: ");
L4 = new JLabel("");
T1 = new JTextField();
T2 = new JTextField();
B1 = new JButton("Suma");
B2 = new JButton("Resta");
B3 = new JButton("Producto");
B4 = new JButton("Div. Ent.");
B5 = new JButton("Div. real");
B6 = new JButton("Modulo");
add(L1); L1.setBounds(135, 10, 100, 30);
add(L2); L2.setBounds(20, 50, 180, 30);
add(T1); T1.setBounds(250, 50, 80, 30);
add(L3); L3.setBounds(20, 90, 180, 30);
add(T2); T2.setBounds(250, 90, 80, 30);
add(B1); B1.setBounds(20, 130, 90, 30);
add(B2); B2.setBounds(130, 130, 90, 30);
add(B3); B3.setBounds(240, 130, 90, 30);
add(B4); B4.setBounds(20, 170, 90, 30);
add(B5); B5.setBounds(130, 170, 90, 30);
add(B6); B6.setBounds(240, 170, 90, 30);
add(L4); L4.setBounds(20, 210, 150, 30);
B1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(!T1.getText().isEmpty() && !T2.getText().isEmpty()){
int a, b, res;
a = Integer.parseInt(T1.getText());
b = Integer.parseInt(T2.getText());
res = a + b;
L4.setText("La suma es: " + res);
}else{
L4.setText("");
}
}
});
B2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(!T1.getText().isEmpty() && !T2.getText().isEmpty()){
int a, b, res;
a = Integer.parseInt(T1.getText());
b = Integer.parseInt(T2.getText());
res = a - b;
L4.setText("La resta es: " + res);
}else{
L4.setText("");
}
}
});
B3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(!T1.getText().isEmpty() && !T2.getText().isEmpty()){
int a, b, res;
a = Integer.parseInt(T1.getText());
b = Integer.parseInt(T2.getText());
res = a * b;
L4.setText("El producto es: " + res);
}else{
L4.setText("");
}
}
});
B4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(!T1.getText().isEmpty() && !T2.getText().isEmpty()){
int a, b, res;
a = Integer.parseInt(T1.getText());
b = Integer.parseInt(T2.getText());
res = a / b;
L4.setText("La division entera es: " + res);
}else{
L4.setText("");
}
}
});
B5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(!T1.getText().isEmpty() && !T2.getText().isEmpty()){
int a, b;
double res;
String cad = "";
a = Integer.parseInt(T1.getText());
b = Integer.parseInt(T2.getText());
res = (double)a / b;
cad = String.format("La division real es: %.3f", res);
L4.setText(cad);
}else{
L4.setText("");
}
}
});
B6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(!T1.getText().isEmpty() && !T2.getText().isEmpty()){
int a, b, res;
a = Integer.parseInt(T1.getText());
b = Integer.parseInt(T2.getText());
res = a % b;
L4.setText("El modulo es: " + res);
}else{
L4.setText("");
}
}
});
}
public static void main(String[] args) {
CALCULADORA obj = new CALCULADORA();
obj.setLayout(null);
obj.setSize(370, 400);
obj.setLocation(400, 300);
obj.setVisible(true);
}
}
PROGRAMA EN CLASE
COMPLETO
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PROY_SIS extends JFrame{
JButton B1,B2,B3;
public PROY_SIS()
{super("SISTEMA");
B1=new JButton("SERIES");
B2=new JButton("CONVERSION");
B3=new JButton("SALIR");
add(B1);B1.setBounds(150,150,120,30);
add(B2);B2.setBounds(150,200,120,30);
add(B3);B3.setBounds(150,250,120,30);
B1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
SERIES pro=new SERIES();
pro.setLayout(null);
pro.setSize(700,500);
pro.setLocationRelativeTo(null);
pro.setDefaultCloseOperation(0);
pro.setVisible(true);
}
}
);
B2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CONVERSION pro=new CONVERSION();
pro.setLayout(null);
pro.setSize(700,500);
pro.setLocationRelativeTo(null);
pro.setDefaultCloseOperation(0);
pro.setVisible(true);
}
}
);
B3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
);
}
public static void main(String[] args) {
PROY_SIS pro=new PROY_SIS();
pro.setLayout(null);
pro.setSize(500,500);
pro.setLocationRelativeTo(null);
pro.setDefaultCloseOperation(0);
pro.setVisible(true); }
}--------------------------- CLASE SERIES --------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class SERIES extends JFrame {
JLabel L1, L2, L3;
JTextField T1, T2, T3;
JButton B1, B2, B3;
public SERIES() {
super("SERIES");
L1 = new JLabel("ING. CANTIDAD");
L2 = new JLabel("SOLUCION");
T1 = new JTextField();
T2 = new JTextField();
B1 = new JButton("FIBO");
B2 = new JButton("FACTORIALES");
B3 = new JButton("SALIR");
add(L1);
L1.setBounds(30, 50, 100, 30);
add(T1);
T1.setBounds(140, 50, 100, 30);
add(L2);
L2.setBounds(30, 200, 60, 30);
add(T2);
T2.setBounds(100, 200, 500, 30);
add(B1);
B1.setBounds(50, 300, 100, 30);
add(B2);
B2.setBounds(160, 300, 180, 30);
add(B3);
B3.setBounds(350, 300, 100, 30);
B1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String res = "";
int n, i, a = 0, b = 1, f = 0;
n = Integer.parseInt(T1.getText());
for (i = 0; i < n; i++) {
f = a + b;
res = res + " " + f;
b = a;
a = f;
}
T2.setText("" + res);
}
});
B2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String res = "";
int n = Integer.parseInt(T1.getText());
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
res += factorial + " ";
}
T2.setText(res);
}
});
B3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
}
}------------------- CLASE CONVERSION ------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class CONVERSION extends JFrame {
JLabel L1, L2, L3;
JTextField T1, T2, T3;
JButton B1, B2, B3;
int convertir(char r) {
int nu = 0;
switch (r) {
case 'I':
nu = 1;
break;
case 'V':
nu = 5;
break;
case 'X':
nu = 10;
break;
case 'L':
nu = 50;
break;
case 'C':
nu = 100;
break;
case 'D':
nu = 500;
break;
case 'M':
nu = 1000;
break;
}
return nu;
}
public CONVERSION() {
super("CONVERSION DE NUMEROS");
L1 = new JLabel("ING. NUM ROMANO");
L2 = new JLabel("SOLUCION");
T1 = new JTextField();
T2 = new JTextField();
B1 = new JButton("ROMANOS");
B2 = new JButton("ARABIGOS");
B3 = new JButton("SALIR");
add(L1);
L1.setBounds(30, 50, 100, 30);
add(T1);
T1.setBounds(140, 50, 100, 30);
add(L2);
L2.setBounds(30, 200, 60, 30);
add(T2);
T2.setBounds(100, 200, 500, 30);
add(B1);
B1.setBounds(50, 300, 100, 30);
add(B2);
B2.setBounds(160, 300, 180, 30);
add(B3);
B3.setBounds(350, 300, 100, 30);
B1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String romano = T1.getText();
int numero = convertirRomano(romano);
T2.setText(String.valueOf(numero));
}
});
B2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Implementar conversión de arábigos a romanos si es necesario
}
});
B3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
}
public int convertirRomano(String romano) {
int numero = 0;
int longitud = romano.length();
int ultimoValor = 0;
for (int i = longitud - 1; i >= 0; i--) {
char letra = romano.charAt(i);
int valor = convertir(letra);
if (valor < ultimoValor) {
numero -= valor;
} else {
numero += valor;
ultimoValor = valor;
}
}
return numero;
}
}