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 3 of 3

Thread: First real program help

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default First real program help

    First i'll say this: this code is probably the most innefficiant way of doing it, because I have been using the knowledge I've learned from about 16 total video tutorials and answers of other peoples questions on many different websites. I have tried to add comments wherever I can to show what I'm doing but in the last part even I don't know whats happening. The program is supposed to be a very basic window with 2 text fields to take imputs and add them together, but it has turned out to be more than I can handle. the main problem(and I think the only one) is that I need some of the objects to be final but I also need to change them.
    Here's the code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class Stuff {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    			//Make all of the objects.
    			JLabel enterFirst = new JLabel("First Number:");
    			JLabel enterSecond = new JLabel("+  Second Number");
    			JTextField firstNum = new JTextField("Enter a number");
    			JTextField secondNum = new JTextField("Enter a number");
    			JButton add = new JButton("Add");
    			JTextField sumField = new JTextField("Sum");
    			//set dimensions for them if you don't want to do them by hand.
    			Dimension size1 = enterFirst.getPreferredSize();
    			Dimension size2 = enterSecond.getPreferredSize();
    			Dimension size3 = firstNum.getPreferredSize();
    			Dimension size4 = secondNum.getPreferredSize();
    			Dimension size5 = add.getPreferredSize();
    			Dimension size6 = sumField.getPreferredSize();
    			//Make a JFrame.
    			JFrame x = new JFrame();
    			x.setLayout(null);
    			x.setVisible(true);
    			x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			x.setSize(600,350);
    			x.setTitle("Calculator");
    			x.setBackground(Color.GRAY);
    			//Add the objects.
    			x.add(enterFirst);
    			x.add(firstNum);
    			x.add(enterSecond);
    			x.add(secondNum);
    			x.add(add);
    			x.add(sumField);
    			//Set the objects positions.
    			enterFirst.setBounds(100, 20, size1.width, size1.height);
    			enterSecond.setBounds(300, 20, size2.width, size2.height);
    			firstNum.setBounds(200, 20, size3.width, size3.height);
    			secondNum.setBounds(420, 20, size4.width, size4.height);
    			add.setBounds(250, 45, size5.width, size5.height);
    			sumField.setBounds(320, 45, size6.width, size6.height);
    			//Set the sumField to be uneditable.
    			sumField.setEditable(false);
    			//Add the ActionListener.
    			ActionListener action1 = new ActionListener() {
    				public void actionPerformed(ActionEvent e) {
    					if (e.getSource() == add) {
    						String sumNum = new String("");
    						int sum = Integer.valueOf(firstNum.getText()) + Integer.valueOf(secondNum.getText());
    						sumNum = Integer.toString(sum);
    						sumField.setText(sumNum);
    					}
     
    				}
    			};
    			}
     
    	}
    the only problem that eclipse is having with it right now is that add, firstNum and secondNum need to be final to be called on in the last part, but they also need to change.
    the errors:"Cannot refer to non final variable add inside an inner class defined in a different method."
    "Cannot refer to non final variable firstNum inside an inner class defined in a different method."
    "Cannot refer to non final variable secondNum inside an inner class defined in a different method."

    PS: If this is completely wrong, which it might be, just tell me, I just want to know how to fix it, if I can.
    EDIT: In the probable case that I am doing this completely wrong, I would appreciate any links you might have that could help melearn this stuff better. Although I have dug through google looking for answers to may problems, I fell that an actuall tutorial on the stuff I need would be more useful.
    Last edited by CoolYoungHipKiddo; February 15th, 2012 at 03:19 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: First real program help

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    Please copy and paste the full text of the error messages here.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: First real program help

    sorry, I fixed it.
    EDIT:I've been thinking about the last part with the actionlistener, and I've revised it a bit, but it still doesn't work:
    ActionListener action1 = new ActionListener() {
    				public void actionPerformed(ActionEvent e) {
    					Boolean addStart = true;
     
    				}
    			};
    			if(addStart == true){
    				String sumNum = new String("");
    				int sum = Integer.valueOf(firstNum.getText()) + Integer.valueOf(secondNum.getText());
    				sumNum = Integer.toString(sum);
    				sumField.setText(sumNum);
    			}
    The only problem I can see with this is that
    a) I can't use the boolian variable anyway because it says:"addStart cannot be resolved to a variable."
    and
    b)Even if it did work, The program would have to restart each time you want to add numbers.
    EDITx2: I have trimmed out the button, there are no error messages on eclipse anymore. The only problem is that it still doesn't work. I think the proble might be that sumField is not updateing, but I know basically nothing about java, so I'll just post the code:
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class Stuff {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    			//Make all of the objects.
    			JLabel enterFirst = new JLabel("First Number:");
    			JLabel enterSecond = new JLabel("+  Second Number");
    			JTextField firstNum = new JTextField("0", 5);
    			JTextField secondNum = new JTextField("0", 5);
    			JTextField sumField = new JTextField("0", 10);
    			//set dimensions for them if you don't want to do them by hand.
    			Dimension size1 = enterFirst.getPreferredSize();
    			Dimension size2 = enterSecond.getPreferredSize();
    			Dimension size3 = firstNum.getPreferredSize();
    			Dimension size4 = secondNum.getPreferredSize();
    			Dimension size6 = sumField.getPreferredSize();
    			//Make a JFrame.
    			JFrame x = new JFrame();
    			x.setLayout(null);
    			x.setVisible(true);
    			x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			x.setSize(600,350);
    			x.setTitle("Calculator");
    			x.setBackground(Color.GRAY);
    			//Add the objects.
    			x.add(enterFirst);
    			x.add(firstNum);
    			x.add(enterSecond);
    			x.add(secondNum);
    			x.add(sumField);
    			//Set the objects positions.
    			enterFirst.setBounds(100, 20, size1.width, size1.height);
    			enterSecond.setBounds(300, 20, size2.width, size2.height);
    			firstNum.setBounds(200, 20, size3.width, size3.height);
    			secondNum.setBounds(420, 20, size4.width, size4.height);
    			sumField.setBounds(320, 45, size6.width, size6.height);
    			//Set the sumField to be uneditable.
    			sumField.setEditable(false);
    			String sumNum = new String("");
    			int sum = Integer.valueOf(firstNum.getText()) + Integer.valueOf(secondNum.getText());
    			sumNum = Integer.toString(sum);
    			sumField.setText(sumNum);
    			}
     
     
    	}
    Last edited by CoolYoungHipKiddo; February 15th, 2012 at 04:32 PM.

Similar Threads

  1. Functions, real numbers and for loops
    By grimhildr in forum Loops & Control Statements
    Replies: 1
    Last Post: October 15th, 2011, 07:11 AM
  2. Mapping real coordinates into GUI
    By gloor in forum AWT / Java Swing
    Replies: 2
    Last Post: May 26th, 2011, 08:35 AM
  3. Testing if dates are real or not.
    By cardsfan33 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2011, 07:46 PM
  4. Storing data from a socket to a file in real time
    By colossusdub in forum Java Networking
    Replies: 0
    Last Post: March 2nd, 2010, 09:10 AM
  5. Changing colors of large image in real time
    By chals in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: May 7th, 2009, 05:06 AM

Tags for this Thread