Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 27th, 2012, 09:52 PM
xbeeceaz xbeeceaz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 2 xbeeceaz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 27 sec
Reputation Power: 0
Swing - Need help with Booleans and if and else

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;


import javax.swing.*;

public class GUIBasedCalc extends JFrame implements ActionListener{
public GUIBasedCalc() {
super("Calculator");
setSize(200, 400);
setResizable(false);
super.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
super.setVisible(true);
int number1;
int number2;
boolean num1; //the booleans i need **HELP** with, **HELP** does final make the boolean unchangable (for ex: false to true)
boolean num2;
boolean operater;
JPanel dis = new JPanel();
JPanel sign = new JPanel();
JPanel nums = new JPanel();
JPanel nums1 = new JPanel();
JPanel nums2 = new JPanel();
GridLayout calc = new GridLayout(1, 3);
GridLayout calc1 = new GridLayout(1, 3);
GridLayout calc2 = new GridLayout(1, 3);
GridLayout sign1 = new GridLayout(1, 3);
FlowLayout lay = new FlowLayout();
setLayout(lay);
final JTextArea disply = new JTextArea(1, 8);
disply.setEditable(false);
JButton one = new JButton("1");
//Add action listener to button
one.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)
{
if (num1 == true) { //wants me to make the boolean a final, but i still need to change it **HELP**
disply.setText("1");
int number1 = 1;
this.num1 = false; // changing the boolean to false **HELP**

} else if (operater = true){
disply.setText("Please choose an operater");
if (operater = false) {
disply.setText("1");
int number2 = 2;
this.num2 = false; //trying to change boolean to false **HELP**
}
}

}
});

thats my code, please help me thanks
how i feel when it does not work

Reply With Quote
  #2  
Old June 28th, 2012, 06:40 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 29 m 42 sec
Reputation Power: 345
Please explain what the problems are. What does the code do or not do that you are having problems with? Post the full text of any error messages here.

Can you edit the post and wrap the code in code tags to preserve formatting?

Reply With Quote
  #3  
Old June 28th, 2012, 07:23 AM
leo2012 leo2012 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 50 leo2012 User rank is Private First Class (20 - 50 Reputation Level)leo2012 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 17 h 24 m 13 sec
Reputation Power: 1
If you make boolean as final you can not change the value using mathods. Default boolean value is FALSE

I did some corrections hope this help !

Code:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;


import javax.swing.*;

public class GUIBasedCalc extends JFrame implements ActionListener{
	
	private int number1 =0;
	private int number2 =0;
	private boolean num1 =true; //the booleans i need **HELP** with, **HELP** does final make the boolean unchangable (for ex: false to true)
	private boolean num2 =true; 
	private boolean operater  =true; 
	
	public GUIBasedCalc() {
		super("Calculator");
		setSize(200, 400);
		setResizable(false); 
		super.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		super.setVisible(true);
		
		JPanel dis = new JPanel(); 
		JPanel sign = new JPanel(); 
		JPanel nums = new JPanel();
		JPanel nums1 = new JPanel();
		JPanel nums2 = new JPanel();
		GridLayout calc = new GridLayout(1, 3);
		GridLayout calc1 = new GridLayout(1, 3);
		GridLayout calc2 = new GridLayout(1, 3);
		GridLayout sign1 = new GridLayout(1, 3); 
		FlowLayout lay = new FlowLayout(); 
		setLayout(lay); 
		final JTextArea disply = new JTextArea(1, 8);
		disply.setEditable(false); 
		JButton one = new JButton("1");
		//Add action listener to button
		one.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e)
			{
				if (num1 == true) { //wants me to make the boolean a final, but i still need to change it **HELP**
					disply.setText("1"); 
					number1 = 1; 
					num1 = false; // changing the boolean to false **HELP**

				} else if (operater = true){
					disply.setText("Please choose an operater"); 
					if (operater = false) {
						disply.setText("1"); 
						 number2 = 2; 
						num2 = false; //trying to change boolean to false **HELP**
					}
				}

			}
		});
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}}

Reply With Quote
  #4  
Old July 3rd, 2012, 01:45 AM
Wetmelon's Avatar
Wetmelon Wetmelon is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Location: London, ON
Posts: 322 Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 3 Days 9 h 32 m 53 sec
Reputation Power: 213
Posted this in the other thread you created (which should probably be closed):

Please post your code in code tags to preserve formatting. Also, I'm confused by your variable names. Why is "Num1" a boolean? is it a boolean or a number?

Also, this line:

Code:
else if (operater = true)
isn't doing what you think it's doing.

You're going to have to explain what you're trying to accomplish and what is occurring that is not desirable.
__________________
<Tetrad> the program I just wrote 1) compiled the first time without any errors and 2) worked like it was supposed to
<Tetrad> I don't know whether to be proud or scared to death
Quote:
Originally Posted by DaWei_M
That covers a multitude of your sins, right there.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Swing - Need help with Booleans and if and else

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap