这是我的代码:
public class Main extends JFrame{
static int NoOfUsers;
static String[][] Accounts = new String[NoOfUsers][2];
public static void main(String[] args){
Login();
}
private static void Login() {
final String FileName = "F:/TextFiles/loginaccs.txt";
try {
BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(FileName)));
int NoOfUsersL = Integer.parseInt(file.readLine());
String[][] Accounts = new String[NoOfUsersL][2];
NoOfUsers = NoOfUsersL;
for (int i=0; i<NoOfUsersL; i++) {
Accounts[i][0] = file.readLine();
Accounts[i][1] = file.readLine();
}
for (int i=0; i<NoOfUsersL; i++) {
System.out.println(Accounts[i][0]);
System.out.println(Accounts[i][1]);
}
file.close();
} catch (IOException e) {
System.out.println("ERROR: unable to read file.");
e.printStackTrace();
}
String username = null;
String password = null;
JTextField usernamejtf = new JTextField(username);
JPasswordField passwordjtf = new JPasswordField(password);
String[] buttons = {"Login", "Create new account"};
Object[] InputDialog = {
"Username: ", usernamejtf, "Password: ", passwordjtf
};
do {
int option = JOptionPane.showOptionDialog(null,
InputDialog,
"Login",
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
buttons,
buttons[0]);
System.out.println(option); //Check
if (option == JOptionPane.CLOSED_OPTION ) {
return;
}
else if (option == 0) {
if (CheckAccount(username,password)) {
System.out.println("Logged in");
} else {
System.out.println("Wrong Password/Username");
}
} else if (option == 1) {
System.out.println("Create Account.");
}
} while (!(CheckAccount(username,password)));
}
private static boolean CheckAccount(String username, String password) {
for (int i=0; i>NoOfUsers; i++) {
if ((username == Accounts[i][0]) && (password == Accounts[i][1])) {
return true;
}
}
return false;
}
}
在&#34; main&#34;中,我调用了Login()方法,Eclipse强迫我将单词&#34; static&#34;在方法名称前面。
无论如何,我可以修改程序,以便该行可以写为:
private void Login(){...};
private boolean CheckAccount(...){...}等?
[额外问题:
由于&#34;静态&#34;这个词,我不能把“&#34; public&#34;在String [] [] Accounts = new String [NoOfUsersL] [2];
之前
这使得CheckAccount无法访问Accounts数组。
如何修改程序以解决此问题。]
每个人都提前。
答案 0 :(得分:1)
静态方法可以是调用静态方法(没有实例)所以要调用实例方法首先创建它并从实例调用exampled,如下所示
private void textBox22_TextChanged(object sender, EventArgs e)
{
String path = "Data Source=LOCALHOST; Initial Catalog= sadd; username=root; password=''";
MySqlConnection sqlconn = new MySqlConnection(path); //communicator //constructors
MySqlCommand sqlcomm = new MySqlCommand();
MySqlDataReader sqldr;
sqlconn.Open();
sqlcomm.Connection = sqlconn;
sqlcomm.CommandType = CommandType.Text;
sqlcomm.CommandText = "Select * from approvedrecords where VoucherNumber=" + textBox22.Text + "";
sqldr = sqlcomm.ExecuteReader();
sqldr.Read();
if (sqldr.HasRows)
{
textBox26.Text = sqldr[0].ToString();
}
sqlconn.Close();
if (textBox22.Text == textBox26.Text)
{
String path8 = "Data Source=LOCALHOST; Initial Catalog= sadd; username=root; password=''";
MySqlConnection sqlcon = new MySqlConnection(path8); //communicator //constructors
string query = "select * from approvedrecords where VoucherNumber = " + textBox22.Text + " ";
MySqlCommand cmd = new MySqlCommand(query, sqlcon);
MySqlDataReader dbr;
sqlcon.Open();
dbr = cmd.ExecuteReader();
while (dbr.Read())
{
string a = (string)dbr["CheckNumber"].ToString();
string b = (string)dbr["DateCreated"];
string c = (string)dbr["Status"];
string d = (string)dbr["PayeesName"];
string f = (string)dbr["Amount"].ToString();
string g = (string)dbr["DatePrinted"];
string h = (string)dbr["Particulars"];
string i = (string)dbr["Prepared_by"];
string j = (string)dbr["Payment_received_by"];
textBox21.Text = a;
textBox23.Text = b;
textBox28.Text = c;
textBox20.Text = d;
textBox19.Text = f;
textBox27.Text = g;
textBox18.Text = h;
textBox16.Text = i;
textBox17.Text = j;
}
}
else
{
MessageBox.Show("ID doesn't exist!");
}
答案 1 :(得分:0)
执行new Main().login();
您应遵循java命名约定,变量和方法名称应以小写字母开头。
答案 2 :(得分:0)
使用:
public static void main(String[] args){
Main main = new Main();
main.login();
}
答案 3 :(得分:0)
为了删除静态限定符,必须使所有方法和变量实例变量和方法。然后,您可以实例化JFrame对象并调用login()
。
我还根据Java官方约定将方法/变量名称转换为mixedCase。我认为你开始用VB或C#编程?
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Main extends JFrame {
private static final long serialVersionUID = -105943237549003486L;
private int numOfUsers;
private String[][] accounts;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Main m = new Main();
m.login("F:/TextFiles/loginaccs.txt");
}
});
}
// Constructor, set up instance values here.
public Main() {
super();
}
private void login(final String fileName) {
try {
BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
numOfUsers = Integer.parseInt(file.readLine());
accounts = new String[numOfUsers][2];
for (int i = 0; i < numOfUsers; i++) {
accounts[i][0] = file.readLine();
accounts[i][1] = file.readLine();
}
for (int i = 0; i < numOfUsers; i++) {
System.out.println(accounts[i][0]);
System.out.println(accounts[i][1]);
}
file.close();
} catch (IOException e) {
System.out.println("ERROR: unable to read file.");
e.printStackTrace();
}
String username = null;
String password = null;
JTextField usernamejtf = new JTextField(username);
JPasswordField passwordjtf = new JPasswordField(password);
String[] buttons = { "Login", "Create new account" };
Object[] InputDialog = { "Username: ", usernamejtf, "Password: ", passwordjtf };
do {
int option = JOptionPane.showOptionDialog(null, InputDialog, "Login", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, null, buttons, buttons[0]);
System.out.println(option); // Check
if (option == JOptionPane.CLOSED_OPTION) {
return;
} else if (option == 0) {
if (checkAccount(username, password)) {
System.out.println("Logged in");
} else {
System.out.println("Wrong Password/Username");
}
} else if (option == 1) {
System.out.println("Create Account.");
}
} while (!(checkAccount(username, password)));
}
private boolean checkAccount(String username, String password) {
for (int i = 0; i > numOfUsers; i++) {
if (username == accounts[i][0] && password == accounts[i][1]) {
return true;
}
}
return false;
}
}