gpt4 book ai didi

Java Swing 打开现有表单

转载 作者:行者123 更新时间:2023-11-30 02:41:21 27 4
gpt4 key购买 nike

我试图调用一个方法来打开一个新的现有框架,但我得到的只是一个没有内容的空框架。

这是在按下按钮时调用该函数的代码:

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

基本上,当我按下 btnPersonalInfo 按钮时,我希望通过 PersonalInfo 方法查看该框架,但现在我只得到一个空框架。

谢谢您,如果重复的话,我们深表歉意,但这是我的第一个问题。

最佳答案

initialize() 中删除 frame = new JFrame();

简单地说:

    JPanel panel = new JPanel();
add(panel, BorderLayout.CENTER);//It will be added to the ContentPane by default.
panel.setLayout(null);
...

然后在调用时,去掉下面的 block :

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

这应该是你的类(class):

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

关于Java Swing 打开现有表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41594769/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com