Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 544 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.8K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.5K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 439 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Button Text into Label Text

866915
Member Posts: 3
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!
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!
Answers
-
see the three labels objLabel1 and other two.
you haven't did
objLabel1 = new Label("");
thats why not initialized error. Instantiating.........
Edited by: Vinayjgeorge on Jun 6, 2011 8:32 PM -
Hi
I just went through the complete code.
* You can use one label and set its text on each press.
* I didn't see action listener with your code.
* use this for buttons to add action listener so that it can detect the button press.
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
objLabel1 = new Label("B"); // this will set the text to label.
}
});
Edited by: Vinayjgeorge on Jun 6, 2011 8:55 PM
This discussion has been closed.