0

我的程序显示它运行成功,但我的对话框不会出现。我是 Java 新手,我不确定我哪里出错了,有人可以帮忙吗?我正在尝试写入将存储用户输入的文件

/*


 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
    package database;

/**
 *
 *
 */

   import java.io.*;
   import java.awt.*;
   import java.awt.event.*;

    `enter code here`import javax.swing.*;

    `public class Database extends JFrame implements ActionListener{

        FlowLayout flow = new FlowLayout();

        JLabel fName = new JLabel("First Name");
        JTextField fName1 = new JTextField(15);

         JLabel blankSpaces1 = new JLabel("                             ");

         JLabel lName = new JLabel("Last Name");
         JTextField lName1 = new JTextField(15);
         JLabel blankSpaces2 = new JLabel("                                                       ");

         JLabel age = new JLabel("Age");
        JTextField age1 = new JTextField(3);
      JLabel blankSpaces3 = new JLabel("                 ");

         JLabel email = new JLabel("Email Address");
         JTextField email1 = new JTextField(30);
         JLabel blankSpaces4 = new JLabel("                 ");

         JLabel phone = new JLabel("Cell Phone Number");
         JTextField phone1 = new JTextField(10);
         JLabel blankSpaces5 = new JLabel("                 ");

         JLabel blankSpaces6 = new JLabel("                 ");


         JButton enter = new JButton("Enter");
         JButton reset = new JButton("Reset");
         JButton exitButton = new JButton("Exit");
         Container con = getContentPane();


        public void Database (){
           Database Dab = new Database();
           con.setLayout(flow);
          setTitle("Antwain's Database");

        Dab.setSize(800,300);
        setVisible(true);





            con.add(fName);
            con.add(blankSpaces1);
            con.add(fName1);
            con.add(blankSpaces1);
            con.add(lName);
            con.add(lName1);
            con.add(blankSpaces2);

            con.add(age);
            con.add(age1);
            con.add(blankSpaces3);

            con.add(email);
            con.add(email1);
            con.add(blankSpaces4);

            con.add(phone);
            con.add(phone1);
            con.add(blankSpaces5);

            con.add(blankSpaces6);
            con.add(blankSpaces2);



            con.add(enter);
            con.add(exitButton);
            con.add(reset); 

        enter.addActionListener(this);
        exitButton.addActionListener(this);
        reset.addActionListener(this);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

            public void actionPerformed(ActionEvent e1){
          Object source = e1.getSource();

         if(source == enter){

            String fName = fName1.getText();
            String lName = lName1.getText();
            String age = age1.getText();
            String email = email1.getText();
            String phone = phone1.getText();

        }
        else
            if(source == reset){
            fName1.setText("");
            lName1.setText("");
            age1.setText("");
            email1.setText("");
            phone1.setText("");
    }
           else
              {
                // if the user clicks on the Exit button (source is Exit button)
                System.exit(0);
              } 


                   }



        public static void main(String[] args) {

           Database dab = new Database();

    }
        }
4

2 回答 2

1

new Database();
调用一个空的构造函数,因为您没有定义任何构造函数。
更改public void Database() {}public Database()
此外,您必须Database Dab = new Database();从构造函数中删除并将行更改Dab.setSize(800,300);setSize(800,300);
您的代码将在您进行此更改时起作用,但您应该真正致力于您的编程风格和代码格式。

于 2012-12-14T07:33:41.457 回答
1

你需要做的还有很多,修复你在代码中做错的事情。希望你能找出你的错误。

public class DataBaseFrame extends JFrame implements ActionListener {

    FlowLayout flow = new FlowLayout();
    JLabel fName = new JLabel("First Name");
    JTextField fName1 = new JTextField(15);
    JLabel blankSpaces1 = new JLabel("");
    JLabel lName = new JLabel("Last Name");
    JTextField lName1 = new JTextField(15);
    JLabel blankSpaces2 = new JLabel("");
    JLabel age = new JLabel("Age");
    JTextField age1 = new JTextField(3);
    JLabel blankSpaces3 = new JLabel("");
    JLabel email = new JLabel("Email Address");
    JTextField email1 = new JTextField(30);
    JLabel blankSpaces4 = new JLabel("");
    JLabel phone = new JLabel("Cell Phone Number");
    JTextField phone1 = new JTextField(10);
    JLabel blankSpaces5 = new JLabel("");
    JLabel blankSpaces6 = new JLabel("");
    JButton enter = new JButton("Enter");
    JButton reset = new JButton("Reset");
    JButton exitButton = new JButton("Exit");

    public DataBaseFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Antwain's Database");
        JPanel panel = new JPanel(new FlowLayout());
        panel.add(fName);
        panel.add(blankSpaces1);
        panel.add(fName1);
        panel.add(blankSpaces1);
        panel.add(lName);
        panel.add(lName1);
        panel.add(blankSpaces2);

        panel.add(age);
        panel.add(age1);
        panel.add(blankSpaces3);

        panel.add(email);
        panel.add(email1);
        panel.add(blankSpaces4);

        panel.add(phone);
        panel.add(phone1);
        panel.add(blankSpaces5);

        panel.add(blankSpaces6);
        panel.add(blankSpaces2);

        panel.add(enter);
        panel.add(exitButton);
        panel.add(reset);
        getContentPane().add(panel);
        enter.addActionListener(this);
        exitButton.addActionListener(this);
        reset.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e1) {
        Object source = e1.getSource();

        if (source == enter) {

            String fName = fName1.getText();
            String lName = lName1.getText();
            String age = age1.getText();
            String email = email1.getText();
            String phone = phone1.getText();

        } else if (source == reset) {
            fName1.setText("");
            lName1.setText("");
            age1.setText("");
            email1.setText("");
            phone1.setText("");
        } else {
            // if the user clicks on the Exit button (source is Exit button)
            System.exit(0);
        }

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                DataBaseFrame dab = new DataBaseFrame();
                dab.setSize(800, 300);
                dab.setVisible(true);
            }
        });

    }
}
于 2012-12-14T07:32:40.530 回答