Raised This Month: $ Target: $400
 0% 

Java help


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
commonbullet
Veteran Member
Join Date: Oct 2005
Old 06-13-2006 , 22:25  
Reply With Quote #1

Quote:
Originally Posted by knekter
I Will post some help in a minute, but First I advice to download Visual J#. It is much easier to work with and free. I am assuming you are using JCreator to write your code?
Don't know why you'd use J#, Java is terrible enough.
If you want to make it for Windows use C# instead.
If you need a good Java IDE, pickup Netbeans or Eclipse.

Here's a crap simple example with Integer numbers - to make a real one you'd have to handle more than int 32 bits numbers, and control float numbers.

Code:
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class CalculatorEdit extends JFrame {     public static final int WIDTH = 600;     public static final int HEIGHT = 200;     private JTextField inputOutputField;     private int lnum;     private int rnum;     private boolean restart;             private enum Operation{         NONE, MULTIPLY, ADD, SUBTRACT, DIVIDE     }     private Operation currentoperation;         public static void main(String[] args)     {         CalculatorEdit guiWithPanel = new CalculatorEdit();         guiWithPanel.setVisible(true);     }             private void pushDigit(String digit){                 if (restart){             inputOutputField.setText(digit);             restart = false;         }                 else if (getCalculatorNum() == 0 && !digit.equals("0"))                 inputOutputField.setText(digit);                 else {             int temp = getCalculatorNum();             inputOutputField.setText(temp + digit);                     }             }         private void setOperation(Operation operation){                         if (lnum > 0){             if (!restart )                 rnum = getCalculatorNum();         }         if(rnum > 0 && (operation == Operation.NONE)){             inputOutputField.setText("" + getResult());         }         lnum = getCalculatorNum();         if (operation != Operation.NONE)             this.currentoperation = operation;         this.restart = true;     }         private int getCalculatorNum(){         try {             int i = new Integer(inputOutputField.getText());             return i;         }         catch(Exception e){             return 0;         }     }         private int getResult(){         int result;         switch (currentoperation){             case NONE :                 result = rnum;                 break;             case MULTIPLY :                 result = lnum * rnum;                 break;             case DIVIDE :                 result = (rnum > 0) ? lnum / rnum : lnum;                 break;             case SUBTRACT :                 result = lnum - rnum;                 break;             case ADD :                 result = lnum + rnum;                 break;             default :                 result = rnum;         }         return result;     }         private void reset(){         lnum = 0;         rnum = 0;         inputOutputField.setText("");     }         public CalculatorEdit()     {         lnum = 0;         inputOutputField = new JTextField("0", 30);                                 setSize(WIDTH, HEIGHT);         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);         //addWindowListener(new WindowDestroyer());         setTitle("Gabens Miniature Calculator");         Container contentPane = getContentPane();         JPanel operationPanel = new JPanel();         JPanel buttonPanel = new JPanel();         contentPane.setLayout(new BorderLayout());                 contentPane.add(inputOutputField, BorderLayout.CENTER);                 JButton zeroButton = new JButton("0");         zeroButton.addActionListener(new java.awt.event.ActionListener() {             public void actionPerformed(java.awt.event.ActionEvent evt) {                 pushDigit("0");             }         });         buttonPanel.add(zeroButton);                 JButton oneButton = new JButton("1");         oneButton.addActionListener(new java.awt.event.ActionListener() {             public void actionPerformed(java.awt.event.ActionEvent evt) {                 pushDigit("1");             }         });         buttonPanel.add(oneButton);         JButton twoButton = new JButton("2");         twoButton.addActionListener(new ActionListener() {             public void actionPerformed(ActionEvent evt) {                 pushDigit("2");             }         });         buttonPanel.add(twoButton);                 JButton threeButton = new JButton("3");         threeButton.addActionListener(new java.awt.event.ActionListener() {             public void actionPerformed(java.awt.event.ActionEvent evt) {                 pushDigit("3");             }         });         buttonPanel.add(threeButton);                 JButton fourButton = new JButton("4");         fourButton.addActionListener(new java.awt.event.ActionListener() {             public void actionPerformed(java.awt.event.ActionEvent evt) {                 pushDigit("4");             }         });         buttonPanel.add(fourButton);                 JButton fiveButton = new JButton("5");         fiveButton.addActionListener(new ActionListener() {             public void actionPerformed(ActionEvent evt) {                 pushDigit("5");             }         });         buttonPanel.add(fiveButton);                 JButton sixButton = new JButton("6");         sixButton.addActionListener(new ActionListener() {             public void actionPerformed(ActionEvent evt) {                 pushDigit("6");             }         });         buttonPanel.add(sixButton);                 JButton sevenButton = new JButton("7");         sevenButton.addActionListener(new ActionListener() {             public void actionPerformed(ActionEvent evt) {                 pushDigit("7");             }         });         buttonPanel.add(sevenButton);                 JButton eightButton = new JButton("8");         eightButton.addActionListener(new ActionListener() {             public void actionPerformed(ActionEvent evt) {                 pushDigit("8");             }         });         buttonPanel.add(eightButton);                 JButton nineButton = new JButton("9");         nineButton.addActionListener(new ActionListener() {             public void actionPerformed(ActionEvent evt) {                 pushDigit("9");             }         });         buttonPanel.add(nineButton);         contentPane.add(buttonPanel, BorderLayout.SOUTH);                 JButton addButton = new JButton("+");         addButton.addActionListener(new ActionListener() {             public void actionPerformed(ActionEvent evt) {                 setOperation(Operation.ADD);             }         });         operationPanel.add(addButton);                 JButton subtractButton = new JButton("-");         subtractButton.addActionListener(new ActionListener() {             public void actionPerformed(ActionEvent evt) {                 setOperation(Operation.SUBTRACT);             }         });         operationPanel.add(subtractButton);                 JButton multiplyButton = new JButton("*");         multiplyButton.addActionListener(new ActionListener() {             public void actionPerformed(ActionEvent evt) {                 setOperation(Operation.MULTIPLY);             }         }); ;         operationPanel.add(multiplyButton);                 JButton divideButton = new JButton("/");         divideButton.addActionListener(new ActionListener() {             public void actionPerformed(ActionEvent evt) {                 setOperation(Operation.DIVIDE);             }         });           operationPanel.add(divideButton);                 JButton resetButton = new JButton("Reset");         resetButton.addActionListener(new ActionListener() {             public void actionPerformed(ActionEvent evt) {                 reset();             }         });           operationPanel.add(resetButton);                 JButton equalButton = new JButton("=");         equalButton.addActionListener(new ActionListener() {             public void actionPerformed(ActionEvent evt) {                 setOperation(Operation.NONE);             }         });         operationPanel.add(equalButton);                 contentPane.add(operationPanel, BorderLayout.NORTH);     }     }
commonbullet is offline
Send a message via ICQ to commonbullet Send a message via MSN to commonbullet
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 09:44.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode