0

Hi,

I'm learning java and I have a problem. The getText() doesn't seem to work. What I am trying to do is to write a simple code, where I assign the text of a button to a string variable and print that string variable on the command line. I get the error

cannot resolve symbol
Symbol: getText()
Class : java.awt.Button

import java.io.*;
import java.awt.*;
import java.event.*;
import java.*;
import java.util.*

class tests
{
     public static void main(String[] args)
     {
        Frame f = new Frame();
        Button b1 = new Button("Text");
        f.add(b1);
        f.setSize(500,500);
        f.setVisible(true);
        String x = b1.getText();
        System.out.println(x);
     }
}

Thanks,

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by TheHelp
1

Line #3 should be import java.awt.event.*;
You don't neet line #4.
Missing semicolon on line #5.
You also require import javax.swing.*;
Change your Button to JButton.

0

There is no get text method for a JButton. assign the text as a variable, and then just use that to print out, and set the text on the JButton

0

Thanks guys, the getText() method worked with the JButton. Thanks..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.