Java 继承或 GUI 出了问题

标签 java swing user-interface inheritance

尽管有一些提示,我仍然弄错了。我最终得到一个基本窗口和另一个具有额外功能的窗口,但没有前一个窗口的基本窗口。相反,我想要一个结合了基本功能和新功能的新窗口。这是我得到的代码:(您还建议采用哪种方法?)

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

}

}

现在是要“扩展”的:

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

}

}



- 或者 -

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

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

}
}

抱歉,代码太多,感谢您的帮助!

最佳答案

我不太确定你的问题是什么,但你的 WindowTemplate 基本上一个JFrame,所以你不想在构造函数中创建一个新的 JFrame,而是将这些方法“应用”到 this 而不是 myFrame。 尝试这样的事情:

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

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

}

当您创建新的 a_Welcome 时,其构造函数将调用 super 构造函数,即 WindowTemplate,后者将依次调用 JFrame > 构造函数

关于Java 继承或 GUI 出了问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6768079/

相关文章:

c++ - 制作 map 编辑器的语言/GUI库

java - AWS Lambda 与 Java Spring

Java - JTextPane 中的自动缩进

java - 从字符串数组的 ArrayList 中删除字符串的可能逻辑?

java - 在 JTextPane 中保存字体

python - 如何将 python 多处理进程输出发送到 Tkinter gui

java - 命令: "DELETE FROM Table where ..." doesn't work

java - 为什么我收到错误 "Exception in thread "AWT-EventQueue- 0"java.lang.ArithmeticException:/by zero"?

java - 在 MVC 上的一个 Jframe 中有多个带有按钮的 Jpanel,如何在我的 Controller 中获取 actionlistener?

java - 后退按钮工作时操作栏主页按钮崩溃