In this tutorial we are providing you an example which illustrates you how to a create calculator in Swing with the source code and screen shot.
Writing Calculator Program in SwingIn this tutorial we are providing you an example which illustrates you how to a create calculator in Swing with the source code and screen shot.
For developing a small calculator program in swing we need two different classes
1) SwingCalculator.java
2) Calculator.java
The SwingCalculator.java calls the Calculator.javaclass by JFrame frame = new Calculator(). All the methods and actions are to be performed in Calculator.javaclass.
Calculator Code in Java Swing
Please save the code as SwingCalculator.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class SwingCalculator {
public static void main(String[] args) {
JFrame frame = new Calculator();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Here is the code of Calculator.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class Calculator extends JFrame {
private final Font BIGGER_FONT = newFont("monspaced",
Font.PLAIN, 20);
private JTextField textfield;
private boolean number = true;
private String equalOp = "=";
private CalculatorOp op = new CalculatorOp();
public Calculator() {
textfield = new JTextField("0", 12);
textfield.setHorizontalAlignment(JTextField.RIGHT);
textfield.setFont(BIGGER_FONT);
ActionListener numberListener = new NumberListener();
String buttonOrder = "1234567890 ";
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 4, 4));
for (int i = 0; i < buttonOrder.length(); i++) {
String key = buttonOrder.substring(i, i+1);
if (key.equals(" ")) {
buttonPanel.add(new JLabel(""));
} else {
JButton button = new JButton(key);
button.addActionListener(numberListener);
button.setFont(BIGGER_FONT);
buttonPanel.add(button);
}
}
ActionListener operatorListener = newOperatorListener();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4, 4, 4));
String[] opOrder = {"+", "-", "*", "/","=","C"};
for (int i = 0; i < opOrder.length; i++) {
JButton button = new JButton(opOrder[i]);
button.addActionListener(operatorListener);
button.setFont(BIGGER_FONT);
panel.add(button);
}
JPanel pan = new JPanel();
pan.setLayout(new BorderLayout(4, 4));
pan.add(textfield, BorderLayout.NORTH );
pan.add(buttonPanel , BorderLayout.CENTER);
pan.add(panel , BorderLayout.EAST );
this.setContentPane(pan);
this.pack();
this.setTitle("Calculator");
this.setResizable(false);
}
private void action() {
number = true;
textfield.setText("0");
equalOp = "=";
op.setTotal("0");
}
class OperatorListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (number) {
action();
textfield.setText("0");
} else {
number = true;
String displayText = textfield.getText();
if (equalOp.equals("=")) {
op.setTotal(displayText);
} else if (equalOp.equals("+")) {
op.add(displayText);
} else if (equalOp.equals("-")) {
op.subtract(displayText);
} else if (equalOp.equals("*")) {
op.multiply(displayText);
} else if (equalOp.equals("/")) {
op.divide(displayText);
}
textfield.setText("" + op.getTotalString());
equalOp = e.getActionCommand();
}
}
}
class NumberListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String digit = event.getActionCommand();
if (number) {
textfield.setText(digit);
number = false;
} else {
textfield.setText(textfield.getText() + digit);
}
}
}
public class CalculatorOp {
private int total;
public CalculatorOp() {
total = 0;
}
public String getTotalString() {
return ""+total;
}
public void setTotal(String n) {
total = convertToNumber(n);
}
public void add(String n) {
total += convertToNumber(n);
}
public void subtract(String n) {
total -= convertToNumber(n);
}
public void multiply(String n) {
total *= convertToNumber(n);
}
public void divide(String n) {
total /= convertToNumber(n);
}
private int convertToNumber(String n) {
return Integer.parseInt(n);
}
}
}
The constructor new CalculatorOp() calls the CalculatorOp class. The Swing component JTextField is used to create textbox on which calculation is to be performed. JPanel arranges the numeric buttons in a panel. JButton is used to perform an action. OperatorListener class is called to perform action on operators, i.e, '+,-,*,/,='. The class NumberListener is called for numbers 0 to 9.
Output will be displayed as:
AWT is a Java package for creating graphical user interfaces. JavaScript is a completely unrelated programming language.
AWT stands for Abstract window toolkit. AWT gives us the components using which we can create User- Interface based applications in java. Some of the components are: a. Frame b. Panel c. Window d. CheckBox e. RadioButton f. Button g. TextBox h. TextArea i. Etc
AWT (Abstract Window Toolkit) is a top-level Java package. Listing out the hundreds or thousands of methods would be a waste of effort. See the related link below for the Java documentation on the AWT package.
AWT stands for Abstract window tootlkit . Abstract window Toolkit provides a standard application programming interface for writing graphical user interfaces in java.
java.awt is a standard package of Java. It is a GUI(Graphical User Interface) package, which has classes in it such as Frame, Panel, and Button. Most of the package was later replaced with the javax.swing package, which has most of the same classes, only with a J prepended to them (JFrame, JPanel, JButton). However the java.awt package still includes some event handlers that are considered standard in java (java.awt.event).
Yes it is possible to draw in java by using AWT package. or by using javax package.
AWT is a Java package for creating graphical user interfaces. JavaScript is a completely unrelated programming language.
AWT stands for Abstract window toolkit. AWT gives us the components using which we can create User- Interface based applications in java. Some of the components are: a. Frame b. Panel c. Window d. CheckBox e. RadioButton f. Button g. TextBox h. TextArea i. Etc
AWT (Abstract Window Toolkit) is a top-level Java package. Listing out the hundreds or thousands of methods would be a waste of effort. See the related link below for the Java documentation on the AWT package.
AWT stands for Abstract Window Toolkit It contains the list of java classes & packages that can be used to create UI based applications using java. Some components available are: * button * text field * text area * scroll bar * etc...
awt contains all the graphical features of java, including many different elements to create an acceptable and usable GUI for applications easily. such as buttons, sliders and text areas.
http://docs.oracle.com/javase/7/docs/api/java/awt/event/ItemListener.html#itemStateChanged%28java.awt.event.ItemEvent%29 "http://docs.oracle.com/javase/7/docs/api/java/awt/event/ItemListener.html#itemStateChanged%28java.awt.event.ItemEvent%29"
AWT stands for Abstract window tootlkit . Abstract window Toolkit provides a standard application programming interface for writing graphical user interfaces in java.
AWT (Abstract Window Toolkit) is Java's original widget program allowing webmasters to add outside widgets to their sites for users to interface between them and other sites.
Virtual Vision Institute in Pune is the best for Java Course. You get best training regarding Java interfaces, Java Packages, Java AWT & SwingsFor More Information you can visit our websitevirtualvision.in/core-java/Contact Number : 9766119340
There are many different types of Java programs. a. Simple Standalone applications - They are simple java programs that have a bunch of classes and have a starting class that has a main method b. UI Based Applications - These are UI based applications built using AWT or Java Swings c. J2EE Web Applications - These are web based applications that can be run in a web browser. They are coded using technologies like servlets, jsp, hibernate, spring etc.
java.awt is a standard package of Java. It is a GUI(Graphical User Interface) package, which has classes in it such as Frame, Panel, and Button. Most of the package was later replaced with the javax.swing package, which has most of the same classes, only with a J prepended to them (JFrame, JPanel, JButton). However the java.awt package still includes some event handlers that are considered standard in java (java.awt.event).