2017-07-10 49 views
0

我有一个类LoginView这样的:如何在侦听器(java)中设置可见帧?

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并执行它运行的代码。在注册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"); 


    } 
}); 
} 

} 

回答

0

在LoginView你正在创建一个新的SignView点击注册按钮时(这是正确的)。

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

在SignView要初始化和设置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构造或者它建成后这就是所谓的一些其他的初始化方法。即

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