gpt4 book ai didi

java - swing Jform 创建

转载 作者:行者123 更新时间:2023-12-02 07:13:24 25 4
gpt4 key购买 nike

这是我的代码,问题是我找不到为什么创建一个额外的表单

        public void actionPerformed(ActionEvent ae){
submitBtn.setForeground(Color.red);
//getting value form the textFields
String name = nameTxt.getText();
String userName = userNameTxt.getText();
char[] pass = password.getPassword();
Object monthValue = month.getSelectedItem();
String yearValue = yearTxt.getText();
String dateValue = dateTxt.getText();
Object genderValue = gender.getSelectedItem();
String mobileValue = mobileTxt.getText();
Object locationValue = country.getSelectedItem();

showDetails page = new showDetails();
page.setVisible(true);
JPanel contentPane = new JPanel();
contentPane.setLayout(null);
contentPane.setBorder(BorderFactory.createTitledBorder("user Details"));
JLabel nameVL = new JLabel("Name : " + name );
nameVL.setBounds(80, 30, 200, 200);
contentPane.add(nameVL);
page.getContentPane().add(contentPane);
}

以上代码位于signuppage.java类

我创建一个表单来将标签结果显示到 showDetails 类

public class showDetails extends JFrame {

Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

public showDetails() {
JFrame showFrame = new JFrame("user details");
showFrame.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
showFrame.setSize(300, 400);
showFrame.setVisible(true);
showFrame.setLocation((screen.width - 500)/2,((screen.height-350)/2));
}
}

但我的问题是结果没有显示在 showFram 中,而不是这个表单的创建不知道如何......

最佳答案

ShowDeatils 扩展自 JFrame,而在其构造函数中,您正在创建另一个 JFrame,因此,您每次都会创建两个框架您创建 showDetails

的新实例

已更新

基本上,或者你需要做的是......

public void actionPerformed(ActionEvent ae){
submitBtn.setForeground(Color.red);
//getting value form the textFields
String name = nameTxt.getText();
String userName = userNameTxt.getText();
char[] pass = password.getPassword();
Object monthValue = month.getSelectedItem();
String yearValue = yearTxt.getText();
String dateValue = dateTxt.getText();
Object genderValue = gender.getSelectedItem();
String mobileValue = mobileTxt.getText();
Object locationValue = country.getSelectedItem();

JFrame showFrame = new JFrame("user details");
showFrame.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
showFrame.setSize(300, 400);
showFrame.setLocation((screen.width - 500)/2,((screen.height-350)/2));

JPanel contentPane = new JPanel();
contentPane.setLayout(null);
contentPane.setBorder(BorderFactory.createTitledBorder("user Details"));
JLabel nameVL = new JLabel("Name : " + name );
nameVL.setBounds(80, 30, 200, 200);
contentPane.add(nameVL);

showFrame.getContentPane().add(contentPane);
showFrame.setVisible(true);
showFrame.setVisible(true);
}

关于java - swing Jform 创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15242136/

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