FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!

[Java] Calculator with GUI Pt. 1




Do you want to create a calculator with UI on Java? Then this is the right blog post you're looking for! You can inspect (or even copy) this source code and play it on your own!

Implementation:
On your main method, just invoke the Calculator class.
Code:

EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CalculatorUI();
            }
        });



Source code:
Code:

import javax.swing.*;
import javax.swing.border.*;

import java.awt.*;
import java.awt.event.*;

import java.text.*;

public class CalculatorUI implements ActionListener {
   private JFrame frame;
   private JPanel result, fields, fields2;
   private JLabel display;
   private JButton one, two, three, four, five, six, seven, eight, nine, zero;
   private JButton point, add, subtract, multiply, divide, sqrt, modulus, equals, reset;

   private String query = "";
   private double answer1 = 0;
   private int operation = 0;

   public CalculatorUI() {
      initComponents();
   }

   public void initComponents() {
      //Set to system look and feel
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        } catch(InstantiationException ie) {
            ie.printStackTrace();
        } catch(IllegalAccessException iae) {
            iae.printStackTrace();
        } catch(UnsupportedLookAndFeelException uloafe) {
            uloafe.printStackTrace();
        }

        //Create frame
        frame = new JFrame("Calculator");
        frame.setLayout(new BorderLayout());

        //Set panels
        result = new JPanel();
        result.setLayout(new FlowLayout(FlowLayout.CENTER));

        fields = new JPanel();
        fields.setLayout(new GridLayout(4, 4, 5, 5));

        fields2 = new JPanel();
        fields2.setLayout(new GridLayout(1, 3, 5, 5));

        //Set window options
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //Center the window
        int width = 300;
        int height = 400;
        Dimension setting = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (setting.width - width) / 2;
        int y = (setting.height - height) / 2;
        frame.setBounds(x, y, width, height);




1 blog comments below

Quote:
Code:
try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        } catch(InstantiationException ie) {
            ie.printStackTrace();
        } catch(IllegalAccessException iae) {
            iae.printStackTrace();
        } catch(UnsupportedLookAndFeelException uloafe) {
            uloafe.printStackTrace();
        }


If you're going to printstacktrace every exception, why not just do this?
Code:
try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
    e.printStackTrace();
}


adri on Sat Apr 21, 2012 1:57 pm



FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.