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

Thread: Java GUI button calculator

  1. #1
    Junior Member
    Join Date
    Aug 2021
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java GUI button calculator

    Ok, I have used window in eclipse workspace to build a basic calculator GUI,

    I then have the buttons i want to now add onclick events or however you say that in JAVA that when i click it displays for button 1 or bt1 the variable n1 =1 to display in the text field 1.
    then on next click 11 then 111
    \after that is stored in a variable i would like the then hit say the add button and then add the value of that int.

    however im already stuck have the gui sorted but when i try to add variable inside the onlick event i get the error

    enclosing scope must be final


    protected void createContents() {
    		shell = new Shell();
    		shell.setSize(260, 260);
    		shell.setText("SWT Application");
     
    		input = new Text(shell, SWT.BORDER);
    		input.setText("input");
    		input.setBounds(10, 10, 224, 21);
     
    		int n1 = 1;		
    		Button bt1 = new Button(shell, SWT.NONE);
    		bt1.addSelectionListener(new SelectionAdapter() {
    			@Override
    			public void widgetSelected(SelectionEvent e) {
    			if(n1 == 1){
    				input.setText(String.valueOf(n1));
    				n1++;
    			}
    			}
    		});
    Last edited by scriptKing; August 6th, 2021 at 07:55 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java GUI button calculator

    want to add to number 1 button 1 then +10
    Sorry, I do not understand your problem. Can you try explaining it again in a different way and perhaps with an example.
    If button 1 is bt1 then say that, so there is no confusion.
    What does it mean to add to a button? Are you talking about changing the text displayed in the button?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    --- Update ---

    the error

    enclosing scope must be final
    What statement is that error on?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2021
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java GUI button calculator

    it is on
    n1++;

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java GUI button calculator

    n1 is a local variable in the createContents method. It goes out of scope when execution leaves the method.
    Can the variable declaration be moved to the class level so that it stays in scope?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2021
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java GUI button calculator

    Hi Norm,

    Thanks for your help with this,

    I have ended up going

    	protected void createContents() {
    		shell = new Shell();
    		shell.setSize(260, 260);
    		shell.setText("SWT Application");
     
    		input = new Text(shell, SWT.BORDER | SWT.RIGHT);
    		input.setBounds(10, 10, 224, 21);
     
    		Button bt1 = new Button(shell, SWT.NONE);
    		bt1.addSelectionListener(new SelectionAdapter() {
    			@Override
    			public void widgetSelected(SelectionEvent e) {
    				String enterNumber = input.getText() + bt1.getText();
    				input.setText(enterNumber);
    			}
    		});

    i have a feeling this is showing a string so wil need to turn it into and integer maybe anyway not really what i was original intending but working now.

Similar Threads

  1. Calculator double zero button issue.
    By mtahirkn in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 27th, 2021, 06:29 AM
  2. Replies: 2
    Last Post: March 29th, 2019, 03:26 AM
  3. Replies: 1
    Last Post: March 30th, 2014, 03:56 AM
  4. Replies: 6
    Last Post: March 8th, 2014, 10:08 AM
  5. GUI Calculator: Adding Answer Button? Please Help
    By Staticity in forum What's Wrong With My Code?
    Replies: 29
    Last Post: July 27th, 2011, 03:18 PM