当我第一次调用一个对象时,我的 JFrame 是空白的

问题描述

我看到了一件奇怪的事情。当我在调试模式下运行代码并通过单击按钮调用此行时:

 popup.setVisible(true);

我看到一个没有上下文的空白窗口 (popupConfig)。我该怎么做才能使用这个 popupConfig 一次性显示我的 popupVisible(true)

主要

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class PopupConfig  extends JFrame 
{
    private final static Color backgroundColor = new Color(0,51,102);
    
    private final static Font customeFont = new Font("Arial",Font.BOLD,18);
    
    public PopupConfig() 
    {
        setTitle("TEST");
        setAlwaysOnTop(true);
        setSize(600,500);
        setLocation(400,400);
        getContentPane().setLayout(null);
        getContentPane().setBackground(backgroundColor);
        setDefaultCloSEOperation(JFrame.disPOSE_ON_CLOSE);

        JLabel lblSeting = new JLabel("SETTINGS");
        lblSeting.setHorizontalAlignment(SwingConstants.CENTER);
        lblSeting.setFont(new Font("Arial",42));
        lblSeting.setForeground(Color.WHITE);
        lblSeting.setSize( 331,49 );
        lblSeting.setLocation( 114,11 );
        getContentPane().add(lblSeting,BorderLayout.norTH);
    }
}

PopupConfig

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class PopupConfig  extends JFrame 
{
    private final static Color backgroundColor = new Color(0,102);
    private final static Font customeFont = new Font("Arial",18);

    public PopupConfig() 
    {
        setTitle("TEST");
        setAlwaysOnTop(true);
        setSize(600,BorderLayout.norTH);
    }
}

窗口

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Window  extends JFrame implements ActionListener{
    
    private JButton btn;
    private PopupConfig popup;
    
    public Window()
    {
        setTitle("TEST");
        setAlwaysOnTop(true);
        setSize(600,400);
        getContentPane().setLayout(null);
        getContentPane().setBackground(new Color(0,102) );
        setDefaultCloSEOperation(JFrame.disPOSE_ON_CLOSE);
         
        btn = new JButton("TEST");
        
        btn.addActionListener(this);
        btn.setBounds(200,200,200);
        add(btn);
        
        popup = new PopupConfig();
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        popup.setVisible(true);
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)