|
|
|||||||
| Computers & Information Technologies « Everything related to computers and internet. » |
![]() |
|
|
Share | Thread Tools | Search this Thread |
|
|
#1 |
|
Registered Member
Last Online: 06-20-2011
Join Date: Mar 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
|
Please some help to create a calculator on java netbeans program
plz
|
|
|
|
|
|
#2 |
|
Last Online: 12-20-2021
Join Date: Mar 2006
Posts: 6,245
Thanks: 2,121
Thanked 3,365 Times in 1,740 Posts
Groans: 29
Groaned at 44 Times in 35 Posts
|
Here's one I made a year and a half ago as a Java Course project.
Code:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame{
private JTextField textfield; // The number Text Field.
private JPanel superPanel; // The panel that contains all the other panels.
private JPanel numberPanel; // The panel that contains the number buttons.
private JPanel operatorPanel; // The panel that contains the operator buttons.
private JMenuBar menuBar = new JMenuBar(); // The Calculator's Menu Bar.
private JFrame helpFrame = new JFrame("Help"); // The help's Frame
private JDialog errorDialog = new JDialog();
private ActionListener buttonListener = new ButtonListener(); // The listener for the Buttons others than numbers and operators.
private ActionListener numberListener = new NumberListener(); // The listener for the Number Buttons.
private ActionListener operatorListener = new OperatorListener(); // The listener for the Operator Buttons.
private boolean enteredFirstNumber = false; // The boolean that tells the operation is a first number was entered to make an operation.
private boolean enteredDot = false; // The boolean that disables the user from adding more than a dot in a number.
private String operator = "="; // The operator that controls the operations.
private CalculatorOperation operation = new CalculatorOperation(); // The operation object/
private final Font FONT = new Font("Comic Sans MS", Font.PLAIN, 25); // The font for the calculator Frame/
public Calculator(){
/*
* ADDING THE MENU BAR
*/
JMenu fileMenu = new JMenu("File"); // Creating a Menu with the name File.
JMenuItem fileExitMenuItem = new JMenuItem("Exit"); // Creating the Exit button for the File Menu.
fileExitMenuItem.addActionListener(buttonListener); // Adding the listener.
fileMenu.add(fileExitMenuItem); // Adding the Exit button to the File Menu.
menuBar.add(fileMenu); // Adding the File Menu to the Menu Bar.
JMenu editMenu = new JMenu("Edit"); // Creating a Menu with the name Edit.
JMenuItem editClearMenuItem = new JMenuItem(); // Creating the Clear button for the Edit Menu.
editClearMenuItem.setName("C"); // Setting The Name of the Button that will be used in the listener.
editClearMenuItem.setText("Clear"); // Setting The Text that is shown in the button.
editClearMenuItem.addActionListener(operatorListener); // Adding the Listener.
editMenu.add(editClearMenuItem); // Adding the Clear Button to the Edit Menu.
JMenuItem editEqualMenuItem = new JMenuItem(); // Creating the Equal button for the Edit Menu.
editEqualMenuItem.setName("="); // Setting The Name of the Button that will be used in the listener.
editEqualMenuItem.setText("Equals"); // Setting The Text that is shown in the button.
editEqualMenuItem.addActionListener(operatorListener); // Adding the Listener.
editMenu.add(editEqualMenuItem); // Adding the Equal Button to the Edit Menu.
menuBar.add(editMenu); // Adding the Edit Menu to the Menu Bar.
JMenu helpMenu = new JMenu("Help"); // Creating A Menu with the Name Help.
JMenuItem helpHowToUseMenuItem = new JMenuItem("How To Use"); // Adding the How To Use Button for the Help Menu.
helpHowToUseMenuItem.addActionListener(buttonListener); // Adding The Listener.
helpMenu.add(helpHowToUseMenuItem); // Adding the How To Use Button to the Help Menu.
menuBar.add(helpMenu); // Adding the Help Menu to the Menu Bar.
/*
* ADDING THE NUMBER BUTTONS TO THE NUMBER PANEL
*/
String numberName = "1234567890. "; // The Names of the number buttons in order.
numberPanel = new JPanel(new GridLayout(4,4, 5,5)); // Creating the Panel of the Number Buttons
for(int i = 0; i < numberName.length(); i++){
String name = numberName.substring(i,i+1); // Choosing the Name of the Button from the String.
if(name.equals(" "))
break;
else{
if(name.equals("0")){
JLabel label = new JLabel();
numberPanel.add(label); // Adding a Label before the 0 Button to make the Button appear in the Middle.
}
JButton button = new JButton(name); // Creating the Button.
button.setFont(FONT); // Setting the Font.
button.addActionListener(numberListener); // Adding the Listener.
numberPanel.add(button); // Adding the Button to the Panel.
}
}
/*
* ADDING THE OPERATOR BUTTONS TO THE OPERATOR PANEL // Same methodes used for the number panel are used in this Panel setting.
*/
String operatorName = "+-*/C= ";
operatorPanel = new JPanel(new GridLayout(4,4, 5,5));
for(int i = 0; i < operatorName.length(); i++){
String name = operatorName.substring(i,i+1);
if(name.equals(" "))
break;
else{
JButton button = new JButton(name);
button.setFont(FONT);
button.addActionListener(operatorListener);
operatorPanel.add(button);
}
}
/*
* CREATING THE TEXT FIELD TO SHOW THE RESULTS AND CURRENT NUMBERS
*/
textfield = new JTextField("0"); // Creating the Textfield with the number 0.
textfield.setFocusable(false); // Disabling the user from adding unrecognized digits (example: letters)
textfield.setHorizontalAlignment(JTextField.RIGHT); // Aligne to right in text field.
textfield.setFont(FONT); // Setting the font.
/*
* ADDING SUBPANELS TO THE SUPER PANEL
*/
superPanel = new JPanel(new BorderLayout(4,4)); // Creating a BorderLayout Panel with 4 pixels between the Contained Items
superPanel.add(textfield, BorderLayout.NORTH); // Adding the Textfield at the top of the Panel.
superPanel.add(numberPanel, BorderLayout.WEST); // Adding the Numberpanel at the left of the Panel.
superPanel.add(operatorPanel, BorderLayout.EAST); // Adding the Operatorpanel at the right of the Panel.
/*
* ADDING THE HELP FRAME
*/
JButton okButton = new JButton("OK"); // This is the button that allows the user to close the help frame.
JPanel okPanel = new JPanel(); // This is the panel that's gonna containe the OK Button
okPanel.add(okButton); // Adding the ok button to the panel
okButton.addActionListener(buttonListener); // Adding the listener to the ok button
JPanel helpPanel = new JPanel(new GridLayout(4,1,20,0)); // Creating the help panel with a GridLayout of 4 componants in a column with 20 pixels between them.
// Adding the help text.
helpPanel.add(new JLabel(" Use Number Panel To Enter Numbers"));
helpPanel.add(new JLabel(" Use Operator Panel To Make Operations"));
helpPanel.add(new JLabel(" Note: If You Press Enter, You Wont Be Able To Use The Result For A New Operation"));
helpPanel.add(okPanel);
helpFrame.setContentPane(helpPanel); // Setting the Panel of the help frame.
helpFrame.pack();
helpFrame.setResizable(false);
helpFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // Makes the help frame exists when a user closes it.
/*
* SETTING CONTENT PANE, MENU BAR, SIZE, AND TITLE.
*/
setJMenuBar(menuBar);
setContentPane(superPanel);
setResizable(false);
setTitle("Calculator");
pack();
}
public void clear(){ // This Method Resets The Calculator
enteredFirstNumber = false;
enteredDot = false;
operator = "=";
textfield.setText("0");
operation.setTotal("0");
}
/*
* LISTENERS
*/
public class OperatorListener implements ActionListener{
public void actionPerformed(ActionEvent event){
if(!enteredFirstNumber){ // The sentinel that makes the calculator start a new operation, and not to continue from an old result
enteredFirstNumber = true;
textfield.setText("0");
operation.setTotal("0");
operator = "=";
}
else{ // Here the calculator makes the operations knowing that there are 2 numbers to affect the operation to.
enteredFirstNumber = false;
String display = textfield.getText();
if(operator.equals("+")){
operation.add(display);
}
else if(operator.equals("-")){
operation.sub(display);
}
else if(operator.equals("*")){
operation.mul(display);
}
else if(operator.equals("/")){
try{
operation.div(display);
}
catch(DividingByZeroException e){
clear();
}
}
else if(operator.equals("=")){
operation.setTotal(display);
}
textfield.setText(operation.totalToString()); // Showing the result.
operator = event.getActionCommand(); // Saving the operator for later operations.
if(operator.equals("C")){ // Resetting the calculator.
clear();
}
}
}
}
public class NumberListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
String key = event.getActionCommand();
if(textfield.getText().length() >= 15){
// To avoid entering a number larger than 15 digits.
}
else if(!enteredFirstNumber){ // To remove the leading Zero.
if(key.equals(".")){
textfield.setText("0."); // To wrote 0. instead of . when the first letter entered is a dot.
enteredDot = true;
enteredFirstNumber = true;
}
else if(key.equals("0")){
// To avoid entering a leading Zero.
}
else{
textfield.setText(key);
enteredFirstNumber = true;
}
}
else if(key.equals(".") && !enteredDot){
enteredDot = true;
textfield.setText(textfield.getText()+key);
}
else if(key.equals(".") && enteredDot){
// to avoid entering more than a dot.
}
else
textfield.setText(textfield.getText()+key);
}
}
public class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
if(event.getActionCommand().equals("Exit")){
dispose(); // This event closes the Calculator
}
else if(event.getActionCommand().equals("How To Use")){
helpFrame.setVisible(true); // This event shows the Help Frame.
}
else if(event.getActionCommand().equals("OK")){ // This event closes the Help Frame.
helpFrame.dispose();
}
else if(event.getActionCommand().equals("Ok")){ // This event closes the Error Dialog.
errorDialog.dispose();
}
}
}
/*
* OPERATIONS
*/
public class CalculatorOperation{
private double total;
private double convertToNumber(String number){
return Double.parseDouble(number);
}
public CalculatorOperation(){
total = 0;
}
public void setTotal(String number){
total = convertToNumber(number);
}
public void add(String number){
total += convertToNumber(number);
}
public void sub(String number){
total -= convertToNumber(number);
}
public void mul(String number){
total *= convertToNumber(number);
}
public void div(String number)throws DividingByZeroException{
if(convertToNumber(number)== 0){ // Throwing the exception.
System.out.println("Engaging Exception");
throw new DividingByZeroException();
}
total /= convertToNumber(number);
}
public String totalToString(){
return "" + total; // Returning the total under a String type instead of double.
}
}
/*
* EXCEPTION
*/
public class DividingByZeroException extends Exception{
public DividingByZeroException(){
// The exception creates a Dialog that tells the user that the calculator cannot divide by Zero.
JPanel errorPanel = new JPanel(null); // A null Layout means that we're gonna have to set the places and dimensions of all the componants
JLabel errorLabel = new JLabel("Cannot Divide By Zero!");
JButton okButton = new JButton("Ok");
Font errorFont = new Font("Times New Roman", Font.BOLD, 15);
errorLabel.setFont(errorFont);
errorLabel.setBounds(40,0,260,50);
errorPanel.add(errorLabel);
errorPanel.add(okButton);
okButton.addActionListener(buttonListener);
okButton.setBounds(85, 40, 75,20);
errorDialog.setContentPane(errorPanel);
errorDialog.setSize(260,100);
errorDialog.setAlwaysOnTop(true);
errorDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
errorDialog.setVisible(true);
errorDialog.setResizable(false);
}
}
/*
* MAIN
*/
public static void main(String arguments[]){
JFrame c = new Calculator();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setVisible(true); // Setting the calculator visible to the user
}
}
__________________
What we do in life, echoes in eternity.
|
|
|
|
|
|
#3 |
|
Registered Member
Last Online: 06-20-2011
Join Date: Mar 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
|
thx alot tawa
|
|
|
|
|
|
#4 |
|
Last Online: 12-20-2021
Join Date: Mar 2006
Posts: 6,245
Thanks: 2,121
Thanked 3,365 Times in 1,740 Posts
Groans: 29
Groaned at 44 Times in 35 Posts
|
The important thing is to learn how to do it, and not just copy it.
Anyway it's up to you.
__________________
What we do in life, echoes in eternity.
|
|
|
|
![]() |
|
| Tags |
| java |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
|
|