Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Button Text into Label Text

866915Jun 6 2011 — edited Jun 6 2011
Write a program that has three buttons, each displaying a different text that when pressed will display the text on the button in a text box. That was my task, and here is my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.String;

public class MouseClick
{
Label objLabel1;
Label objLabel2;
Label objLabel3;
public static void main(String[] args)
{
MouseClick MC= new MouseClick();
}

public MouseClick()
{
JFrame f = new JFrame("JFrame");
final JPanel p1 = new JPanel();
Button a = new Button("A");
a.setBounds(20,30,40,40);
JButton b = new JButton("B");
b.setBounds(85,75, 40, 40);
JButton c = new JButton("C");
c.setBounds(130, 120, 40, 40);
p1.add(a);
p1.add(b);
p1.add(c);
f.getContentPane().add(p1);

objLabel1 = new Label("A");
objLabel1.setBounds(20,75,40,40);
objLabel2 = new Label("B");
objLabel2.setBounds(85,120,40,40);
objLabel3 = new Label("C");
objLabel3.setBounds(130,185,40,40);
p1.add(objLabel1);
objLabel1.setVisible(false);
p1.add(objLabel2);
objLabel2.setVisible(false);
p1.add(objLabel3);
objLabel3.setVisible(false);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
f.setSize(400,400);
f.setVisible(true);
}

public class MyMouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent me)
{
//String stra = me.getActionCommand ("A");
//objLabel1.setText (stra);
//String strb = me.getActionCommand ("B");
//objLabel1.setText (strb);
//String strc = me.getActionCommand ("C");
//objLabel1.setText (strc);
Label objLabel1;
Label objLabel2;
Label objLabel3;
objLabel1.setVisible(true);
objLabel2.setVisible(true);
objLabel3.setVisible(true);
}
}
}


I originally used Strings, but it wasn't working. right now the error i'm getting is that the objLabels havent been initialized. Help? thanks!

Comments

Processing
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 4 2011
Added on Jun 6 2011
2 comments
524 views