Calculadora simples
Publicado por Reginaldo de Matias 14/11/2006
[ Hits: 12.790 ]
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); } }
Script para cálculo de distâncias na superfície terrestre utilizando coordenadas geográficas
Algoritmos para Teoria dos Números
Ordenação de vetores com letras do alfabeto
Nenhum comentário foi encontrado.
Wayland em alta, IA em debate e novos ventos para distros e devices
Qt 6.10 e Python 3.14 puxam o ritmo do software livre nesta quarta
Atualizações de Apps, Desktop e Kernel agitam o ecossistema Linux nesta terça-feira
Miyoo Mini Plus + Onion OS (Linux)
IA local no bolso, novo visual no Raspberry Pi OS e mais destaques do software livre
Como mudar o nome de dispositivos Bluetooth via linha de comando
Adicionando o repositório backports no Debian 13 Trixie
queria saber como instalar o android power tools igual ao winux no lin... (7)
Como listar os arquivos em "bloquin... [RESOLVIDO] (6)
Como colocar atalho para uma pasta na área de trabalho do Ubuntu 24.04... (2)