Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Button Text into Label text

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Button Text into Label text

    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!
    Last edited by KevinWorkman; June 6th, 2011 at 10:12 AM. Reason: added highlight tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Button Text into Label text

    Don't forget highlight tags when posting code.

    Did you google that error?

    Hint: Why are you re-declaring objLabel1, 2 and 3? What value do they have when you declare them in your mouseClicked() method?

    And again, why are you mixing Swing and Awt? Why are you using Labels instead of JLabels?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Text file to text area and Radiobuttons?
    By donaldmax in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 27th, 2011, 04:45 AM
  2. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  3. Replies: 3
    Last Post: February 21st, 2011, 07:25 PM
  4. Can't set label text from a Jbutton
    By VBGuy in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 8th, 2010, 10:55 AM
  5. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM

Tags for this Thread