如何在java

时间:2017-11-23 07:17:33

标签: java

我试图将我的登录类链接到我的笔记本电脑课程,但是当我尝试运行该程序时,它只显示"登录成功"当我按下它时,它显示为一个空白帧。谁能告诉我为什么?我已经尝试使用已执行的按钮操作,但仍然显示空白帧。

登录类

import java.awt.*;
import java.io.*;
import javax.swing.*;

public class login extends JFrame{
    JFrame frame = new JFrame(); 
    JButton btnLogin;
    JTextField fieldPass, fieldUser;
    JLabel lblUser, lblPass;    
    String filePath="password.txt";
    String filePathUser="userrname.txt";
    String password="";
    String username="";
    BufferedWriter bw=null;
    BufferedWriter bw2=null;
    FileWriter fw=null;
    FileWriter fw2=null;
    File textFile=new File(filePath);
    File textFileUser=new File(filePathUser);
    Boolean login=false;

    public login() {
        function();
    }

    private void readFile() throws FileNotFoundException{
        String user="";
        String pass="";
        boolean passFound=false;
        boolean userFound=false;
        BufferedReader passReader=null;
        BufferedReader userReader=null;
        password=fieldPass.getText();
        username=fieldUser.getText();
        boolean notFound=false;
        try{
            passReader=new BufferedReader(new FileReader("D:\\sementara buat ngoding\\password.txt"));
            userReader=new BufferedReader(new FileReader("D:\\sementara buat ngoding\\username.txt"));
            do{
                user=userReader.readLine();
                if (username.equalsIgnoreCase(user)) {
                    userFound=true;
                }
                pass=passReader.readLine();
                if (password.equalsIgnoreCase(pass)) {
                    passFound=true;
                }
                if (pass==null||user==null) {
                    notFound=true;
                }
            }while(!userFound&&!passFound&&!notFound);
            if (userFound&&passFound) {
                login=true;
            }
        }
        catch(IOException ex){

        }
        finally{
            try{
                if (passReader!=null) {
                    passReader.close();
                }
                if (userReader!=null) {
                    userReader.close();
                }
            }
            catch(IOException ioe){
                JOptionPane.showMessageDialog(null, "close error");
            }
        }
    }
    @SuppressWarnings("unchecked")
    private void function() {

        btnLogin = new JButton();
        fieldPass = new JTextField();
        fieldUser = new JTextField();
        lblUser = new JLabel();
        lblPass = new JLabel();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btnLogin.setText("LOGIN");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);

            }
        });

        fieldPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fieldPassActionPerformed(evt);
            }
        });

        lblUser.setText("USERNAME:");

        lblPass.setText("PASSWORD:");

    }

    private void fieldPassActionPerformed(java.awt.event.ActionEvent evt) {

    }                                         

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {

        try{
            readFile();
            if (login==true) {
                JOptionPane.showMessageDialog(null, "login Success");
                Laptop lapcl = new Laptop();
                lapcl.setVisible(true);

            }
            else{
                JOptionPane.showMessageDialog(null, "Login Failed");
            }
        }
        catch(IOException ioe){

        }

    }
    public static void main(String args[]) {
        login LOG = new login();

        JFrame frame = new JFrame();
        JPanel panel = new JPanel();


        panel.setSize(500, 200);
        panel.setBackground(Color.decode("#a020f0"));

        LOG.lblUser.setBounds(72, 30, 100, 30); 
        LOG.lblUser.setBackground(Color.decode("#a020f0"));
        LOG.lblUser.setFont(new Font("Arial",Font.BOLD,14));
        LOG.fieldUser.setBounds(202, 30, 200, 30); 

        LOG.lblPass.setBounds(72, 60, 100, 30); 
        LOG.lblPass.setBackground(Color.decode("#a020f0"));
        LOG.lblPass.setFont(new Font("Arial",Font.BOLD,14));
        LOG.fieldPass.setBounds(202, 60, 200, 30);

        LOG.btnLogin.setBounds(302, 100, 100, 30); 
        LOG.btnLogin.setFont(new Font("Arial",(Font.BOLD),12));
        LOG.btnLogin.setBackground(Color.decode("#7d26cd"));

        frame.setSize(500, 200);
        frame.setVisible(true);
        frame.add(LOG.lblUser);
        frame.add(LOG.fieldPass);
        frame.add(LOG.lblPass);
        frame.add(LOG.fieldUser);
        frame.add(LOG.btnLogin);
        frame.add(panel);
    }
}

笔记本电脑课程

import java.awt.*;
import java.io.*;
import javax.swing.*;

public class Laptop extends JFrame{
    JFrame frame = new JFrame(); 
    JButton btnLaptop, btnTrans, btnHist;


    public Laptop(){
        function();
    }

    private void function() {
        btnLaptop = new JButton();
        btnTrans = new JButton();
        btnHist = new JButton();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btnLaptop.setText("Laptop Data");

        btnTrans.setText("Transaction");

        btnHist.setText("Transaction History");



    }

    public static void main(String[] args) {
        Laptop LAP = new Laptop();

        JFrame frame = new JFrame();
        JPanel panel = new JPanel();

        panel.setSize(900, 600);
        panel.setBackground(Color.decode("#a020f0"));

        LAP.btnLaptop.setBounds(20, 20, 120, 30); 
        LAP.btnLaptop.setFont(new Font("Arial",(Font.BOLD),15));
        LAP.btnLaptop.setBackground(Color.decode("#7d26cd"));

        LAP.btnTrans.setBounds(160, 20, 120, 30); 
        LAP.btnTrans.setFont(new Font("Arial",(Font.BOLD),15));
        LAP.btnTrans.setBackground(Color.decode("#7d26cd"));

        LAP.btnHist.setBounds(300, 20, 120, 30); 
        LAP.btnHist.setFont(new Font("Arial",(Font.BOLD),15));
        LAP.btnHist.setBackground(Color.decode("#7d26cd"));

        frame.setVisible(true);
        frame.setSize(900, 600);
        frame.add(LAP.btnLaptop);
        frame.add(LAP.btnTrans);
        frame.add(LAP.btnHist);
        frame.add(panel);
    }
}

1 个答案:

答案 0 :(得分:0)

您正在运行login.class文件。所以Laptop类的主要功能从未调用过。 你可以把你已经完成的事情做成的事情放到Laptop类的主要方法中。 所以你的btnLoginActionPerformed登录类方法就像这样......

btnLoginActionPerformed

您还可以删除Laptop类的主要方法。因为它没用。