-2

I want to create a JFrame with a JLabel on it reading "Simple Text". But not in the usual way. I want to do it in such a way that JFrame stands alone as one class in one tab, and the Jlabel in another tab. How can I do it? I did it as shown below, but the text didn’t appear on the window.

public class FirstClass {

    public static void main (String [] args) {

        SecondClass sc = new SecondClass ();
        ThirdClass tc = new ThirdClass ();

        sc.newWindow ();
        tc.newText ();
    }
}

import javax.swing.JFrame;

public class SecondClass {

    public void newWindow () {
        JFrame jf = new JFrame ("Text Window");
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(500, 500);
    }
}

import javax.swing.JLabel;

public class ThirdClass {

    public void newText (){
        JLabel jl = new JLabel ("Simple Text");
        jl.setVisible(true);
        jl.setBounds(50, 50, 50, 50);
        add (jl);
    }
}
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
  • 1
    This seems like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) why do you want to do that? Is it possible? Yes, but really strange... – Frakcool Jun 28 '17 at 17:33
  • Probably, it is. But i don't know how)) – Bəxtiyar Əli Ağayar Jun 28 '17 at 17:34
  • 1
    *"Probably, it is"*, well you haven't even clicked the link I provided so you only "guess", You didn't answer my question, *"why do you want to do that?"*. Please take the [tour] and read [ask] – Frakcool Jun 28 '17 at 17:36
  • I just want to be able to create a program using multiple classes. – Bəxtiyar Əli Ağayar Jun 28 '17 at 17:38
  • and link them of course, that's what the problem is – Bəxtiyar Əli Ağayar Jun 28 '17 at 17:38
  • That's not how you create a program with multiple classes, I would read about the [MVC pattern](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) and apply it in your program. If you do it like you're doing right now, for a simple login view you would need: 1 frame / dialog class, 1 class for `JTextField` for user and one for `JPasswordField`, two more for the `JLabel`s and at least one more for the "OK / Login" `JButton`, that's 5 classes that could be better in one class for the view, one class for the `User` model which contains the `userName` and `password` fields... – Frakcool Jun 28 '17 at 17:43
  • ... all of that without thinking in the login process or the `ActionListener` for your `JButton`, now, imagine how many classes would you need for a complex GUI! Also I see you're using `setBounds(...)`, that suggests you're using (or will use) a `null` layout, which is [evil](http://www.fredosaurus.com/notes-java/GUI/layouts/nulllayout.html) and [frowned upon](https://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) to use it and use a [layout manager](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) (or combinations of them) instead – Frakcool Jun 28 '17 at 17:46

1 Answers1

0

I believe you need to actually add the JLabel to the JFrame, as it stands now you just have an independent JFrame and an independent JLabel. In the class where you are creating the JLabel, you want to return that like...

public JLabel newText (){
    JLabel jl = new JLabel ("Simple Text");
    jl.setVisible(true);
    jl.setBounds(50, 50, 50, 50);
    return jl;
}

Then same with JFrame

import javax.swing.JFrame;

public class SecondClass {

public JFrame newWindow () {
    JFrame jf = new JFrame ("Text Window");
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setSize(500, 500);
    return jf;
}

Then in your third class where you connect them, add the JLabel to the JFrame.

public class FirstClass {

public static void main (String [] args) {

    SecondClass sc = new SecondClass ();
    ThirdClass tc = new ThirdClass ();

    JFrame frame = sc.newWindow ();
    JLabel label = tc.newText ();
    frame.add(label);
}

}

Though as someone said in the comments, this is a really roundabout way to do this that's a little unnecessary. Though I also have not used the Java GUI JFrame/JLabel etc in a while so I can't confirm that all that is working.

Carson
  • 1,147
  • 5
  • 19
  • 41
  • now I'll check if your correction works)) if does, thank you very much indeed. – Bəxtiyar Əli Ağayar Jun 28 '17 at 17:42
  • Yup, the general idea is JFrame and JLabel should just be objects that you can pass between classes like that. Presuming you imported the classes and all properly so they can communicate. Something you should see over and over with Java as you go deeper with it. – Carson Jun 28 '17 at 17:47
  • oh yes it worked. before now I just forget to import library)) hahaha. I'm very happy, thank you very much Carson I'm very grateful to you. – Bəxtiyar Əli Ağayar Jun 28 '17 at 18:18