0

给我的第一篇文章。我是计算机科学学位第一年的学生。

我只是为分配构建一个基本的 GUI,由于某种原因,我一生都看不到,这是我创建的 JFrame 之一,当从注册按钮调用注册类的实例时,它显示完全空白登录类中的动作侦听器...

我还有一个单独的主类,其中包含 Main 方法并调用 Login 类。登录类 JFrame 工作正常,如前所述,只有在登录类的注册按钮操作侦听器中调用注册类时,才会出现问题。程序中的所有其他 JFrame 也可以正常工作。

我试图直接从 main 调用 Register 类,它也有同样的问题,尽管我试图将其简化为最基本的形式,但除了空白的无色 JFrame 之外,它仍然不显示任何内容。

这是代码(未完成但按原样工作)。我为我的草率道歉,但我是一个完整的初学者。

谁能看到它有什么问题?

提前致谢!

    package guitest;

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;

    public class Register extends JFrame{

        JButton regSubmit = new JButton("Submit");

        JTextField email = new JTextField();
        JTextField name = new JTextField();
        JTextField Address1 = new JTextField();
        JTextField Address2 = new JTextField();

        JPasswordField password1 = new JPasswordField();
        JPasswordField password2 = new JPasswordField();

        String nameTxt = email.getText();
        String passTxt = password1.getText();
        String info = "";

        FlowLayout layout1 = new FlowLayout();    

        public void Reg(){
            this.setTitle("La Volpe Registration");
            this.setLayout(layout1);    
            this.add(Address1);
            this.add(Address2);
            this.add(email);
            this.add(password1);
            this.add(password2);
            this.add(name);
            this.add(regSubmit);
            this.getContentPane().setBackground(Color.green);
            this.setSize(370, 160);

            regSubmit.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    regSubmitActionPerformed(evt);
                }

                private void regSubmitActionPerformed(java.awt.event.ActionEvent evt) { 

                String name = email.getText();
                String pass = password1.getText();
                String info = "";

                System.out.println("registering...");

                boolean eof;

                try{
                    // Create file
                    FileWriter file = new FileWriter("\\regdata.txt");
                    BufferedWriter out = new BufferedWriter(file);
                    out.write("\n"+nameTxt+", "+passTxt);
                }

                catch (Exception e){
                }   
            }   
        });
        this.setVisible(true);
    }    
}

以及链接到它的类...


package guitest;

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;

    /**
    * @author david
    */

    public class Login {

    JFrame loginFrame = new JFrame();
    Register reg3 = new Register();

    JButton submit = new JButton("Submit");
    JButton clear = new JButton("Clear");
    JButton register = new JButton ("Register with Us");

    JPasswordField pass = new JPasswordField(20);
    JTextField email = new JTextField(20);
    JLabel em = new JLabel("Email Address: ");
    JLabel pw = new JLabel("Password: ");

    String pathname;
    String line;
    String [] records = new String [1000];
    int count = 0;

    FlowLayout layout1 = new FlowLayout();

        public Login(){

            //Adds Email label and text field
            loginFrame.add(em);
            loginFrame.add(email);

            //Adds password label and field
            loginFrame.add(pw);
            loginFrame.add(pass);

            //Adds buttons
            loginFrame.add(submit);
            loginFrame.add(clear);
            loginFrame.add(register);

            loginFrame.getContentPane().setBackground(Color.green);

            loginFrame.setLayout(layout1);

            loginFrame.setSize(370, 160);

            loginFrame.setTitle("La Volpe - Login");

            submit.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    submitActionPerformed(evt);    
                }            

                private void submitActionPerformed(java.awt.event.ActionEvent evt){                                         
                String emailadd = email.getText();
                String password = pass.getText();
                pathname = "\\regdata.txt";

                boolean eof;

                try{
                    FileReader file = new FileReader(pathname);
                    BufferedReader buff = new BufferedReader(file);

                    eof = false; // set the eof boolean to false

                        while (!eof){
                        line = buff.readLine();
                        if (line == null){ // test to see if the end of file has been reached
                        eof = true;  // if the end of file has been found set the eof Boolean to true
                        }
                        else{ 
                        // end of file not reached so move the contents of the line to the records
                        //array
                        records[count] = line;
                        count ++;
                        System.out.println(line);  // print out the new line input for error checking
                        }
                    }    
                    buff.close();
                }
                catch (IOException e){
                    System.out.println("Error --  "+ e.toString());
                }

                boolean notTrue = false;

                for (int i = 0; i < count; i++) {       
                    if ((!notTrue)&&((emailadd + "," + password).equals(records[i]))) {
                        FoodSelectionMain loggedIn = new FoodSelectionMain();
                        loggedIn.setVisible(true);
                    } 
                }
                if (!notTrue){
                    JOptionPane.showInputDialog("Please check your login "
                    + "and try again. If you are a new user, please "
                    + "register by pressing the 'REGISTER' button");
                }
            }   

        }); 

        // TODO add your handling code here:


        clear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearActionPerformed(evt);
            }

            public void clearActionPerformed(java.awt.event.ActionEvent evt){
                email.setText(null);
                pass.setText(null);
            }

        }); 
        register.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registerActionPerformed(evt);
            }

            public void registerActionPerformed(java.awt.event.ActionEvent evt){
                reg3.setVisible(true);
                    System.out.println("Register pressed");
            }
        });
        loginFrame.setVisible(true);
    }
}
4

1 回答 1

1

试试这个,在 Register 类中,将构造函数名称从 Reg() 更正为 Register()。

在构建 gui 应用程序之前,请牢记这些准则

  1. 创建容器子类的对象。

  2. 将容器中的所有组件视为实例变量。

  3. 在方法的构造函数之外设置这些实例变量和事件处理(即 setComponent()、setHandler().etc),但从构造函数执行这些方法调用。

  4. 现在主要使用这个...

.

EventQueue.invokeLater(new Runnable() {

    public void run() {
        Myframe f = new Myframe();
        f.setVisible(true);
    }
}
于 2012-05-22T04:22:35.887 回答