Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6171669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:15:28+00:00 2026-05-23T23:15:28+00:00

despite some tips, I’m still getting this one wrong. I end up with one

  • 0

despite some tips, I’m still getting this one wrong. I end up with one basic window and another one with extra features, but without the basic ones from the previous window. Instead, I would like one new window combining the basic and new features. Here is the code I’ve got: (also which approach would you advise?)

package windows;

import java.awt.*;
import javax.swing.*;

public abstract class WindowTemplate extends JFrame {

/**
 * Create the GUI and show it. For thread safety, this method should be
 * invoked from the event-dispatching thread.
 */
public WindowTemplate () {

JFrame myFrame = new JFrame("My first window");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
myFrame.setSize(550, 450);
myFrame.setLocationRelativeTo(null);

// JLabel emptyLabel = new JLabel("");
// emptyLabel.setPreferredSize(new Dimension(550, 450));

// myFrame.getContentPane().setLayout(new CardLayout());
// myFrame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

// myFrame.pack();

}

}

now the one that is meant to be “extended”:

package windows;

import java.awt.*;
import javax.swing.*;

public class a_Welcome extends WindowTemplate {

public a_Welcome() {

JPanel area = new JPanel();

JLabel text = new JLabel("One line another line and another line"); // , JLabel.CENTER);

// text.setBounds(80, 400, 400, 50);
add(area);

// area.setLayout(null);
area.add(text, new CardLayout());

// area.add(text); // , BorderLayout.CENTER);

Font font = new Font("SansSerif", Font.BOLD, 30);
text.setFont(font);
text.setForeground(Color.green);
area.setBackground(Color.darkGray);
area.setSize(550, 450);

}

}

// timer-after 5 seconds-go to the next window (countdown in the bottom right corner)

and the main:

package windows;

public class Launcher {

public static void main(String[] args) {

// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {

        // WindowTemplate.createWindow();
        // a_Welcome.createWindow();

         a_Welcome window = new a_Welcome();
         window.setVisible(true);
    }
});

}

}

— Alternatively —

public class WindowTemplate extends JFrame {

// Constructor
public WindowTemplate() {
    init();
}

public void init() {
    // add basic components
    JFrame myFrame = new JFrame("My first window");
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.setVisible(true);
    myFrame.setSize(550, 450);
    myFrame.setLocationRelativeTo(null);
}
}

and

public class a_Welcome extends WindowTemplate {

public a_Welcome() {
    super();
}

@Override
public void init() {
    super.init(); // important so you get the base stuff
    // add other components
    JPanel area = new JPanel();
    JLabel text = new JLabel("One line another line and another line");
    add(area);
    area.add(text, new CardLayout());

    Font font = new Font("SansSerif", Font.BOLD, 30);
    text.setFont(font);
    text.setForeground(Color.green);
    area.setBackground(Color.darkGray);
    area.setSize(550, 450);

}
}

Sorry for lots of code and thanks for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:15:28+00:00Added an answer on May 23, 2026 at 11:15 pm

    I’m not quite sure what your problem is, but your WindowTemplate basically is a JFrame, so you don’t want to create a new JFrame in your constructor but rather “apply” those method to this instead of myFrame.
    Try something like this:

    public WindowTemplate()
    {
        super("My first window");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // "this" is optional
        setVisible(true);
        setSize(550, 450);
        setLocationRelativeTo(null);
    }
    

    and

    public a_Welcome()
    {
        super(); // this is implicit, because a_Welcome extends WindowTemplate, which has got a constructor without parameters
        //[add more stuff here]
    
    }
    

    When you’re creating a new a_Welcome, its constructor will call the super constructor, which is WindowTemplate which will in turn call the JFrame constructor

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Despite reading many articles (some on this board) I am still scratching my head
Despite this being one of the best error messages I've ever seen (second only
Despite some posts on this forum and others I cannot find something that tells
For some reason I'm not getting this. (Example model below) If I write: var
I think this has been done many times before but despite reading some posts
I have to work on several VB6 legacy projects and despite some good VB6
I'm new to alfresco, and despite some research, I did not find how to
Despite reading lots of other posts regarding GIT and moved files I still struggle
This is driving me nuts. I am using some 3rd-party code in a Windows
I have heard that some characters are not present in the Unicode standard despite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.