我有一个叫做WindowTemplate的类,它是其他(更复杂)窗口的基础。这是一个抽象类,然后我尝试使用“扩展”技巧将更多内容添加到新窗口中,并保留原始的“骨架”。不过那是我的问题,因为如果我运行WindowTemplate.createWindow();或a_Welcome.createWindow(); (它们应该指向同一件事),我得到了“基本”窗口。但是当我运行a_Welcome窗口= new a_Welcome(); (应该是基础+新的东西),我只会得到我添加的额外位,而没有原始功能。这是我的代码:

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 static void createWindow() {

    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();

}

}


具有新窗口和一些其他内容的类(忽略a_):

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)


主要:

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);
        }
    });

}

}


 谢谢你的帮助!

最佳答案

静态方法createWindow()始终创建一个新的JFrame,它不是WindowTemplate的超类。 a_Window的构造方法正在向WindowTemplate添加尚未初始化的组件,因为静态createWindow()创建了一个独立的框架。

我建议您将静态createWindow()更改为WindowTemplate构造函数,然后尝试再次运行main

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();

}

}

10-07 16:34