Calculadora simples
Publicado por Reginaldo de Matias 14/11/2006
[ Hits: 12.881 ]
Homepage: http://mundodacomputacaointegral.blogspot.com/
O presente script efetua as quatro operações básicas de uma calculadora simples. Usa interface gráfica (swing).
/*Calculadora Simples
*Data: 14 de Setembro de 2006
*/
/**
*
*@author: Reginaldo de Matias
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculadora extends JFrame implements ActionListener
{
static JPanel jp1,jp2,jp3;
static JLabel l1,l2,l3,l4;
static JTextField tf1,tf2;
static JButton b1,b2,b3,b4;
private Container janela;
public Calculadora()
{
setTitle("Calculadora Simples");
setSize(300,250);
setResizable(false);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
jp1 = new JPanel();
jp1.setLayout(new GridLayout(6,1,10,10));
jp2 = new JPanel();
jp2.setLayout(new GridLayout(6,1,10,10));
jp3 = new JPanel();
jp3.setLayout(new FlowLayout());
l1 = new JLabel("Valor 1: ");
tf1 = new JTextField(10);
l2 = new JLabel("Valor 2: ");
tf2 = new JTextField(10);
l3 = new JLabel("");
l4 = new JLabel("");
b1 = new JButton(" + ");
b1.addActionListener(this);
b2 = new JButton(" - ");
b2.addActionListener(this);
b3 = new JButton(" X ");
b3.addActionListener(this);
b4 = new JButton(" / ");
b4.addActionListener(this);
jp1.add(l1); jp2.add(tf1); jp3.add(b1);
jp1.add(l2); jp2.add(tf2); jp3.add(b2);
jp1.add(l3); jp2.add(l4); jp3.add(b3);
jp3.add(b4);
janela = getContentPane();
janela.add(jp1,BorderLayout.WEST);
janela.add(jp2,BorderLayout.CENTER);
janela.add(jp3,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent evt)
{
try
{
Object source = evt.getSource();
float x = Float.parseFloat(tf1.getText());
float y = Float.parseFloat(tf2.getText());
if(source == b1)
{
l3.setText("Resultado: ");
l4.setText("" +soma(x,y));
}
if(source == b2)
{
l3.setText("Resultado: ");
l4.setText("" +subtrai(x,y));
}
if(source == b3)
{
l3.setText("Resultado: ");
l4.setText("" +multiplica(x,y));
}
if(source == b4)
{
l3.setText("Resultado: ");
l4.setText("" +divide(x,y));
}
}
catch(RuntimeException e)
{
System.out.println("Caught Runtime Exception " +e);
}
catch(Exception e)
{
System.out.println("\nCaught Exception " +e);
}
}
public float soma(float a,float b)
{
return (a+b);
}
public float subtrai(float a,float b)
{
return (a-b);
}
public float multiplica(float a,float b)
{
return (a*b);
}
public float divide(float a,float b)
{
return (a/b);
}
public static void main(String args[])
{
Calculadora window = new Calculadora();
window.setVisible(true);
}
}
Um classe que facilita a leitura de dados do teclahdo
Crivo de Eratóstenes Simples em Java
Classe Java para a validação de CNPJ
Nenhum comentário foi encontrado.
Instalando COSMIC no Linux Mint
Turbinando o Linux Mint: o poder das Nemo Actions
Inteligência Artificial no desenvolvimento de software: quando começar a usar?
Contorno de BUG do "color picker" para COSMIC Desktop
Pós Instalação do POP! OS 24.04
Contorno para BUG de som no COSMIC Desktop
Pfsense inacessivel após um periodo de tempo [RESOLVIDO] (3)









