7 Replies - 294 Views - Last Post: 18 August 2012 - 07:44 AM Rate Topic: -----

#1 keekeee14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-August 12

making a button and a label

Posted 16 August 2012 - 09:22 PM

this program is suppose to have a button and a label with the same name. After clicking each of the three buttons, a label with the same name pops up. The errors says that the labels are undeclared. why?




import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.String;
 
public class MouseClick
{


public static void main(String[] args)
{
//these strings will be the name of the buttons and labels
String a = new String(" who?");
String b = new String("what?");
String c = new String("when?");
//declaring the labels
Label objLabel1 = new Label(a);
objLabel1.setBounds(20,75,40,40);
Label objLabel2 = new Label(B)/>;
objLabel2.setBounds(85,120,40,40);
Label objLabel3 = new Label(c);
objLabel3.setBounds(130,185,40,40);
MouseClick MC= new MouseClick();
}
 
public MouseClick()
{
//frame declared
JFrame h= new JFrame("Button and labels");
//panel declared
final JPanel d = new JPanel();
//buttons declared
Button e= new Button(a);
e.setBounds(20,30,40,40);//their sizes
JButton f= new JButton(B)/>;
f.setBounds(85,75, 40, 40);
JButton g = new JButton(c);
g.setBounds(130, 120, 40, 40);
d.add(e);//add the buttons to the panel
d.add(f);
d.add(g);
h.getContentPane().add(d);//and the panel to the frame
 

d.add(objLabel1);//adds the labels to the panel. don't want them to be seen yet.
objLabel1.setVisible(false);
d.add(objLabel2);
objLabel2.setVisible(false);
d.add(objLabel3);
objLabel3.setVisible(false);
h.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
h.setSize(400,400);//frame settings
h.setVisible(true);
}
 
public class MyMouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent me)
{
objLabel1.setVisible(true);//after the mouse clicks, the labels become visible.
objLabel2.setVisible(true);
objLabel3.setVisible(true);
}
}
}



*Edited please :code:

This post has been edited by pbl: 17 August 2012 - 06:01 AM
Reason for edit:: Code tags added


Is This A Good Question/Topic? 0
  • +

Replies To: making a button and a label

#2 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: making a button and a label

Posted 16 August 2012 - 09:34 PM

please :code:

secondly, take a look at this tutorial to help you with your errors.

You haven't defined the objLabel1, 2, and 3 in your mouseClicked method. you need to move the definitions of those labels to member/class variable spot.
Was This Post Helpful? 0
  • +
  • -

#3 keekeee14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-August 12

Re: making a button and a label

Posted 17 August 2012 - 12:08 AM

View PostSwiftStriker00, on 16 August 2012 - 09:34 PM, said:

please :code:

secondly, take a look at this tutorial to help you with your errors.

You haven't defined the objLabel1, 2, and 3 in your mouseClicked method. you need to move the definitions of those labels to member/class variable spot.

I am very new at this. What do you mean by member/class? which class? if i put it in the second class, would it be undeclared in the first?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2001
  • View blog
  • Posts: 4,868
  • Joined: 10-September 10

Re: making a button and a label

Posted 17 August 2012 - 03:06 AM

Did you review the recommended tutorial? If so, you should comment on what you did/didn't find helpful in it to solve your problem.

In this construction:
	public static void main(String[] args)
	{
		//these strings will be the name of the buttons and labels
		String a = new String(" who?");
		String b = new String("what?");
		String c = new String("when?");
		//declaring the labels
		Label objLabel1 = new Label(a);
		objLabel1.setBounds(20,75,40,40);
		Label objLabel2 = new Label(B)/>;
		objLabel2.setBounds(85,120,40,40);
		Label objLabel3 = new Label(c);
		objLabel3.setBounds(130,185,40,40);
		MouseClick MC= new MouseClick();
	}

all variables declared in the main() method are local variables to the main() method. The variables local to main() include: a, b, c, the 3 Labels and the MouseClick instance. Being local to main(), the scope of those variables is the main() method. In other words, those variables are only visible to or known by the main() method.

The terms I've used; local variable, scope, and visibility; are basic programming terms that you should know. If you don't, look them up.

Quote

What do you mean by member/class? If i put it in the second class, would it be undeclared in the first?

A better understanding of variable terms and concepts would help you answer this question. Here's the Java tutorial on basic variable concepts and terminology. And you should review this tutorial for a slightly more advanced look at variables in an OOP context.

And give your variables better names. You should not have single letter variable names except as array indices or loop control variables.

Good luck!
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,309
  • Joined: 06-March 08

Re: making a button and a label

Posted 17 August 2012 - 06:02 AM

Please:
- :code:
- indent your code properly
- use meaningful variable names

This post has been edited by pbl: 17 August 2012 - 06:02 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#6 keekeee14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-August 12

Re: making a button and a label

Posted 17 August 2012 - 08:00 AM

I understand the variables. I moved the variables outside the main.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.String;
 
public class MouseClick
{
String a = new String(" who?");
String b = new String("what?");
String c = new String("when?");
Label objLabel1 = new Label(a);
objLabel1.setBounds(20,75,40,40);
Label objLabel2 = new Label(B)/>;
objLabel2.setBounds(85,120,40,40);
Label objLabel3 = new Label(c);
objLabel3.setBounds(130,185,40,40);

public static void main(String[] args)
{
MouseClick MC= new MouseClick();
}
 
public MouseClick()
{
JFrame h= new JFrame("Button and labels");
final JPanel d = new JPanel();
Button e= new Button(a);
e.setBounds(20,30,40,40);
JButton f= new JButton(B)/>;
f.setBounds(85,75, 40, 40);
JButton g = new JButton(c);
g.setBounds(130, 120, 40, 40);
d.add(e);
d.add(f);
d.add(g);
h.getContentPane().add(d);
 

d.add(objLabel1);
objLabel1.setVisible(false);
d.add(objLabel2);
objLabel2.setVisible(false);
d.add(objLabel3);
objLabel3.setVisible(false);
h.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
h.setSize(400,400);
h.setVisible(true);
}
 
public class MyMouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent me)
{

objLabel1.setVisible(true);
objLabel2.setVisible(true);
objLabel3.setVisible(true);
}
}
}

is there something wrong with my numbers?
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2001
  • View blog
  • Posts: 4,868
  • Joined: 10-September 10

Re: making a button and a label

Posted 17 August 2012 - 08:07 AM

Quote

is there something wrong with my numbers?

Can you be more specific?

These lines:

objLabel1.setBounds(20,75,40,40);
objLabel2.setBounds(85,120,40,40);
objLabel3.setBounds(130,185,40,40);

cannot exist outside of a method or initialization block, but put them in a method.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,309
  • Joined: 06-March 08

Re: making a button and a label

Posted 18 August 2012 - 07:44 AM

View PostGregBrannon, on 17 August 2012 - 11:07 AM, said:

but put them in a method.

or in your constructor ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1