Swing 如何在侦听器(java)中设置可视帧?

Swing 如何在侦听器(java)中设置可视帧?,swing,listener,frame,Swing,Listener,Frame,我有一个类登录视图,如下所示: import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.J

我有一个类登录视图,如下所示:

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;

public class LoginView {

public static void main(String[] args) {
    JFrame frame = new JFrame("Pagina di login");
    frame.setSize(600, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    placeComponents(frame);
    frame.setVisible(true);
}

private static void placeComponents(JFrame frame) {
    frame.setLayout(null);

    JLabel userLabel = new JLabel("Username");
    userLabel.setBounds(110, 10, 80, 25);
    frame.add(userLabel);

    JTextField userText = new JTextField(20);
    userText.setBounds(200, 10, 180, 25);
    frame.add(userText);

    JLabel passwordLabel = new JLabel("Password");
    passwordLabel.setBounds(110, 40, 80, 25);
    frame.add(passwordLabel);

    JPasswordField passwordText = new JPasswordField(20);
    passwordText.setBounds(200, 40, 180, 25);
    frame.add(passwordText);

    JButton loginButton = new JButton("Login");
    loginButton.setBounds(110, 80, 80, 25);
    frame.add(loginButton);

    JButton registerButton = new JButton("Registrati");
    registerButton.setBounds(280, 80, 100, 25);
    frame.add(registerButton);

    ActionListener loginButtonListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String uname = userText.getText();
            String pass = passwordText.getText();
            boolean login_error=true;
            if (pass.contains("admin") && uname.contains("admin")) {
                JButton source = (JButton) e.getSource();
                JOptionPane.showMessageDialog(source, "Username e password valide per admin. \n" + 
                "Sei entrato nel sistema come admin.");
                login_error=false;
                userText.setText(null);
                passwordText.setText(null);
                frame.dispose();
                AdminView catalogo = new AdminView();
            }

            try {
                        BufferedReader br = new BufferedReader(new FileReader("cliente.txt"));
                        try {
                            String line;
                            while ( (line = br.readLine()) != null) {
                                String[] snippet = line.split(",");
                                //System.out.println(snippet[0]+" "+snippet[1]);
                                if (snippet[0].equals(uname) && snippet[1].equals(pass)) {
                                    JButton source = (JButton) e.getSource();
                                    JOptionPane.showMessageDialog(source, "Username e password validi. \n" + 
                                    "Sei entrato nel negozio.");
                                    login_error=false;
                                    userText.setText(null);
                                    passwordText.setText(null);
                                    frame.dispose();
                                    AdminView catalogo = new AdminView();
                                    break;
                                }
                            }
                        } catch (IOException ioe){
                            JOptionPane.showMessageDialog(null, "cliente.txt not found", "login error", JOptionPane.ERROR_MESSAGE);
                        }
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(null, "cliente.txt not found", "login error", JOptionPane.ERROR_MESSAGE);
            }

            if (login_error == true) {
                JOptionPane.showMessageDialog(null, "Username e password non validi.", "login error", JOptionPane.ERROR_MESSAGE);
                userText.setText(null);
                passwordText.setText(null);
            }




        }
    };

    ActionListener registerButtonListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            SignView registrazione = new SignView();
        }
    };

    loginButton.addActionListener(loginButtonListener);

    registerButton.addActionListener(registerButtonListener);
}

}
此类生成两个按钮,loginButton和registerButton。在登录ActionListener中,我检查这些文本字段,打开AdminView并执行它运行的代码。在register ActionListener中,我打开了SignupView,但是如果我执行代码,打开的框架就不会出现。为什么?

这是尚未完成的SignupView类,但它可能会出现:

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;

public class SignView {

public static void main(String[] args) {
    JFrame frame = new JFrame("Pagina di Registrazione");
    frame.setSize(500, 450);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    placeComponents(frame);
    frame.setVisible(true);
}

private static void placeComponents(JFrame frame) {
    frame.setLayout(null);

    JLabel userLabel = new JLabel("Username");
    userLabel.setBounds(10, 10, 80, 25);
    frame.add(userLabel);

    JTextField userText = new JTextField(20);
    userText.setBounds(150, 10, 240, 25);
    frame.add(userText);

    JLabel passwordLabel = new JLabel("Password");
    passwordLabel.setBounds(10, 40, 80, 25);
    frame.add(passwordLabel);

    JPasswordField passwordText = new JPasswordField(20);
    passwordText.setBounds(150, 40, 240, 25);
    frame.add(passwordText);

    JLabel pwdLabel = new JLabel("Conferma password");
    pwdLabel.setBounds(10, 70, 130, 25);
    frame.add(pwdLabel);

    JTextField pwdText = new JTextField(20);
    pwdText.setBounds(150, 70, 240, 25);
    frame.add(pwdText);

    JLabel nameLabel = new JLabel("Nome");
    nameLabel.setBounds(10, 100, 80, 25);
    frame.add(nameLabel);

    JTextField nameText = new JTextField(20);
    nameText.setBounds(150, 100, 240, 25);
    frame.add(nameText);

    JLabel surnameLabel = new JLabel("Cognome");
    surnameLabel.setBounds(10, 130, 80, 25);
    frame.add(surnameLabel);

    JTextField surnameText = new JTextField(20);
    surnameText.setBounds(150, 130, 240, 25);
    frame.add(surnameText);

    JLabel cityLabel = new JLabel("Citta' di residenza");
    cityLabel.setBounds(10, 160, 130, 25);
    frame.add(cityLabel);

    JTextField cityText = new JTextField(20);
    cityText.setBounds(150, 160, 240, 25);
    frame.add(cityText);

    JLabel codeLabel = new JLabel("Codice fiscale");
    codeLabel.setBounds(10, 190, 80, 25);
    frame.add(codeLabel);

    JTextField codeText = new JTextField(20);
    codeText.setBounds(150, 190, 240, 25);
    frame.add(codeText);

    JLabel telLabel = new JLabel("Numero di telefono");
    telLabel.setBounds(10, 220, 130, 25);
    frame.add(telLabel);

    JTextField telText = new JTextField(20);
    telText.setBounds(150, 220, 240, 25);
    frame.add(telText);

    JLabel smartLabel = new JLabel("Numero di cellulare");
    smartLabel.setBounds(10, 250, 130, 25);
    frame.add(smartLabel);

    JTextField smartText = new JTextField(20);
    smartText.setBounds(150, 250, 240, 25);
    frame.add(smartText);

    JButton creationAccButton = new JButton("Crea account");
    creationAccButton.setBounds(150, 280, 110, 25);
    frame.add(creationAccButton);

    JButton backLoginButton = new JButton("Ritorna al login");
    backLoginButton.setBounds(270, 280, 120, 25);
    frame.add(backLoginButton);

    ActionListener creationAccButtonListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String user = userText.getText();
            String passwd = passwordText.getText();
            String passwdConfirm = pwdText.getText();
            String uname = nameText.getText();
            String cityRes = cityText.getText();
            String taxCode = codeText.getText();
            String telNumber = telText.getText();
            String telSmartphone = smartText.getText();
            boolean login_error=true;
            try {
                        BufferedReader br = new BufferedReader(new FileReader("cliente.txt"));
                        try {
                            String line;
                            while ((line = br.readLine()) != null) {
                                String[] snippet = line.split(",");
                                //System.out.println(snippet[0]+" "+snippet[1]);
                                if (snippet[0].equals(user)) {
                                    JButton source = (JButton) e.getSource();
                                    JOptionPane.showMessageDialog(source, "Username gia' registrato.");
                                    login_error=false;
                                    userText.setText(null);
                                    passwordText.setText(null);
                                    break;
                                }
                            }
                        } catch (IOException ioe){
                            JOptionPane.showMessageDialog(null, "cliente.txt not found", "login error", JOptionPane.ERROR_MESSAGE);
                        }
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(null, "cliente.txt not found", "login error", JOptionPane.ERROR_MESSAGE);
            }

            if (login_error == true) {
                JOptionPane.showMessageDialog(null, "Username e password non validi.", "login error", JOptionPane.ERROR_MESSAGE);
                userText.setText(null);
                passwordText.setText(null);
                frame.dispose();
                AdminView catalogo = new AdminView();
            }       
    }
};

creationAccButton.addActionListener(creationAccButtonListener);

backLoginButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog((Component) e.getSource(),
                "button has been pressed");


    }
});
}

}

在LoginView中,单击“注册”按钮时,您正在创建一个新的SignView(这是正确的)

在SignView中,您正在初始化和设置在main方法中可见的JFrame,就好像这是入口点一样

public static void main(String[] args) {
    JFrame frame = new JFrame("Pagina di Registrazione");
    frame.setSize(500, 450);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    placeComponents(frame);
    frame.setVisible(true);
}
如果您想要创建并显示SignView,那么您需要将该初始化放入SignView构造函数或在构造之后调用的其他初始化方法中。i、 e

public SignView {
    JFrame frame = new JFrame("Pagina di Registrazione");
    frame.setSize(500, 450);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    placeComponents(frame);
    frame.setVisible(true);
}
public SignView {
    JFrame frame = new JFrame("Pagina di Registrazione");
    frame.setSize(500, 450);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    placeComponents(frame);
    frame.setVisible(true);
}