第二个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(当我使用AjuterAdhesent的main进行初始化时,它是正确创建的(

A类附件:

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);

}
}

如有任何帮助,我们将不胜感激!

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

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

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

看看不起作用的代码:

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

有什么区别?

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

不要扩展JFrame!那你就不会引起混乱了。

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

最新更新