0

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

        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 中,而是创建了这种形式不知道如何....

4

1 回答 1

2

ShowDeatilsextends from 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);
    }
于 2013-03-06T08:17:59.853 回答