Cronômetro
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Calculadora;
/**
*
* @author Android
*/
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CalculadoraTeste { public static void main(String[] args) {
CalculadoraFrame frame = new CalculadoraFrame(); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setVisible( true ); frame.setSize(250, 280); frame.setResizable(false); frame.setLocationRelativeTo(null);
}
} class CalculadoraFrame extends JFrame{ public CalculadoraFrame(){ setTitle("Calculadora"); CalculadoraPanel panel = new CalculadoraPanel(); add( panel ); pack(); }
}
class CalculadoraPanel extends JPanel{ public CalculadoraPanel(){ setLayout( new BorderLayout() ); resultado = 0; ultimoCommando = "="; iniciar = true; display = new JButton("0"); display.setEnabled( false ); display.setSize(100, 100); add( display, BorderLayout.NORTH );
Action insertAction = new InsertAction();
Action commandAction = new CommandAction();
panel = new JPanel(); panel.setLayout( new GridLayout( 4, 4 ));
addButton("7", addButton("8", addButton("9", addButton("/", insertAction ); insertAction ); insertAction ); commandAction );
addButton("4", addButton("5", addButton("6", addButton("*", insertAction ); insertAction ); insertAction ); commandAction );
addButton("1", addButton("2", addButton("3", addButton("-", insertAction ); insertAction ); insertAction ); commandAction );
addButton("0", addButton(".", addButton("=", addButton("+", insertAction ); insertAction ); commandAction ); commandAction );
add( panel, BorderLayout.CENTER );
//Conjunto de código que faz capturar as teclas digitadas como entradas para a calculadora
InputMap imap = getInputMap(
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ); imap.put( KeyStroke.getKeyStroke('0'), "panel.0"); imap.put( KeyStroke.getKeyStroke('1'),