【问题标题】:Add element to JFrame from other class从其他类向 JFrame 添加元素
【发布时间】:2020-10-15 07:32:07
【问题描述】:

我有两个课程合二为一JFrame 创建:

public class Window extends JFrame {

    public void createWindow() throws IOException {
        setTitle(GeneralValues.PROGRAM_NAME);
        setSize(1100, 600);
        setResizable(false);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setLayout(null);
        setVisible(true); 
    }
}

第二个我有一个JLabel

public class InitialConfiguration {

    Window w = new Window();

    public void main() {
        JLabel blauncherIcon = new JLabel();
        blauncherIcon.setBounds(100, 100, 100, 100);
        blauncherIcon.setOpaque(true);
        blauncherIcon.setText("Text");

        w.add(blauncherIcon);
    }
}

我的主要方法:

public class Main {

    Window w = new Window();
    InitialConfiguration ic = new InitialConfiguration();

    public static void main(String[] args) {
    
         w.createWindow();
         ic.main();
    
    }

}

我想将InitialConfiguration 类的标签添加到Window 类的框架,但不幸的是,此代码创建了一个框架,但没有添加InitialConfiguration 类的标签。能做到吗?

【问题讨论】:

  • 1) InitialConfiguration 中的方法是您实际的main 方法吗?因为如果是这样,它的声明是不正确的,这段代码甚至不会运行。如果不是,那么您应该重命名该方法。 2)“这段代码创建了一个JFrame”,是吗?我没有看到你在任何地方的框架上调用setVisible()...... 3)你不调用createWindow(),只有你的Window()的构造函数。
  • 1) 为了尽快获得更好的帮助,edit 添加minimal reproducible exampleShort, Self Contained, Correct Example。硬编码数据替换数据库。 2) Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同的语言环境中的不同 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them 以及white space 的布局填充和边框。 3)另见..
  • .. What is the XY problem? 4) ` Window w = new Window();` 不要将类命名为 Window,因为它与 AWT 类同名。给它一个描述性的名称。
  • @maloomeister 这个方法 main 不是我的 main 方法,我知道如果是,程序甚至不会启动,我还有另一个 main 方法,它是首先在 Window 中调用 createWindow 的 main 方法然后主要在 InitialConfiguration 中。由于我的代码太大而无法粘贴到此处,因此我剪切了一些 Window 类,并没有注意到我剪切了 setVisible(true),但它实际上是。
  • 我注意到您将 LayoutManager 设置为 null。这是一个非常的坏主意。在微不足道的情况下它可能看起来不错,但 LayoutManager 的存在是为了帮助开发人员。迟早你会想要在与你正在测试的系统不同的系统上运行一个程序,而那些空布局管理器会在后端咬你。相信我,与替代方案相比,它们相对容易学习和使用

标签: java swing jframe


【解决方案1】:

您的问题是,您的 Window 实例出错了。您的InitialConfiguration 有一个成员变量Window,标签被添加到其中。但这与您在 Main 类中的 Window 实例不同。

总而言之,您创建并显示窗口,但从不显示您实际添加标签的第二个窗口。

总的来说,如果你真的想保持这种结构(我建议不要这样做,但你可能有你的理由),将正确的 Window 实例作为方法参数传递应该可以解决它。

public class InitialConfiguration {

    // Removed second Window here
  
    public void main(Window w) { // rename this method please
        JLabel blauncherIcon = new JLabel();
        blauncherIcon.setBounds(100, 100, 100, 100); // use layout managers
        blauncherIcon.setOpaque(true);
        blauncherIcon.setText("Text");
        w.add(blauncherIcon);
    }
}

还有你的“主要”:

public class Main {

    Window w = new Window(); // rename the class Window please
    InitialConfiguration ic = new InitialConfiguration();

    public static void main(String[] args) {
    
         w.createWindow();
         ic.main(w);
    }
}

【讨论】:

    【解决方案2】:

    你没有在窗口课上开始 Jframe,我不知道你的主课,所以我做了它是为了你的理解。检查此代码;

    已编辑

    主要:示例代码:

    public class MainMenu{
    
       
        public static void main(String[] args) {
            Windows w = new Windows();
            InitialConfiguration ini = new InitialConfiguration(w);
            w.createWindow();
            ini.main();
        }
    }
    

    ICONFIG 类示例代码:

    public class InitialConfiguration {
    
        Window w;
    
        public InitialConfiguration(Windows w){
            this.w = w;
    }
        public void main() {
            
            
            JLabel blauncherIcon = new JLabel();
            blauncherIcon.setBounds(100, 100, 100, 100);
            blauncherIcon.setOpaque(true);
            blauncherIcon.setText("Text");
    
            w.add(blauncherIcon);
            w.repaint();
        }
    }
    

    窗口类示例代码:

    public class Window extends JFrame {
    
        public void createWindow(){
            setTitle("Your title");
            setSize(1100, 600);
            setResizable(false);
            setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            setLayout(null);
            setVisible(true);
        }
    }
    

    【讨论】:

    • 它有效,但我想从我的主要方法调用 w.createWindow()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    • 2011-03-24
    • 2017-10-05
    • 2018-09-28
    • 2019-09-04
    • 2016-07-21
    相关资源
    最近更新 更多