Calculadora simples
Publicado por Reginaldo de Matias 14/11/2006
[ Hits: 12.419 ]
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
Cadastramento de Produtos com .JOptionPane
Script para cálculo de distâncias na superfície terrestre utilizando coordenadas geográficas
Pesquisa Binaria em um vetor ordenado
Nenhum comentário foi encontrado.
Atualizar o macOS no Mac - Opencore Legacy Patcher
Crie alias para as tarefas que possuam longas linhas de comando - bash e zsh
Criando um gateway de internet com o Debian
Configuração básica do Conky para mostrar informações sobre a sua máquina no Desktop
Aprenda a criar músicas com Inteligência Artificial usando Suno AI
Instalando Zoom Client no Fedora 40
Instalando Navegador Firefox no Debian 12
Bloqueando propagandas no Youtube e outros sites com o uBlocker Origin
Envio de link pelo whatsapp (0)
Sistema da Coréia do Norte - Red Star OS (55)
Como adicionar módulo de saúde da bateria dos notebooks Acer ao kernel... (47)