AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Off-Topic (https://forums.alliedmods.net/forumdisplay.php?f=15)
-   -   Java help (https://forums.alliedmods.net/showthread.php?t=29815)

Lord_Destros 06-13-2006 08:56

Java help
 
I know there are probably many problems with this code as well as the way I did it, but the main thing I can't figure out to do is set more than one digit in the Text area when pushing buttons (if i push 1, then 2, then 3, 3 appears instead of 123).

Obviously this is a calculator program...........

I tried making it so that each time someone pushes a button, it adds that number to a string which it then displays.

Yes, I know the operation buttons do nothing and the calculator layout itself is pretty crappy.


Code:
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class CalculatorEdit extends JFrame implements ActionListener {     public static final int WIDTH = 600;     public static final int HEIGHT = 200;     private JTextField inputOutputField;         public static void main(String[] args)     {         Calculator guiWithPanel = new Calculator();         guiWithPanel.setVisible(true);     }     public CalculatorEdit()     {         inputOutputField = new JTextField("0", 30);         setSize(WIDTH, HEIGHT);         addWindowListener(new WindowDestroyer());         setTitle("Gabens Miniature Calculator");         Container contentPane = getContentPane();         JPanel operationPanel = new JPanel();         JPanel buttonPanel = new JPanel();         contentPane.setLayout(new BorderLayout());                 JButton zeroButton = new JButton("0");         zeroButton.addActionListener(this);         buttonPanel.add(zeroButton);                 JButton oneButton = new JButton("1");         oneButton.addActionListener(this);         buttonPanel.add(oneButton);         JButton twoButton = new JButton("2");         twoButton.addActionListener(this);         buttonPanel.add(twoButton);                 JButton threeButton = new JButton("3");         threeButton.addActionListener(this);         buttonPanel.add(threeButton);                 JButton fourButton = new JButton("4");         fourButton.addActionListener(this);         buttonPanel.add(fourButton);                 JButton fiveButton = new JButton("5");         fiveButton.addActionListener(this);         buttonPanel.add(fiveButton);                 JButton sixButton = new JButton("6");         sixButton.addActionListener(this);         buttonPanel.add(sixButton);                 JButton sevenButton = new JButton("7");         sevenButton.addActionListener(this);         buttonPanel.add(sevenButton);                 JButton eightButton = new JButton("8");         eightButton.addActionListener(this);         buttonPanel.add(eightButton);                 JButton nineButton = new JButton("9");         nineButton.addActionListener(this);         buttonPanel.add(nineButton);         contentPane.add(buttonPanel, BorderLayout.SOUTH);                 JButton addButton = new JButton("+");         addButton.addActionListener(this);         operationPanel.add(addButton);                 JButton subtractButton = new JButton("-");         subtractButton.addActionListener(this);         operationPanel.add(subtractButton);                 JButton multiplyButton = new JButton("*");         multiplyButton.addActionListener(this);         operationPanel.add(multiplyButton);                 JButton divideButton = new JButton("/");         divideButton.addActionListener(this);         operationPanel.add(divideButton);                 JButton resetButton = new JButton("Reset");         resetButton.addActionListener(this);         operationPanel.add(resetButton);                 JButton equalButton = new JButton("=");         equalButton.addActionListener(this);         operationPanel.add(equalButton);                 contentPane.add(operationPanel, BorderLayout.NORTH);     }     public void actionPerformed(ActionEvent e)     {        Container contentPane = getContentPane();         String a = "";         String b = "";         int answer = 0;         double operation = 0;         if (e.getActionCommand().equals("0"))         {             if (operation == 0)             {                 a = a + "0";                 inputOutputField.setText(a);             }             else             {                 b = b + "0";                 inputOutputField.setText(b);             }         }         else if (e.getActionCommand().equals("1"))         {            if (operation == 0)             {                 a = a + "1";                 inputOutputField.setText(a);             }             else             {                 b = b + "1";                 inputOutputField.setText(b);             }         }         else if (e.getActionCommand().equals("2"))         {            if (operation == 0)             {                 a = a + "2";                 inputOutputField.setText(a);             }             else             {                 b = b + "2";                 inputOutputField.setText(b);             }         }         else if (e.getActionCommand().equals("3"))         {            if (operation == 0)             {                 a = a + "3";                 inputOutputField.setText(a);             }             else             {                 b = b + "3";                 inputOutputField.setText(b);             }         }         else if (e.getActionCommand().equals("4"))         {            if (operation == 0)             {                 a = a + "4";                 inputOutputField.setText(a);             }             else             {                 b = b + "4";                 inputOutputField.setText(b);             }         }         else if (e.getActionCommand().equals("5"))         {            if (operation == 0)             {                 a = a + "5";                 inputOutputField.setText(a);             }             else             {                 b = b + "5";                 inputOutputField.setText(b);             }         }         else if (e.getActionCommand().equals("6"))         {            if (operation == 0)             {                 a = a + "6";                 inputOutputField.setText(a);             }             else             {                 b = b + "6";                 inputOutputField.setText(b);             }         }         else if (e.getActionCommand().equals("7"))         {            if (operation == 0)             {                 a = a + "7";                 inputOutputField.setText(a);             }             else             {                 b = b + "7";                 inputOutputField.setText(b);             }         }         else if (e.getActionCommand().equals("8"))         {            if (operation == 0)             {                 a = a + "8";                 inputOutputField.setText(a);             }             else             {                 b = b + "8";                 inputOutputField.setText(b);             }         }                 else if (e.getActionCommand().equals("9"))         {            if (operation == 0)             {                 a = a + "9";                 inputOutputField.setText(a);             }             else             {                 b = b + "9";                 inputOutputField.setText(b);             }         }                 else if (e.getActionCommand().equals("+"))         {             operation = 1;         }         else if (e.getActionCommand().equals("-"))         {             operation = 2;         }         else if (e.getActionCommand().equals("*"))         {             operation = 3;         }         else if (e.getActionCommand().equals("/"))         {             operation = 4;         }         else if (e.getActionCommand().equals("="))         {             if (operation == 1);             {                 answer = (Integer.parseInt(a) + Integer.parseInt(b));                 inputOutputField.setText("" + answer);             }                         if (operation == 2);             {                 answer = (Integer.parseInt(a) - Integer.parseInt(b));                 inputOutputField.setText("" + answer);             }             if (operation == 3);             {                 answer = (Integer.parseInt(a) * Integer.parseInt(b));                 inputOutputField.setText("" + answer);             }             if (operation == 4);             {                 answer = (Integer.parseInt(a) / Integer.parseInt(b));                 inputOutputField.setText("" + answer);             }         }         else if (e.getActionCommand().equals("Reset"))         {             operation = 0;             a = "";             b = "";             inputOutputField.setText("");         }         else             System.out.println("Error in button interface.");     } }

knekter 06-13-2006 15:29

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?

knekter 06-13-2006 15:32

You realize that you are reseting the a and b strings to nothing each time something is pressed.... thats your problem.

Lord_Destros 06-13-2006 22:22

Quote:

Originally Posted by knekter
You realize that you are reseting the a and b strings to nothing each time something is pressed.... thats your problem.

Wait, where?

Also, yes I use JCreator.

commonbullet 06-13-2006 22:25

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);     }     }

knekter 06-14-2006 11:16

I never use Java or J# anymore, I like you love C#. I was just saying that if you do wish to use Java might as well make it J#. Easier for making apps and games in my opinion.
Code:
public void actionPerformed(ActionEvent e) {     Container contentPane = getContentPane();     String a = "";     String b = ""; }
That is where you reset the variables and why the text box keeps clearing.

Lord_Destros 06-18-2006 21:55

Quote:

Originally Posted by knekter
I never use Java or J# anymore, I like you love C#. I was just saying that if you do wish to use Java might as well make it J#. Easier for making apps and games in my opinion.
Code:
public void actionPerformed(ActionEvent e) {     Container contentPane = getContentPane();     String a = "";     String b = ""; }
That is where you reset the variables and why the text box keeps clearing.

Ya thx, +karma for all! Got it working now :D.


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

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