我无法使文本文件出现在我的JTextArea中

时间:2015-03-23 07:31:52

标签: java eclipse jtextarea filereader

这是我第一次发帖提问。所以请保持友好。

我的java程序出现问题,我的JTextArea无法显示文本文件,我正在使用Eclipse Luna

这是我的计划:

public class Frame_Cashier extends JFrame {
    String[] category = {"Breakfast","Drinks","Add-ons","Desserts","Happy Meal","Value Meal"};
    JComboBox comboBox = new JComboBox(category);
    private JPanel contentPane;
    JTextArea txtrItemId;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {        
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Frame_Cashier frame = new Frame_Cashier();
                    frame.setVisible(true);
                    frame.setLocationRelativeTo(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Frame_Cashier(){

        Container c = getContentPane();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 759, 469);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JMenuBar menuBar = new JMenuBar();
        menuBar.setBackground(new Color(178, 34, 34));
        menuBar.setBounds(0, 0, 752, 21);
        contentPane.add(menuBar);

        JMenu mnFile = new JMenu("File");
        mnFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        mnFile.setForeground(new Color(255, 255, 255));
        mnFile.setBackground(new Color(255, 69, 0));
        mnFile.setFont(new Font("MV Boli", Font.BOLD, 15));
        JMenuItem transaction = new JMenuItem("Transaction");
        transaction.setForeground(new Color(255, 255, 255));
        transaction.setBackground(new Color(178, 34, 34));
        transaction.setFont(new Font("MV Boli", Font.BOLD, 12));
        JMenuItem logout = new JMenuItem("Logout");
        logout.setForeground(new Color(255, 255, 255));
        logout.setBackground(new Color(178, 34, 34));
        logout.setFont(new Font("MV Boli", Font.BOLD, 12));
        menuBar.add(mnFile);
        mnFile.add(transaction);
        mnFile.add(logout);

        JLabel img_background = new JLabel("");
        img_background.setBackground(new Color(255, 51, 0));

        Image img = new ImageIcon(this.getClass().getResource("cashier_img.jpg")).getImage();

        JTextArea textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setBorder(new LineBorder(new Color(0, 0, 0), 3, true));
        textArea.setBackground(new Color(255, 255, 204));
        textArea.setBounds(380, 32, 353, 280);
        contentPane.add(textArea);

        JTextArea txtrItemId = new JTextArea();
        txtrItemId.setBackground(new Color(255, 255, 204));
        txtrItemId.setToolTipText("Item ID / Item Name");
        txtrItemId.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
        txtrItemId.setBounds(20, 114, 119, 150);
        txtrItemId.setEditable(false);
        contentPane.add(txtrItemId);

        comboBox.setSelectedIndex(1);
        comboBox.setSelectedIndex(5);
        comboBox.setBackground(new Color(255, 255, 204));
        comboBox.setBounds(20, 61, 338, 21);
        contentPane.add(comboBox);

        JTextArea textArea_2 = new JTextArea();
        textArea_2.setEditable(false);
        textArea_2.setBackground(new Color(255, 255, 204));
        textArea_2.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
        textArea_2.setBounds(175, 114, 72, 150);
        contentPane.add(textArea_2);

        JTextArea textArea_3 = new JTextArea();
        textArea_3.setEditable(false);
        textArea_3.setBackground(new Color(255, 255, 204));
        textArea_3.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
        textArea_3.setBounds(273, 114, 72, 150);
        contentPane.add(textArea_3);

        JTextArea textArea_1 = new JTextArea();
        textArea_1.setBackground(new Color(255, 255, 204));
        textArea_1.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        textArea_1.setBounds(63, 290, 119, 22);
        contentPane.add(textArea_1);

        JTextArea txtrNoImage = new JTextArea();
        txtrNoImage.setBackground(new Color(255, 255, 204));
        txtrNoImage.setBorder(new LineBorder(new Color(0, 0, 0), 3, true));
        txtrNoImage.setText("No Image");
        txtrNoImage.setBounds(202, 274, 144, 126);
        contentPane.add(txtrNoImage);

        JLabel lblPrice = new JLabel("Price");
        lblPrice.setForeground(new Color(255, 204, 51));
        lblPrice.setFont(new Font("MV Boli", Font.BOLD, 12));
        lblPrice.setBounds(281, 89, 64, 14);
        contentPane.add(lblPrice);

        JLabel lblNewLabel_3 = new JLabel("Stock");
        lblNewLabel_3.setForeground(new Color(255, 204, 51));
        lblNewLabel_3.setFont(new Font("MV Boli", Font.BOLD, 12));
        lblNewLabel_3.setBounds(186, 89, 48, 14);
        contentPane.add(lblNewLabel_3);

        JLabel lblNewLabel_2 = new JLabel("Item ID / Item Name");
        lblNewLabel_2.setForeground(new Color(255, 204, 51));
        lblNewLabel_2.setFont(new Font("MV Boli", Font.BOLD, 12));
        lblNewLabel_2.setBounds(10, 89, 157, 14);
        contentPane.add(lblNewLabel_2);

        JLabel lblNewLabel_1 = new JLabel("Item I.D");
        lblNewLabel_1.setForeground(new Color(255, 204, 0));
        lblNewLabel_1.setFont(new Font("MV Boli", Font.BOLD, 12));
        lblNewLabel_1.setBounds(0, 290, 64, 22);
        contentPane.add(lblNewLabel_1);

        JLabel lblNewLabel = new JLabel("Select Category");
        lblNewLabel.setFont(new Font("MV Boli", Font.BOLD, 13));
        lblNewLabel.setForeground(new Color(255, 204, 0));
        lblNewLabel.setBounds(10, 32, 134, 31);
        contentPane.add(lblNewLabel);

        JButton btnNewButton_1 = new JButton("Void");
        btnNewButton_1.setBackground(SystemColor.controlDkShadow);
        btnNewButton_1.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
        btnNewButton_1.setForeground(new Color(255, 215, 0));
        btnNewButton_1.setFont(new Font("MV Boli", Font.BOLD, 13));
        btnNewButton_1.setBounds(634, 340, 99, 31);
        contentPane.add(btnNewButton_1);

        JButton btnNewButton = new JButton("Pay");
        btnNewButton.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
        btnNewButton.setFont(new Font("MV Boli", Font.BOLD, 15));
        btnNewButton.setForeground(new Color(255, 215, 0));
        btnNewButton.setBackground(SystemColor.controlDkShadow);
        btnNewButton.setBounds(380, 340, 99, 31);
        contentPane.add(btnNewButton);

        JButton btnNewButton_2 = new JButton("Edit");
        btnNewButton_2.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
        btnNewButton_2.setBackground(SystemColor.controlDkShadow);
        btnNewButton_2.setFont(new Font("MV Boli", Font.BOLD, 13));
        btnNewButton_2.setForeground(new Color(255, 215, 0));
        btnNewButton_2.setBounds(513, 340, 89, 31);
        contentPane.add(btnNewButton_2);

        img_background.setIcon(new ImageIcon(img));
        img_background.setAlignmentX(Component.CENTER_ALIGNMENT);
        img_background.setBounds(0, 21, 752, 410);
        contentPane.add(img_background);

        combohandler1 ch1 = new combohandler1();
        comboBox.addActionListener(ch1);
    }

    private class combohandler1 implements ActionListener {
    public void actionPerformed(ActionEvent e){
            String item1,item2,item3,item4,item5,item6; 
            if(comboBox.getSelectedItem().equals("Breakfast")){
                try{
                        FileReader file = new FileReader("breakfast.txt");

                        BufferedReader br = new BufferedReader(file);

                        item1 = br.readLine();
                        item2 = br.readLine();
                        item3 = br.readLine();
                        item4 = br.readLine();
                        item5 = br.readLine();

                        System.out.println("test.txt");
                        txtrItemId.setText("\n"+item1+"\n"+item2+"\n"+item3+"\n"+item4+"\n"+item5+"\n");

                        br.close();
                    }catch(Exception x){
                    JOptionPane.showMessageDialog(null,"Unable to find");               
                }           
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您已将文本区域声明两次,一次在构造函数中本地,一次全局

您将在nullPointerException

获得txtrItemId.setText();

更改:JTextArea txtrItemId = new JTextArea();

至:txtrItemId = new JTextArea();

当您尝试在事件处理程序中访问txtrItemId时,您正在访问全局声明的那个,因为您初始化了构造函数中声明的变量,此变量的范围(在构造函数中声明)将仅限于该方法/构造函数。