-1

I am a beginner at Java and coding. I don't know if this problem is occurring because I forgot something in the code or something is not correct in the code.

I have already tried looking through all the questions on Stack Overflow that are similar to my question and none of them helped me. I've been doing trial and error but still cannot fix it.

import javax.swing.JFrame;
import javax.swing.JLabel;

public class JLabel {

    public static void main(String args[]) {
        JFrame myFrame = new JFrame();
        String myTitle = "Blank Frame";
        JLabel label1 = new JLabel("Test");
     `````
        label1.setText("Test Text");
     `````
        myFrame.setTitle(myTitle);
        myFrame.setTitle(900,600);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setVisible(true);
    }
}

At 'label1.setText("Test Text");' is where the problem is. My goal for this code is to create a window that has some text in it. Hopefully, the fix is simple and not as complicated as many codes.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Promediz
  • 1
  • 2
  • 2
    I am unable to reproduce the issue you have documented locally. However, mine fails to compile on `myFrame.setTitle(900,600);` – Compass Aug 09 '19 at 15:05

3 Answers3

1

You need to rename your class, as that's an already existing class that you're using. Creating a LJabel object will now create an instance of your object rather than the java.swing equivalent.

You didn't add the label to the frame.

myFrame.add(label1);

You're also calling setTitle() twice when I believe you meant to call setSize().

This code works for me :

public static void main(String[] args) {
    JFrame myFrame = new JFrame();
    String myTitle = "Blank Frame";
    JLabel label1 = new JLabel("Test");
    label1.setText("Test Text");
    myFrame.add(label1);
    myFrame.setTitle(myTitle);
    myFrame.setSize(900,600);
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.setVisible(true);
}
joanis
  • 10,635
  • 14
  • 30
  • 40
ejdebruin
  • 51
  • 4
0

remove the ````` things and you are fine... or are these just for the example?

Phash
  • 428
  • 2
  • 7
  • the ````` things are to provide which line is needing the fix – Promediz Aug 09 '19 at 15:07
  • provide a real example, without compile-errors. You said, you are a beginner, so the false characters are an error. Remove all lines, and add them one by one. When does the error occur? – Phash Aug 09 '19 at 15:12
0

This message tells you that the method setText is not define in your defined class JLabel which is not equivalent of JLabel class available in Swing packages. Rename your own defined class JLabel to JLabelTest for example. Also, remove this line myFrame.setTitle(900,600);, the definition of setTitle doesn't allow those parameters, and replace it with myFrame.setSize(900,600); Your complete code should be:

import javax.swing.JFrame;
import javax.swing.JLabel;

public class JLabelTest {

    public static void main(String args[]) {
        JFrame myFrame = new JFrame();
        String myTitle = "Blank Frame";
        JLabel label1 = new JLabel("Test");

        label1.setText("Test Text");

        myFrame.setTitle(myTitle);
        myFrame.setSize(900,600);
        myFrame.getContentPane().add(label1); // to display the label
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setVisible(true);
    }
}

Just an personal advise; try to read very carefully your tutorial and understand how Swing works step by step.

Hope this will help you.

Harry Coder
  • 2,429
  • 2
  • 28
  • 32