Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package calculator;
- import java.awt.*;
- import javax.swing.*;
- import javax.swing.BorderFactory;
- import javax.swing.border.*;
- import java.awt.event.*;
- //Not public. Only accessible from within this folder.
- class CalcGUI extends JFrame {
- //class variables and constants
- //use the same font for everything
- static Font displayFont = new Font("Arial", Font.PLAIN, 18);
- //instance objects inside GUI
- JButton bClr,bPlus,bMinus,bTimes,bDivide,bEquals,bSqr;
- JButton[] bDigit = new MyButton[10];
- JButton bp,bs; //bp=button point, bs = button sign
- private JTextField display;
- MyActionListener myAL = new MyActionListener();
- MyKeyListener myKL = new MyKeyListener();
- // for testing only
- // public static void main(String args[]) {
- // new CalcGUI();
- // }
- CalcGUI() { //constructor
- //set up everything to do with the JFrame here
- this.setSize(310,300);
- this.setTitle("Simple Calculator");
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setDefaultLookAndFeelDecorated(true);
- this.setResizable(false);
- //use a contentPane in case I want to change the background
- Container content = this.getContentPane( );
- content.setLayout(new BorderLayout( ));
- //Make panels and components
- JPanel panelTop = new JPanel(); //defaults to flowlayout
- panelTop.setLayout(new FlowLayout(FlowLayout.CENTER,12,0)); //add some horizontal spacing
- panelTop.setBackground(new Color(249,249,179));
- display = new JTextField("0.0",16);
- display.setHorizontalAlignment(JTextField.RIGHT);
- display.setFont(displayFont);
- display.addKeyListener(myKL);
- panelTop.add(display);
- JButton bClr = new JButton("CE"); //this button is made separately. It needs smaller font.
- bClr.addActionListener(myAL);
- panelTop.add(bClr);
- //use borders for spacing.
- Border border0 = BorderFactory.createEmptyBorder(5,0,5,0);
- panelTop.setBorder(border0);
- content.add(panelTop, BorderLayout.NORTH);
- //set up number panel
- JPanel panelNum = new JPanel();
- panelNum.setLayout(new GridLayout(4,3,3,3));
- panelNum.setBackground(new Color(62,140,227));
- Border border = BorderFactory.createMatteBorder(5, 5, 2, 2, new Color(62,140,227));
- panelNum.setBorder(border);
- //create the digit buttons using an array.
- //The name of the buttons doesn't matter.
- //I'm basing my calcMain on the fact that each button will have a different first letter.
- for(int i=0;i<10;i++)
- bDigit[i] = new MyButton("" + i);
- panelNum.add(bDigit[7]);
- panelNum.add(bDigit[8]);
- panelNum.add(bDigit[9]);
- panelNum.add(bDigit[4]);
- panelNum.add(bDigit[5]);
- panelNum.add(bDigit[6]);
- panelNum.add(bDigit[1]);
- panelNum.add(bDigit[2]);
- panelNum.add(bDigit[3]);
- panelNum.add(bDigit[0]);
- JButton bp = new MyButton(".");
- JButton bs = new MyButton("\u00B1"); //Unicode +- character.
- panelNum.add(bp);
- panelNum.add(bs);
- content.add(panelNum, BorderLayout.CENTER);
- JPanel panelRight = new JPanel();
- panelRight.setLayout(new GridLayout(6,1,3,3));
- panelRight.setBackground(new Color(227,140,227));
- Border border2 = BorderFactory.createMatteBorder(9, 9, 7, 9, new Color(235,160,235));
- panelRight.setBorder(border2);
- JButton bSqr = new MyButton("\u221A");
- JButton bPlus = new MyButton("+");
- JButton bMinus = new MyButton("-");
- JButton bTimes = new MyButton("x");
- JButton bDivide = new MyButton("\u00F7");
- JButton bEquals = new MyButton("=");
- panelRight.add(bSqr);
- panelRight.add(bPlus);
- panelRight.add(bMinus);
- panelRight.add(bTimes);
- panelRight.add(bDivide);
- panelRight.add(bEquals);
- content.add(panelRight, BorderLayout.EAST);
- this.validate();
- this.setVisible(true);
- }
- //these two functions are called from CalcMain and are used to set the display.
- void setDisplay(String s){
- display.setText(s);
- }
- void setDisplay(Double d){
- display.setText(d.toString());
- }
- private class MyButton extends JButton{
- MyButton(String s) {
- super(s); //create the JButton just like normal
- char c = (char)s.charAt(0);
- if (c >= '0' && c <= '9') this.setBackground(new Color(230,240,255));
- this.setFont(displayFont);
- this.addActionListener(myAL); //all buttons can use the same action listener.
- }
- }
- private class MyActionListener implements ActionListener {
- public void actionPerformed( ActionEvent e ) {
- String txt = e.getActionCommand();
- CalcMain.sendButton(txt); //Here's the important part.
- }
- }
- private class MyKeyListener implements KeyListener {
- public void keyTyped( KeyEvent e ) {
- String txt ="";
- // Keyboard code for the pressed key.
- // int key = e.getKeyCode();
- char key = e.getKeyChar();
- switch (key) {
- case '\n': //Enter = '='
- txt="=";
- break;
- case '\b': //backspace = CE
- txt="C";
- break;
- case '/':
- txt="\u00F7";
- break;
- case '*':
- txt="x";
- break;
- case 'q':
- txt="\u221A";
- break;
- default:
- txt= Character.toString(key);
- }
- e.consume(); //after it updates the display, the keypressed also is displayed. Must consume it!!!
- CalcMain.sendButton(txt);
- }
- public void keyReleased( KeyEvent e ) {}
- public void keyPressed( KeyEvent e ) {}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement