首页 > 解决方案 > 第二个 JFrame 是空白的并且非常小

问题描述

我是 Swing 新手,无法替换现有的 JFrame。我初始化第一个 JFrame 没有问题。

GererAdgerent 类:

public class GererAdherent extends JFrame {

private JPanel contentPane;
static GererAdherent frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame = new GererAdherent();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public GererAdherent() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btnAjouterAdherent = new JButton("Ajouter Adherent");
    btnAjouterAdherent.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            frame.setVisible(false);
            AjouterAdherent ajouterAdherent = new AjouterAdherent();
            ajouterAdherent.setVisible(true);
        }
    });
    btnAjouterAdherent.setBounds(104, 34, 130, 23);
    contentPane.add(btnAjouterAdherent);
}

}

但是一旦我尝试初始化一个不同的 JFRame,我会得到一个没有所有组件的空白 JFrame(当我使用 AjouterAdherent 的 main 初始化时它被正确创建)

类 AjouterAdherent :

public class AjouterAdherent extends JFrame {

JFrame frame;
JTextField txtNom;

static Properties p=new Properties();
static BibliothequeDAORemote proxy;
public static void main(String[] args) throws NamingException  {

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                AjouterAdherent window = new AjouterAdherent();
                window.frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

}

/**
 * Create the application.
 */
public AjouterAdherent() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.getContentPane().setBackground(SystemColor.menu);
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JTextPane txtpnAjouterClient = new JTextPane();
    txtpnAjouterClient.setFont(new Font("Tahoma", Font.BOLD, 11));
    txtpnAjouterClient.setBackground(SystemColor.menu);
    txtpnAjouterClient.setEnabled(false);
    txtpnAjouterClient.setEditable(false);
    txtpnAjouterClient.setForeground(Color.black);
    txtpnAjouterClient.setBounds(175, 11, 89, 20);
    txtpnAjouterClient.setText("Ajouter Client");
    frame.getContentPane().add(txtpnAjouterClient);

    JTextPane txtpnNom = new JTextPane();
    txtpnNom.setBackground(SystemColor.menu);
    txtpnNom.setEnabled(false);
    txtpnNom.setEditable(false);
    txtpnNom.setForeground(Color.black);
    txtpnNom.setBounds(36, 49, 72, 20);
    txtpnNom.setText("Nom");
    frame.getContentPane().add(txtpnNom);


}
}

任何帮助将不胜感激!

标签: javaswingjframe

解决方案


(当我使用 AjouterAdherent 的 main 初始化时,它是正确创建的)

所以看看能正常工作的代码:

AjouterAdherent window = new AjouterAdherent();
window.frame.setVisible(true);

我尝试初始化一个不同的 JFRame,我得到一个空白的 JFrame

并查看不起作用的代码:

AjouterAdherent ajouterAdherent = new AjouterAdherent();
ajouterAdherent.setVisible(true);

有什么区别?

问题是您的类扩展了 JFrame 并且还创建了一个新的 JFrame,因此您有两个框架实例,一个带有组件,一个没有。

不要扩展 JFrame!这样你就不会造成混乱。

只应在向类添加新功能时扩展类。向框架添加组件并不是添加新功能。


推荐阅读