Calculadora simples
Publicado por Reginaldo de Matias 14/11/2006
[ Hits: 12.633 ]
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); } }
Ordenação de vetores com letras do alfabeto
Contador de caracteres, palavras e linhas de um arquivo
Avaliação de expressões matemáticas
Algoritmos para Teoria dos Números
Nenhum comentário foi encontrado.
Instalar e Configurar o Slackware Linux em 2025
Como configurar os repositórios do apt no Debian 12 em 2025
Passkeys: A Evolução da Autenticação Digital
Instalação de distro Linux em computadores, netbooks, etc, em rede com o Clonezilla
Configurando o Conky para iniciar corretamente no sistema
3 configurações básicas que podem melhorar muito a sua edição pelo editor nano
Como colorir os logs do terminal com ccze
Instalação Microsoft Edge no Linux Mint 22
Como configurar posicionamento e movimento de janelas no Lubuntu (Openbox) com atalhos de teclado
Opção "authori... do DHCP não funciona. (1)
Nao consigo abrir meu waydroid (1)
IRPF 2025 não consegue entregar a declaração (3)