Java Gui写入输出文件

时间:2012-12-14 07:13:18

标签: java swing user-interface input

我的程序显示它已成功运行,但我的对话框不会显示。我是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();

    }
        }

2 个答案:

答案 0 :(得分: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);
            }
        });

    }
}

答案 1 :(得分:1)

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