Results 1 to 9 of 9
Thread: HELP!!! please
- 08-17-2012, 12:58 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 2
- Rep Power
- 0
HELP!!! please
The applet program is suppose to have three buttons that when clicked, three textboxs appear with the same name as the button.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.String;
public class MouseClick
{
public static void main(String[] args)
{
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);
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);
}
}
}
-
Re: HELP!!! please
What is your question, where exactly are you stuck? If you can ask a specific question, we'll be much better able to help you.
-
Re: HELP!!! please
there aren't any textboxes. there are a Button, 2 JButtons, a JFrame, a JPanel, + 3 Labels
-
Re: HELP!!! please
Suggestions:
- Don't mix awt and Swing components. Stick with just Swing components such as JButtons, JLabels, and JTextFields.
- Use ActionListeners with JButtons, not MouseListeners. ActionListeners are built to work extremely well with buttons and are harder to mess up.
- Avoid absolute positioning but instead use layout managers to help you position components. The tutorials will help you figure out how to do this.
- After displaying new components or removing components from a container, call revalidate() and repaint() on that container to display the changes.
- Go through the standard Java Swing tutorials as they will explain all this and a lot better than I can.
- Please try to make your question headings here more informative. "Help please" tells us nothing about your problem. A more informative title would be something like, "help displaying components in a GUI" or something similar. This would clue GUI experts to look at your thread. Your title will have the opposite effect.
Much luck!
- 08-17-2012, 05:37 AM #5
Member
- Join Date
- Aug 2012
- Posts
- 2
- Rep Power
- 0
- 08-17-2012, 07:43 AM #6
Re: HELP!!! please
Forum Rules -- especially the third paragraph
Guide For New Members
BB Code List - Java Programming Forum
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-17-2012, 07:44 AM #7
- 08-17-2012, 03:36 PM #8
Re: HELP!!! please
So, you have no idea why this program is not working so you have placed the whole thing here. Also, PLEASE place [code] tags around your code. makes it much easier to see and read and allows us to help you.
Also, PLEASE follow the instructions made by Fubarable. He read through your entire program and gave you great advice on how to fix your program. Lastly, besides actually removing components, you could use setVisible(false); on them so that they disappear. All components inhereit thier powers from javax.swing.JComponent so all swing components can set their visibility. java.awt.Component, cannot. This is why one should not be using AWT. Just does not have those great features in there. Now, if you DO want to remove them, in which case, follow Fubarable.My API:Java Code:cat > a.out || cat > main.class
- 08-17-2012, 03:46 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17


1Likes
LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks