首页 > 解决方案 > 向 GUI 构造函数添加参数会停止 GUI 弹出

问题描述

我已经开始使用 Eclipse 学习 Java,并且我更改了 main 函数以将两个字符串传递给经过类似更改的 GUI 构造函数(之前没有传递任何内容)。

GUI 现在不会在屏幕上弹出,但可以从屏幕底部的任务栏访问。我只是想知道为什么会这样?我在下面粘贴了缩短的代码。

我试了很多次,试图在网络上找到关键字的问题。

public class MainButton 
{
    public static void main(String[] args) 
    {
        String A = "title"; String B = "Button";
        Agui a = new Agui(A,B);

               //BEFORE Agui a = new Agui();
    }
}
import javax.swing.*;

public class Agui extends JFrame 
{
    //BEFORE public Agui()
    public Agui(String A, String B) 
    {
        setTitle(A);
        setSize(400, 400);

        // Create JButton and JPanel
        JButton button = new JButton(B);
        JPanel panel = new JPanel();

        // Add button to JPanel
        panel.add(button);
        // And JPanel needs to be added to the JFrame itself!
        this.getContentPane().add(panel);

        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
      }
}

无需转到屏幕底部的任务栏就可以弹出窗口,并了解为什么会出现此问题的逻辑,这将是很棒的。

标签: javaeclipseuser-interfaceconstructor

解决方案


推荐阅读