gpt4 book ai didi

java - 创建 JFrame 的正确方法

转载 作者:行者123 更新时间:2023-12-01 07:59:38 25 4
gpt4 key购买 nike

我正在阅读两种创建 JFrame 的方法,但我不清楚哪种方法更好或有何区别:

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

public class MyFramePanel {

public static void main(String[] args) {

JFrame myFrame = new JFrame("MyFramePanel");
....

我在一些书中找到了这个:

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

public class ButtonFrame **extends JFrame**{

public ButtonFrame() {
super("Button Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
...
setVisible(true);
}

public static void main(String[] args) {
ButtonFrame bf = new ButtonFrame();
}
}

我将使用第一个 - 我在 Stackoverflow 中找到它 - 但我想知道为什么我正在阅读的大多数有关 Java 的书籍都使用“扩展 JFrame”。

非常感谢您,如果我的问题很愚蠢,很抱歉打扰您。

最佳答案

第一个是不错的选择。想想Composition over Inheritance .

To favor composition over inheritance is a design principle that gives the design higher flexibility, giving business-domain classes and more stable business domain in the long term.

如果您不在 JFrame 中使用 MyFramePanel,而是在 Applet 中使用,则更容易切换且更易于维护从长远来看。

关于java - 创建 JFrame 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26224397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com