Lia
Lia

Reputation: 45

How To Add JLabel to Already Existing Jframe?

lol i dont even know if i worded that right

i am a really new programmer and this is my code for the main class:

package culminatingnew;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane; 
import javax.swing.JTextField;


 public class CulminatingNew {



public static void main(String[] args) {


 Container container = null;


JFrame jframe = new JFrame("Math Adventure");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setLocationRelativeTo(null);
jframe.setBounds (150, 0, 1000, 1000);
jframe.setBackground(Color.blue);
jframe.setVisible(true);


 JLabel labelText = new JLabel("Welcome!");        

 jframe.getContentPane().add(new CharacterChoose());//
 jframe.setVisible(true);
 jframe.getContentPane().add(labelText); 
 jframe.setVisible(true);

So basically, I'm making a game. In another class in the assignment package is CharacterChoose, where the user is greeted with a picture of their character. All I want to do is add text to this same screen saying "Welcome", but whenever I try this it just ignores the CharacterChoose screen and opens a new blank frame that says "Welcome". Is there any way to fix this?

Upvotes: 0

Views: 262

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

GUI's are not linear/procedural programs, the are event driven, that is, something happens and you respond to it.

Instead, maybe consider using a button saying "Continue" or something, which the user must press, once pressed, you can then present the next view.

I'd recommend having a look at CardLayout for easier management of switching between views.

See How to Use Buttons, Check Boxes, and Radio Buttons, How to Write an Action Listeners and How to Use CardLayout for more details

Upvotes: 1

Related Questions