我试图调用一个方法来打开一个新的现有框架,但是我得到的只是一个没有内容的空框架。
这是在按下按钮时调用该函数的代码:
JButton btnPersonalInfo = new JButton("Personal Info");
btnPersonalInfo.setBounds(10, 5, 120, 23);
btnPersonalInfo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame PersonalInfo = new JFrame("Personal Info");
PersonalInfo content = new PersonalInfo();
PersonalInfo.setContentPane(content);
PersonalInfo.setSize(700,700);
PersonalInfo.setLocation(100,15);
PersonalInfo.setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE );
PersonalInfo.setResizable(false);
PersonalInfo.setVisible(true);
}
});
panel_1.add(btnPersonalInfo);
这就是我初始化
PersonalInfo
函数的方式: public PersonalInfo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel lblPersonalInfo = new JLabel("Personal Information");
lblPersonalInfo.setFont(new Font("Arial", Font.BOLD, 16));
lblPersonalInfo.setBounds(110, 11, 185, 14);
panel.add(lblPersonalInfo);
JLabel lblFullName = new JLabel("Full Name");
lblFullName.setBounds(10, 36, 58, 14);
panel.add(lblFullName);
JLabel lblNationality = new JLabel("Nationality");
lblNationality.setBounds(10, 61, 78, 14);
panel.add(lblNationality);
JLabel lblDateBirth = new JLabel("Date of Birth");
lblDateBirth.setBounds(10, 86, 78, 14);
panel.add(lblDateBirth);
JLabel lblGender = new JLabel("Gender");
lblGender.setBounds(10, 111, 46, 14);
panel.add(lblGender);
JLabel lblAddress = new JLabel("Address");
lblAddress.setBounds(10, 164, 58, 14);
panel.add(lblAddress);
JLabel lblMobile = new JLabel("Mobile");
lblMobile.setBounds(10, 189, 46, 14);
panel.add(lblMobile);
JLabel lblEmail = new JLabel("E-mail");
lblEmail.setBounds(10, 214, 46, 14);
panel.add(lblEmail);
JRadioButton rdbtnM_2 = new JRadioButton("M");
rdbtnM_2.setBounds(74, 133, 109, 23);
panel.add(rdbtnM_2);
JRadioButton rdbtnF = new JRadioButton("F");
rdbtnF.setBounds(74, 107, 109, 23);
panel.add(rdbtnF);
}
}
基本上,我希望在按下
PersonalInfo
按钮时从btnPersonalInfo
方法查看框架,现在我只得到一个空框架。谢谢你,如果这是重复的,对不起,但这是我的第一个问题。
最佳答案
从您的frame = new JFrame();
中删除initialize()
只是:
JPanel panel = new JPanel();
add(panel, BorderLayout.CENTER);//It will be added to the ContentPane by default.
panel.setLayout(null);
...
然后在调用时,删除以下块:
JFrame PersonalInfo = new JFrame("Personal Info");
PersonalInfo content = new PersonalInfo();
PersonalInfo.setContentPane(content);
替换为:
PersonalInfo personalInfo = new PersonalInfo();
personalInfo.setSize(700,700);
personalInfo.setLocation(100,15);
....
因此,这应该是您的方法调用:
public void actionPerformed(ActionEvent e) {
PersonalInfo personalInfo = new PersonalInfo();
personalInfo.setSize(700,700);
personalInfo.setLocation(100,15);
personalInfo.setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE );
personalInfo.setResizable(false);
personalInfo.setVisible(true);
}
这应该是您的班级:
public class PersonalInfo extends JFrame
{
public PersonalInfo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
JPanel panel = new JPanel();
add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel lblPersonalInfo = new JLabel("Personal Information");
lblPersonalInfo.setFont(new Font("Arial", Font.BOLD, 16));
lblPersonalInfo.setBounds(110, 11, 185, 14);
panel.add(lblPersonalInfo);
JLabel lblFullName = new JLabel("Full Name");
lblFullName.setBounds(10, 36, 58, 14);
panel.add(lblFullName);
JLabel lblNationality = new JLabel("Nationality");
lblNationality.setBounds(10, 61, 78, 14);
panel.add(lblNationality);
JLabel lblDateBirth = new JLabel("Date of Birth");
lblDateBirth.setBounds(10, 86, 78, 14);
panel.add(lblDateBirth);
JLabel lblGender = new JLabel("Gender");
lblGender.setBounds(10, 111, 46, 14);
panel.add(lblGender);
JLabel lblAddress = new JLabel("Address");
lblAddress.setBounds(10, 164, 58, 14);
panel.add(lblAddress);
JLabel lblMobile = new JLabel("Mobile");
lblMobile.setBounds(10, 189, 46, 14);
panel.add(lblMobile);
JLabel lblEmail = new JLabel("E-mail");
lblEmail.setBounds(10, 214, 46, 14);
panel.add(lblEmail);
JRadioButton rdbtnM_2 = new JRadioButton("M");
rdbtnM_2.setBounds(74, 133, 109, 23);
panel.add(rdbtnM_2);
JRadioButton rdbtnF = new JRadioButton("F");
rdbtnF.setBounds(74, 107, 109, 23);
panel.add(rdbtnF);
}
}