2012-08-27 41 views
1

如何从另一个类访问输入的“用户名”值?在包里面 我在编码时遇到了麻烦。我应该宣布一些变量公开吗? 我声明了用户名市民,但我得到的错误如何从类中访问字符串

这是我的代码:

package login; 

import java.awt.HeadlessException; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.*; 
import javax.swing.JOptionPane; 

@SuppressWarnings("serial") 
public class Login extends javax.swing.JFrame { 

    public Login() { 
     initComponents0(); 
    } 

    @SuppressWarnings("unchecked") 

    private void initComponents0() { 

     jLabel1 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 
     uname = new javax.swing.JTextField(); 
     jLabel3 = new javax.swing.JLabel(); 
     login = new javax.swing.JButton(); 
     reset = new javax.swing.JButton(); 
     pwd = new javax.swing.JPasswordField(); 
     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N 
     jLabel1.setText("Login Pane"); 
     jLabel2.setText("User Name:"); 
     jLabel3.setText("Password:"); 
     login.setText("Login"); 
     login.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
     String un = uname.getText(); 
     @SuppressWarnings("deprecation") 
     String pw = pwd.getText(); 
     try{ 
       FileInputStream fstream = new FileInputStream("data.dat"); 
       try (DataInputStream in = new DataInputStream(fstream)) { 
       BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
       String strLine; 
       boolean registered = false; 
       boolean registered0 = false; 
       while ((strLine = br.readLine()) != null) { 
       String values[] = strLine.split("\\|"); 
       if ((strLine.startsWith(un))&&(pw.equals(values[1]))){ 
       registered = true; 
       break; 
       } 
       if ((strLine.startsWith(un))&&(!pw.equals(values[1]))){ 
       registered0 = true; 
       break; 
       } 
       } 
       if(registered){ 
        JOptionPane.showMessageDialog(null,"Hello: "+un ,"Registration",JOptionPane.INFORMATION_MESSAGE); 
        File file = new File("temp.dat"); 
       try { 
           try (FileWriter writer = new FileWriter(file, false)) { 
           String data0 = un; 
           writer.write(data0); 
           }} 
       catch (IOException | HeadlessException z) { 
           JOptionPane.showMessageDialog(null, e); 
           } 
       } 
       else if(registered0){JOptionPane.showMessageDialog(null,"It seems you entered a wrong password! \n Please try again " ,"Admin",JOptionPane.INFORMATION_MESSAGE);} 
       else 
       { 
       int sel = JOptionPane.showConfirmDialog(null,"It seems that you haven't registered yet? \n Launch Registration Pane?","Admin",JOptionPane.INFORMATION_MESSAGE); 
       if (sel == JOptionPane.YES_OPTION){ 
       java.awt.EventQueue.invokeLater(new Runnable() { 
       @Override 
       public void run() { 
       new Register().setVisible(true); 
       }});} 

       }}} 
       catch ( IOException | HeadlessException ez){ 
       JOptionPane.showMessageDialog(null,"A null file was created in order to \n avoid File Catch errors","Admin",JOptionPane.INFORMATION_MESSAGE); 
       File file = new File("data.dat"); 
       try { 
           try (FileWriter writer = new FileWriter(file, true)) { 
           String data0 = "null"; 
           String data1 = "null"; 
           writer.write(data0+" | "+data1+"\n"); 
           }} 
       catch (IOException | HeadlessException z) { 
           JOptionPane.showMessageDialog(null, e); 
           } 
       } 
     }}); 
     reset.setText("Reset Field"); 
     reset.addActionListener(new ActionListener() { 
          @Override 
          public void actionPerformed(ActionEvent e) { 
           uname.setText(""); 
           pwd.setText(""); 
           } 
           }); 
     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
        .addComponent(jLabel1) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jLabel2) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(uname, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jLabel3) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addGroup(layout.createSequentialGroup() 
           .addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE) 
           .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
           .addComponent(reset, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)) 
          .addComponent(pwd)))) 
       .addContainerGap(30, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jLabel1) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel2) 
        .addComponent(uname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel3) 
        .addComponent(pwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(login) 
        .addComponent(reset)) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 

     pack(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
     @Override 
      public void run() { 
       new Login().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JButton login; 
    private javax.swing.JPasswordField pwd; 
    private javax.swing.JButton reset; 
    private javax.swing.JTextField uname; 
    // End of variables declaration 
} 

编辑

package login; 

import java.awt.HeadlessException; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.*; 
import javax.swing.JOptionPane; 

@SuppressWarnings("serial") 
public class Login extends javax.swing.JFrame { 
private String username,password; 
public String getUsername() { 
return username; 
} 
public String getPassword() { 
return password; 
} 

    public Login() { 
     initComponents0(); 
    } 

    @SuppressWarnings("unchecked") 

    private void initComponents0() { 

     jLabel1 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 
     uname = new javax.swing.JTextField(); 
     jLabel3 = new javax.swing.JLabel(); 
     login = new javax.swing.JButton(); 
     reset = new javax.swing.JButton(); 
     pwd = new javax.swing.JPasswordField(); 
     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N 
     jLabel1.setText("Login Pane"); 
     jLabel2.setText("User Name:"); 
     jLabel3.setText("Password:"); 
     login.setText("Login"); 
     login.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
     String un = uname.getText(); 
     @SuppressWarnings("deprecation") 
     String pw = pwd.getText(); 
     username = un; 
     password = pw; 

     try{ 
       FileInputStream fstream = new FileInputStream("data.dat"); 
       try (DataInputStream in = new DataInputStream(fstream)) { 
       BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
       String strLine; 
       boolean registered = false; 
       boolean registered0 = false; 
       while ((strLine = br.readLine()) != null) { 
       String values[] = strLine.split("\\|"); 
       if ((strLine.startsWith(un))&&(pw.equals(values[1]))){ 
       registered = true; 
       break; 
       } 
       if ((strLine.startsWith(un))&&(!pw.equals(values[1]))){ 
       registered0 = true; 

       break; 
       } 
       } 
       if(registered){ 

        username = un; 
        password = pw; 

        JOptionPane.showMessageDialog(null,"Hello: "+un ,"Registration",JOptionPane.INFORMATION_MESSAGE); 
        File file = new File("temp.dat"); 
       try { 
           try (FileWriter writer = new FileWriter(file, false)) { 
           String data0 = un; 
           writer.write(data0); 
           }} 
       catch (IOException | HeadlessException z) { 
           JOptionPane.showMessageDialog(null, e); 
           } 
       } 
       else if(registered0){JOptionPane.showMessageDialog(null,"It seems you entered a wrong password! \n Please try again " ,"Admin",JOptionPane.INFORMATION_MESSAGE);} 
       else 
       { 
       int sel = JOptionPane.showConfirmDialog(null,"It seems that you haven't registered yet? \n Launch Registration Pane?","Admin",JOptionPane.INFORMATION_MESSAGE); 
       if (sel == JOptionPane.YES_OPTION){ 
       java.awt.EventQueue.invokeLater(new Runnable() { 
       @Override 
       public void run() { 
       new Register().setVisible(true); 
       }});} 

       }}} 
       catch ( IOException | HeadlessException ez){ 
       JOptionPane.showMessageDialog(null,"A null file was created in order to \n avoid File Catch errors","Admin",JOptionPane.INFORMATION_MESSAGE); 
       File file = new File("data.dat"); 
       try { 
           try (FileWriter writer = new FileWriter(file, true)) { 
           String data0 = "null"; 
           String data1 = "null"; 
           writer.write(data0+" | "+data1+"\n"); 
           }} 
       catch (IOException | HeadlessException z) { 
           JOptionPane.showMessageDialog(null, e); 
           } 
       } 
     }}); 
     reset.setText("Reset Field"); 
     reset.addActionListener(new ActionListener() { 
          @Override 
          public void actionPerformed(ActionEvent e) { 
           uname.setText(""); 
           pwd.setText(""); 
           } 
           }); 
     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
        .addComponent(jLabel1) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jLabel2) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(uname, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jLabel3) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addGroup(layout.createSequentialGroup() 
           .addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE) 
           .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
           .addComponent(reset, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)) 
          .addComponent(pwd)))) 
       .addContainerGap(30, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jLabel1) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel2) 
        .addComponent(uname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel3) 
        .addComponent(pwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(login) 
        .addComponent(reset)) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 

     pack(); 
    } 

    /* 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
     @Override 
      public void run() { 
       new Login().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JButton login; 
    private javax.swing.JPasswordField pwd; 
    private javax.swing.JButton reset; 
    private javax.swing.JTextField uname; 
    // End of variables declaration 
} 

回答

2

private全球非静态字段来保存usernamepassword领域,当您接受输入时,将值分配给您的全局变量usernamepassword。然后获得public的getter方法,并返回该实例的usernamepassword

类似:

public class Login extends javax.swing.JFrame { 
private String username,password;//assign private global fields for the instance 

//the variables are assigned when you accept user input 

public String getUsername() { 
return username; 
} 
public String getPassword() { 
return password; 
} 
} 

,那么你会做这样的事情:

Login lg=new Login();//create new instance to gain access to getter methods 
//wait for it to return or until user has enetered the credentials 
System.out.println(lg.getUsername()); 
System.out.println(lg.getPassword()); 
+0

请给我看一些代码:) – JFetz2191

+0

@ JFetz2191看到编辑后 –

+0

你是什么意思**全球**? –

2

你需要对你的私有变量公共getter和setter方法,以打电话或编辑您的私有变量从你的班级外面。这被称为encapsulationOOP的重要概念之一。你可能想知道更多谷歌。

1

在全局范围内声明该变量,并使用Public访问说明符声明这些变量。或

使用getter方法为Private声明变量。并使这些getter方法成为Public