Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Un fucked code from: http://pastebin.com/xJJ4jYdE
- import java.awt.*;
- import java.awt.event.*;
- import java.text.DecimalFormat;
- import javax.swing.*;
- class HoveredButton extends JButton implements MouseListener{
- private Color defaultForeground;
- public HoveredButton(String label){
- super(label);
- addMouseListener(this);
- }
- public void mouseClicked(MouseEvent evt){}
- public void mousePressed(MouseEvent evt){}
- public void mouseReleased(MouseEvent evt){}
- public void mouseEntered(MouseEvent evt){
- defaultForeground = evt.getComponent().getForeground();
- setForeground(Color.WHITE);
- }
- public void mouseExited(MouseEvent evt){
- setForeground(defaultForeground);
- }
- }
- public class Calculator extends JFrame implements ActionListener {
- private static final DecimalFormat FORMAT = new DecimalFormat("###,##0.######;-###,##0.######");
- private JTextField display = null;
- private String lastOperation = "";
- private double buffer = 0.0;
- private String current = "0";
- public static void main(String[] args){
- Calculator calc = new Calculator();
- calc.setVisible(true);
- }
- private HoveredButton addButton(JPanel panel, String label, Color color){
- final HoveredButton button = new HoveredButton(label);
- Action action = new AbstractAction(label){
- public void actionPerformed(ActionEvent e){button.doClick();}
- };
- action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(label));
- button.getActionMap().put("autoAction", action);
- button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put((KeyStroke)action.getValue(Action.ACCELERATOR_KEY), "autoAction");
- button.addActionListener(this);
- button.setBackground(color);
- panel.add(button);
- return button;
- }
- public Calculator() {
- setTitle("My Calculator v1.0");
- setSize(248, 222);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLayout(new BorderLayout());
- display = new JTextField(15);
- display.setText("0");
- display.setHorizontalAlignment(JTextField.RIGHT);
- display.setEditable(false);
- JPanel panel = new JPanel();
- panel.setLayout(new GridLayout(5, 4));
- addButton(panel, "7", Color.LIGHT_GRAY);
- addButton(panel, "8", Color.LIGHT_GRAY);
- addButton(panel, "9", Color.LIGHT_GRAY);
- addButton(panel, "+", Color.CYAN);
- addButton(panel, "4", Color.LIGHT_GRAY);
- addButton(panel, "5", Color.LIGHT_GRAY);
- addButton(panel, "6", Color.LIGHT_GRAY);
- addButton(panel, "-", Color.CYAN);
- addButton(panel, "1", Color.LIGHT_GRAY);
- addButton(panel, "2", Color.LIGHT_GRAY);
- addButton(panel, "3", Color.LIGHT_GRAY);
- addButton(panel, "*", Color.CYAN);
- addButton(panel, ".", Color.LIGHT_GRAY);
- addButton(panel, "0", Color.LIGHT_GRAY);
- addButton(panel, "Sqrt", Color.CYAN);
- addButton(panel, "/", Color.CYAN);
- addButton(panel, "C", Color.MAGENTA);
- addButton(panel, "<-", Color.MAGENTA);
- addButton(panel, "%", Color.CYAN);
- addButton(panel, "=", Color.GREEN);
- JMenuBar menuBar = new JMenuBar();
- JMenu m_file = new JMenu("File");
- JMenuItem i_exit = new JMenuItem("Exit");
- i_exit.addActionListener(this); //Should have a separate actionlistener for the menu, but I can't be bothered.
- m_file.add(i_exit);
- menuBar.add(m_file);
- setJMenuBar(menuBar);
- add(display, BorderLayout.PAGE_START);
- add(panel, BorderLayout.CENTER);
- pack();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- String cmd = e.getActionCommand();
- if(cmd.equals("<-")){
- if(current.length() > 1){
- current=Double.parseDouble(current.substring(0, current.length()-1))+"";
- }else{current="0";}
- }else if(cmd.equals("C")){
- current = "0";
- buffer = 0.0;
- }else if(cmd.equals("%")){
- buffer = Double.parseDouble(current)/100;
- current = buffer+"";
- }else if(cmd.equals("+")||cmd.equals("-")||cmd.equals("*")||cmd.equals("/")){
- buffer = Double.parseDouble(current);
- current = "0";
- lastOperation = cmd;
- }else if(cmd.equals("Sqrt")){
- buffer = Math.sqrt(Double.parseDouble(current));
- current = buffer+"";
- lastOperation = cmd;
- }else if(cmd.equals("=") && !lastOperation.isEmpty()){
- try{
- buffer = calculate(buffer, Double.parseDouble(current), lastOperation.charAt(0));
- current = buffer+"";
- lastOperation = "";
- }catch(IllegalArgumentException ex){/*Apparently we don't care about this.*/}
- }else if(cmd.equals(".")){
- if(current.indexOf('.')==-1)
- current = current+".";
- }else if(cmd.equals("Exit")){
- System.exit(0);
- }else if(Character.isDigit(cmd.charAt(0))){
- current = current+cmd;
- }
- display.setText(FORMAT.format(Double.parseDouble(current)));
- }
- double calculate(double loperand, double roperand, char operator) {
- switch(operator){
- case '+': return loperand + roperand;
- case '-': return loperand - roperand;
- case '*': return loperand * roperand;
- case '/': return loperand / roperand;
- }
- throw new IllegalArgumentException("calculate: Illegal operator encountered");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement