你好。我是新来的。对不起。我正在尝试使用构造函数将此文件 reader 转换为 运行 ...但坦率地说。它不工作

Hello. I'm new. Sorry. I'm trying to get this file reader to run with the constructor... But Frankly. It's not working

import java.io.FileReader;  import java.util.Scanner; import java.io.*;
import javax.swing.JButton; import javax.swing.JTextField;      //JSwing components
import javax.swing.JFrame;  import javax.swing.JPasswordField;
import javax.swing.JLabel;  import javax.swing.JOptionPane;
import javax.swing.JPanel;  import javax.swing.JTextArea;
import java.awt.FlowLayout;  import java.awt.GridBagLayout;     //Layouts
import java.awt.event.ActionListener;                           //Listeners
import java.awt.event.ActionEvent;
import javax.swing.*; //In case I missed something ;)
import java.awt.*;

 public class Bancud_Project extends JFrame implements ActionListener{
    //Declare global variables
    double ttlamount, inv;

    //Clock
    JLabel lbltime = new JLabel("Time");

    //[L1]Fields                                                //[L2]Declare JButton
    JTextField tboxusrnm = new JTextField("",10);           JButton btnlogin = new JButton("OK");   
    JPasswordField tboxpswd = new JPasswordField("",10);    JButton btncancel = new JButton("CANCEL");
                                                            JButton btnclear = new JButton("CLEAR");




    JFrame mainfrm = new JFrame();
    public void login(){ //Login code

    //[L1]Labels
    JLabel lbltitle = new JLabel("Welcome to Tek Shak!");  JLabel lbl1 = new JLabel("Please Log In.");
    JLabel lbl3 = new JLabel("Password: ");  JLabel lbl2 = new JLabel("Username: ");

    //[n1]Fonts
    lbltitle.setFont(new Font("Agency FB",Font.BOLD, 25));
    lbl1.setFont(new Font("Agency FB",Font.BOLD, 18));
    lbl2.setFont(new Font("Consolas",Font.PLAIN, 18));
    lbl3.setFont(new Font("Consolas",Font.PLAIN, 18));



    tboxusrnm.addActionListener(this);
        tboxpswd.addActionListener(this);
            btncancel.addActionListener(this);
                btnlogin.addActionListener(this);
                    btnclear.addActionListener(this);


    JPanel logindisplay = new JPanel();
    logindisplay.setLayout(null); //Set Applicable for bounds


//Positioning//setBounds(x,y,L,W);  //Add to panel   //Color  //Extras

lbltitle.setBounds(20,20,200,30); logindisplay.add(lbltitle); lbltitle.setForeground(Color.white); 
lbl1.setBounds(20,60,150,30);     logindisplay.add(lbl1);     lbl1.setForeground(Color.white);
lbl2.setBounds(20,100,150,30);    logindisplay.add(lbl2);     lbl2.setForeground(Color.white);
lbl3.setBounds(20,140,150,30);    logindisplay.add(lbl3);     lbl3.setForeground(Color.white);
tboxusrnm.setBounds(130,100,150,30); logindisplay.add(tboxusrnm); 
tboxpswd.setBounds(130,140,150,30);  logindisplay.add(tboxpswd);   //lbl3.setToolTipText("Password");
btnlogin.setBounds(20,190,120,30);  logindisplay.add(btnlogin);    btnlogin.setForeground(Color.white); btnlogin.setBackground(Color.darkGray);
btncancel.setBounds(20,225,260,30);  logindisplay.add(btncancel); btncancel.setForeground(Color.white); btncancel.setBackground(Color.darkGray);
btnclear.setBounds(140,190,140,30);  logindisplay.add(btnclear); btnclear.setForeground(Color.white);  btnclear.setBackground(Color.darkGray);

        logindisplay.setBackground(Color.darkGray);
        mainfrm.add(logindisplay);
        mainfrm.setTitle("TekShak Login");  
        mainfrm.setVisible(true);
        mainfrm.setBounds(700,400,310,300); 
        mainfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

public static void main(String[] args) throws FileNotFoundException { //Main
    {
    Bancud_Project run = new Bancud_Project();
    run.login();

    }
    try 
    {
       Scanner userName = new Scanner(new FileReader("employeelist.txt"));
    int id = 0; 
    String username = " ";
        while(userName.hasNext()){
        username = userName.next();
        id = userName.nextInt();

    }
        System.out.println("Username: " +username);
        System.out.println("UserId: " +id);
        userName.close();
    }
    catch (FileNotFoundException ex)
    {
    System.out.print("Error. Error. Path Not found");
    }

  }//Main end




public void actionPerformed(ActionEvent e){//Action Listener
//Tooltip Extras
    tboxusrnm.setToolTipText("Username"); btnlogin.setToolTipText("Log-in!");
    tboxpswd.setToolTipText("Password");  btnclear.setToolTipText("Clears both fields");
    btncancel.setToolTipText("Exits the program");

    if (e.getSource() == btnlogin || e.getSource() == tboxpswd) {       //If Enter pressed or password entered
        String totest = ("cashier"); //Change this to employee no. someday  

        if(totest.equals(tboxusrnm.getText()) && totest.equals(tboxpswd.getText())){
            mainfrm.setVisible(false);
            //try{
            //cashier Start = new cashier();
        //  }
            //catch(FileNotFoundException ex){
            cashier Start = new cashier();
            Start.machina();
        } else {
            tboxusrnm.setText("");
            tboxpswd.setText("");
            JOptionPane.showMessageDialog(null,"Login Incorrect. Please check your login details.","Login Username or Password Incorrect", JOptionPane.ERROR_MESSAGE);
        }

    } else if (e.getSource() == btncancel){
    System.exit(0);                                                     //Kill the program
    } else if (e.getSource() == btnclear){
    tboxusrnm.setText("");
    tboxpswd.setText("");
    }


}







class cashier extends JFrame { //New class
    //Set global variables
    String[] categories = {"Consoles","Credits","Gadgets","Gaming Gear","Robotic Kits"};
    String dreamgirl = "Lindee";
    long itemid = 10000; 
    //             \\\     
    //ID Format  Cat-No-Type-Amt


    //[INI] Components
    JPanel pnlmain = new JPanel();
    //Buttons
    JButton btnpay = new JButton("Payment"); JButton btnedit = new JButton("Edit");  
    JButton btndelete = new JButton("Delete");  JButton btncncl = new JButton("Cancel");
    JButton btnadd = new JButton("Enter");

    //Other Components
    JTextArea rcptdisp = new JTextArea("Receipt Displayed here");
    JComboBox selection = new JComboBox(categories);
    JList stocks = new JList();  JList items = new JList();  JList price = new JList();
    JLabel lbltotal = new JLabel("Total:"); JLabel txttotal = new JLabel("0.00"); JLabel lblid = new JLabel("ID:");
    JTextField idinput = new JTextField(10);
    //All about menus
    JMenuBar menu = new JMenuBar(); JMenu file = new JMenu("File"); 
    JMenuItem LogOut = new JMenuItem("Log Out"); JMenuItem NewTransaction = new JMenuItem("New Transaction");

    //Data Sets
//*   

   //JFrame
   JFrame mainprg = new JFrame();

public void machina() throws FileNotFoundException {//////////////////////////////////MACHINA------->

    //Scanner productInv = new Scanner(new FileReader("productinv.txt"));

    System.out.print("Error. I am not a robot!");


    menu.add(file);
    file.add(LogOut);
    file.add(NewTransaction);

    //[TLP]Topleftpanel components //Extra code For debugging
    JPanel pnltopleft = new JPanel(); pnltopleft.setLayout(null); pnltopleft.setBackground(Color.black);
    JPanel pnlitemdsp = new JPanel();
    JPanel pnlaction = new JPanel(); 

    //[DSN]JPanel Formatting
    pnlitemdsp.setLayout(new FlowLayout());
    pnlitemdsp.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Items")); pnlitemdsp.setBounds(20,60,500,300); pnlitemdsp.setBackground(Color.lightGray);
    pnlitemdsp.setLayout(null); 
    pnlitemdsp.add(stocks, BorderLayout.WEST); stocks.setBounds(15,20,110,260); stocks.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Stocks"));
    pnlitemdsp.add(items, BorderLayout.CENTER); items.setBounds(135,20,220,260); items.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "ItemID//Item"));
    pnlitemdsp.add(price, BorderLayout.EAST); price.setBounds(365,20,125,260); price.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Price"));
    pnltopleft.add(pnlaction); pnlaction.setBounds(20,370,500,170); pnlaction.add(lblid);
    pnltopleft.add(pnlitemdsp);

    ///[ACT]Action Display Panel
    pnlaction.setLayout(null);
    pnlaction.add(btnpay);    btnpay.setBounds(10,20,100,40); btnpay.setBackground(Color.lightGray);
    pnlaction.add(btnedit);   btnedit.setBounds(115,20,100,40);                      btnedit.setBackground(Color.lightGray);
    pnlaction.add(btndelete); btndelete.setBounds(220,20,100,40);                        btndelete.setBackground(Color.lightGray); 
    pnlaction.add(btncncl);   btncncl.setBounds(325,20,100,40);                       btncncl.setBackground(Color.lightGray);
    pnlaction.add(btnadd);    btnadd.setBounds(10,70,100,20);                        btnadd.setBackground(Color.lightGray);
    pnlaction.add(idinput);   idinput.setBounds(140,70,200,22);
    pnlaction.add(lblid);     lblid.setBounds(120,70,40,22);
    //[DSN]Custom positions //setBounds(x,y,L,W);  //Add to panel   //Color  //Extras
    selection.setBounds(20,20,500,20); pnltopleft.add(selection); selection.setEditable(false);



    //[TRP]Toprightpanel components //Extracode For debugging
    JPanel pnltopright = new JPanel(); pnltopright.setLayout(null); pnltopright.setBackground(Color.lightGray);
    pnltopright.add(rcptdisp); rcptdisp.setBounds(5,255,440,220); rcptdisp.setBackground(Color.white);
    pnltopright.add(lbltotal); lbltotal.setBounds(10,490,50,30);    lbltotal.setFont(new Font("Agency FB",Font.BOLD, 20));
    pnltopright.add(txttotal); txttotal.setBounds(400,490,50,30);   txttotal.setFont(new Font("Agency FB",Font.BOLD, 20));
    JPanel pnlpic = new JPanel(); pnltopright.add(pnlpic); pnlpic.setBounds(5,5,440,230); pnlpic.setBackground(Color.black);

    //Add right to left
    pnltopleft.add(pnltopright);
    pnltopright.setBounds(530,20,450,520);
    //[PCP]PicturePanel



    //[MAIN]Payload 
    mainprg.setTitle("Cashier_Bancud");
    mainprg.add(pnltopleft); 
    mainprg.setJMenuBar(menu);  
    mainprg.setBounds(10,10,1005,610); ///Program size
    mainprg.setDefaultCloseOperation(EXIT_ON_CLOSE);
    mainprg.setVisible(true);   

    //[DTA]Data Importer
//*     while(productInv.hasNext()){
 //*        itemid=productInv.nextLong();
        //(itemid = 10000; itemid < 10000; itemid++ )

        //Simply do the same thing in I/o With the scanner to display values
    }
}//Cashier end  
}//End Of The Line        of code XD

主要错误 是错误:未报告的异常FileNotFoundException;必须被抓住或宣布被抛出 Start.machina();

抱歉文本格式问题。我知道该代码使用已弃用的 API,但我找不到任何替代方法。请帮忙。谢谢!

你必须赶上 FileNotFoundException

try {
    Start.machina();
} catch(FileNotFoundException ex) {
    // take actions
}

您要在 catch 块中执行的操作取决于您的应用程序逻辑。