java - 第二个 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);
}
}
任何帮助将不胜感激!
解决方案
(当我使用 AjouterAdherent 的 main 初始化时,它是正确创建的)
所以看看能正常工作的代码:
AjouterAdherent window = new AjouterAdherent();
window.frame.setVisible(true);
我尝试初始化一个不同的 JFRame,我得到一个空白的 JFrame
并查看不起作用的代码:
AjouterAdherent ajouterAdherent = new AjouterAdherent();
ajouterAdherent.setVisible(true);
有什么区别?
问题是您的类扩展了 JFrame 并且还创建了一个新的 JFrame,因此您有两个框架实例,一个带有组件,一个没有。
不要扩展 JFrame!这样你就不会造成混乱。
只应在向类添加新功能时扩展类。向框架添加组件并不是添加新功能。
推荐阅读
- javascript - 固定 div 浮动在另一个 div 上导致 scrollIntoView 在 angular2/4/5 中过冲,我该如何解决?
- docker - Docker 使用单独的凭据进行推送和拉取
- c# - 启动 powershell 进程后继续执行代码
- asp.net - MVC登录密码比较区分大小写
- html - :focus-withing 不适用于悬停效果
- sql - Oracle 作业调度程序在每天一个特定时间执行,对该作业任务进行两次查询
- javascript - 如何在 Chrome 中加载下一页之前断开 websocket
- java - 使用 MediaStore.ACTION_IMAGE_CAPTURE 要求用户按 OK 或 RETRY
- r - 如何从 R 中的 for 循环返回绘图?
- acumatica - 复制到变量