2015-03-23 2 views
1

Это первый раз, когда я задал вопрос. Поэтому, пожалуйста, дружайте.Я не могу сделать текстовый файл моим JTextArea

У меня возникли проблемы с моей программой 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

Пожалуйста, уменьшите образец кода до [Минимальный, Полный и Подтверждаемый пример] (http://stackoverflow.com/help/mcve), и мы будем более вероятно и в состоянии помочь. – dbank

+1

Есть ли какое-то исключение? – Masudul

+0

Если есть исключение, оно, похоже, поймано и потеряно. Вместо этого «JOptionPane» отображается с сообщением «Невозможно найти». @Angelo Maniego В блоке 'catch' добавьте что-то вроде' x.printStackTrace() 'и посмотрите, появляется ли исключение на консоли при запуске. – dbank

ответ

0

Вы заявили текстовую область дважды, один раз локально в конструкторе и один раз глобально

вы будете получать nullPointerException на txtrItemId.setText();

подмену: JTextArea txtrItemId = new JTextArea();

до: txtrItemId = new JTextArea();

Когда вы пытаетесь получить доступ к txtrItemId в обработчике событий, вы получаете доступ к тому, который объявлен глобально, где, как вы инициализировали переменную, объявленную в конструкторе, область действия этой переменной (объявленной в конструкторе) будет ограничена только к этому методу/конструктору.

Смежные вопросы