Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Help with understanding simple GUI Code

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with understanding simple GUI Code

    Hi, I am fairly new to java and was wondering if someone could comment this code for me each relevent as if to a new GUI user, my friend suggested this type of feed back could be useful so I am hoping someone could help me out in simple terms lol.

    The two files are attached, the first is basically a caller and the second has all the main code thanks.

    first

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

    second

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
      class Calculatortest extends JFrame {
     
      private JTextField textfield;
      private boolean number = true;
      private String  equalOp  = "=";
      private CalculatorOp op = new CalculatorOp();
     
      public Calculatortest()
      {
      textfield = new JTextField("0", 12);
      textfield.setHorizontalAlignment(JTextField.RIGHT);
     
     
      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);
     
      buttonPanel.add(button);
      }
      }
      ActionListener operatorListener = new OperatorListener();
      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);
     
      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);
      }
     
    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with understanding simple GUI Code

    I don't get it. You want us to write in code comments for you? These forums are for learning, and asking someone else to comment code that is not theirs stops far short of that (and in fact can be considered against forum rules). If you wrote the code, you should know what it does and should be capable of writing comments describing what it does. If you did not write the code, then I suggest asking a specific question where you do not understand the code, and make use of the API to learn about the classes and method calls.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with understanding simple GUI Code

    I am attempting to learn, on second glance It may seem quite contentious but my intentions are good, A guide in the form of commenting would go a long way in helping me out and if that is not considered learning, I appreciate the effort to tell me your opinion.

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with understanding simple GUI Code

    Also to clarify I wrote about 50% of the code myself but the finer details in the main files are really lost on me, thanks to anyone who takes the time.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with understanding simple GUI Code

    Quote Originally Posted by jordan123 View Post
    I am attempting to learn, on second glance It may seem quite contentious but my intentions are good, A guide in the form of commenting would go a long way in helping me out and if that is not considered learning, I appreciate the effort to tell me your opinion.
    In my experience (which is a lot) you will learn a considerable amount more if you write the comments - this forces you to dive head first into not only the code, but also the API, read about class and methods that are used, and to understand the syntax and nuances of the language. The easy way out is to ask someone to do it for you. We can help you along the way with any specific questions you may have.

    Learning in this manner can be compared to learning any other language (speech language) - you can have someone translate it for you but you will never learn to speak and understand until you sit down and step through the language to comprehend the words, phrases, tenses, etc...

    If it is the learning experience you wish to gain the I encourage you to write the comments yourself and if you get stuck or don't understand something, then ask.

  6. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with understanding simple GUI Code

    Ok. I have one specefic question, on 11 where it says calculatorop and its apperences as op through the code.

    what does this actually do? that seems to be the main problem for me on this. thanks

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with understanding simple GUI Code

    What do you mean 'on 11'? Do you mean line 11 (there are no line numbers in your code, so referring to it this way is hard)? And by calculatorop, do you mean CalculatorOp (java is case sensitive, as are the brains of many a programmer). Should I understand correctly, the following page might help in explaining classes and objects (aka instances of classes):
    Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)

Similar Threads

  1. Replies: 1
    Last Post: February 10th, 2012, 09:49 AM
  2. Problem understanding Java code modification
    By eagle09 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 26th, 2011, 04:13 PM
  3. HashMap : Simple Usage and Understanding
    By weakprogrammer in forum Collections and Generics
    Replies: 1
    Last Post: June 25th, 2011, 03:37 PM
  4. Need help with simple code
    By suxen in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 21st, 2011, 08:10 PM
  5. Help understanding this code
    By Zepx in forum Java Theory & Questions
    Replies: 2
    Last Post: October 20th, 2009, 10:18 AM