弹出两个gui窗口而不是一个

时间:2016-01-30 15:45:41

标签: java eclipse swing user-interface jframe

所以我写了下面的代码:

 package myProject;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.swing.*;

public class GuiTest extends JFrame{
/**
 * 
 */
private static final long serialVersionUID = 1L;

JTextField user = new JTextField();
JTextField pass = new JTextField();
JLabel title = new JLabel("Login");
JLabel usernameGui = new JLabel("Username:");
JLabel passwordGui = new JLabel("Password:");
public String userName;
public String passWord;

//Non GUI variables
public String username;
public String password;
File dir = new File("C:\\Users\\User\\Desktop\\account1.txt");
public boolean pressed = false;


public GuiTest(){
    JFrame window = new JFrame("Position");
    //window.setSize(600, 600);
    window.setBounds(500,200,600,600);
    window.setResizable(false);
    window.setVisible(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setLayout(null);
    window.add(panel);

    //Labels
    panel.add(title);
    title.setBounds(290, 110, 100, 100);
    panel.add(usernameGui);
    usernameGui.setBounds(150,200,150,30);
    panel.add(passwordGui);
    passwordGui.setBounds(150,240,150,30);

    //Text fields
    panel.add(user);
    user.setBounds(230,240,150,30);
    panel.add(pass);
    pass.setBounds(230,200,150,30);

    //Button
    JButton btn = new JButton("Login");
    btn.setBounds(250, 290, 100, 30);
    panel.add(btn);
    btn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            userName = user.getText();
            passWord = pass.getText();  
            pressed = true;
            System.out.println(passWord+" "+password+" "+userName+" "+username);
            //System.out.println(mn.passWord+" "+mn.password+" "+mn.userName+" "+mn.username);
        }
    });
}
public static void main(String[] args){
    GuiTest test = new GuiTest();
    GuiTest mn = new GuiTest();
    try{

        mn.Write("MyUser", "MyPass");

        Scanner scan = new Scanner(mn.dir);
        String text = scan.nextLine();
        scan.close();
        System.out.println(text);

        String[] sep = text.split(" ");

        mn.username = sep[0];
        mn.password = sep[1];
        System.out.println("User: " + mn.username + " pass: " + mn.password);

    }catch(Exception e){
        System.out.println("Error! File didn't create.");
    }

        Scanner usernameIn = new Scanner(System.in);
        Scanner passwordIn = new Scanner(System.in);
        String userIn = usernameIn.nextLine();
        String passIn = passwordIn.nextLine();

        if(mn.userName.equals(mn.username) && mn.passWord.equals(mn.password)){
            System.out.print("Access Granted");
        }else{
            System.out.println("Access Denied");
            System.out.println(mn.passWord+" "+mn.password+" "+mn.userName+" "+mn.username);
        }

}
public void Write(String user, String pass){
    String userONE = user;
    String passONE = pass;
    try{
    PrintWriter file = new PrintWriter(dir);
    file.print(userONE+" "+passONE);
    file.close();
    }catch(Exception e){
        System.out.println("Error! File didn't create.");
    }
}
}

当我运行它时会弹出两个窗口(而不是1)并且我无法测试输入,因为它们都以某种方式使用我猜的变量。有谁知道如何修理它? 提前谢谢。

1 个答案:

答案 0 :(得分:3)

  

当我运行它时会弹出两个窗口(而不是1)

由于您要创建两个相同类的对象。

GuiTest test = new GuiTest();
GuiTest mn = new GuiTest();

只需删除其中一个GuiTest test = new GuiTest();即可。由于您没有使用test对象。