2015-01-09 63 views
1

我想讓我的程序使用JFileChooser加載一個txt文件,但它似乎不工作。當我按下JButton時,控制檯給了我很多錯誤。以下是目前爲止的完整代碼:JFileChooser崩潰 - Java 7

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

public class Sudoku extends JFrame{ 
    JPanel mainWindow = new JPanel(); 
    JPanel buttonWindow = new JPanel(); 
    JPanel sudokuArea = new JPanel(); 
    JButton load = new JButton("Load"); 
    JButton solve = new JButton("Solve"); 
    JTextArea sudokuGrid = new JTextArea(); 
    Field field = new Field(); 

    public static void main(String[] args) { 

    new Sudoku(); 
    } 

    public Sudoku(){ 
     super("SudokuSolver"); 
     setSize(200,300); 
     setResizable(false); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     add(mainWindow); 

     mainWindow.setLayout(new BorderLayout()); 
     mainWindow.add(buttonWindow, BorderLayout.SOUTH); 
     mainWindow.add(sudokuArea, BorderLayout.CENTER); 

     buttonWindow.add(load); 
     buttonWindow.add(solve); 

     sudokuArea.setLayout(new BorderLayout()); 
     sudokuArea.add(sudokuGrid, BorderLayout.CENTER); 

     sudokuGrid.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); 
     sudokuGrid.setEditable(false); 
     sudokuGrid.append(field.toString()); 

     load.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       loader(); 
       } 
      public void loader(){ 
       JFileChooser sumtin = new JFileChooser(); 
       if(sumtin.showOpenDialog() == JFileChooser.APPROVE_OPTION) 
       { 
         File filer = sumtin.getSelectedFile(); 
         field.fromFile(filer.getName()); 
         sudokuGrid.setText(field.toString()); 
         mainWindow.revalidate(); 
         mainWindow.repaint(); 
       } 
      } 
      }); 
     setVisible(true); 
    } 

該字段方法來自另一個名爲Field的類,但它並不真正相關(我認爲)。

這裏的控制檯說什麼:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
     at Sudoku$1.loader(Sudoku.java:52) 
     at Sudoku$1.actionPerformed(Sudoku.java:45) 
     at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
     at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
     at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
     at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour 
ce) 
     at java.awt.Component.processMouseEvent(Unknown Source) 
     at javax.swing.JComponent.processMouseEvent(Unknown Source) 
     at java.awt.Component.processEvent(Unknown Source) 
     at java.awt.Container.processEvent(Unknown Source) 
     at java.awt.Component.dispatchEventImpl(Unknown Source) 
     at java.awt.Container.dispatchEventImpl(Unknown Source) 
     at java.awt.Component.dispatchEvent(Unknown Source) 
     at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
     at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
     at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
     at java.awt.Container.dispatchEventImpl(Unknown Source) 
     at java.awt.Window.dispatchEventImpl(Unknown Source) 
     at java.awt.Component.dispatchEvent(Unknown Source) 
     at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
     at java.awt.EventQueue.access$200(Unknown Source) 
     at java.awt.EventQueue$3.run(Unknown Source) 
     at java.awt.EventQueue$3.run(Unknown Source) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour 
ce) 
     at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour 
ce) 
     at java.awt.EventQueue$4.run(Unknown Source) 
     at java.awt.EventQueue$4.run(Unknown Source) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour 
ce) 
     at java.awt.EventQueue.dispatchEvent(Unknown Source) 
     at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
     at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
     at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
     at java.awt.EventDispatchThread.run(Unknown Source) 

我真的不知道做的是什麼,我真的不知道這意味着什麼。任何指針?

編輯:試圖大衛Colers代碼之後新錯誤代碼:

Sudoku.java:49: error: method showOpenDialog in class JFileChooser cannot be app 
lied to given types; 
           if(sumtin.showOpenDialog() == JFileChooser.APPRO 
VE_OPTION) 
             ^
    required: Component 
    found: no arguments 
    reason: actual and formal argument lists differ in length 
1 error 
+0

'loader'中的哪行是第52行? – gla3dr 2015-01-09 23:24:18

+0

mainWindow.revalidate(); – 2015-01-09 23:25:05

+0

mainWindow是一個JPanel,我將所有其他組件放入其中。 – 2015-01-09 23:25:43

回答

1

你沒有正確處理JFileChooser的一兩件事。 編輯:將此關鍵字更改爲空。

JFileChooser sumtin = new JFileChooser(); 
if(sumtin.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) 
{ 
     File filer = sumtin.getSelectedFile(); 
     field.fromFile(filer.getName()); 
     sudokuGrid.setText(field.toString()); 
     mainWindow.revalidate(); 
     mainWindow.repaint(); 
} 
+0

所以人們不要問,是的,你可以在if語句裏面放置showOpenDialog,這樣你就不需要創建另一個變量來處理返回。 – 2015-01-09 23:30:33

+0

這給了我另一個錯誤。如果(sumtin.showOpenDialog()== JFileChooser.APPRO VE_OPTION) ^ 必需:組件 找到:沒有參數 – 2015-01-09 23:32:11

+0

看看我的代碼,看看你剛剛評論的內容..把「這個」運算符放在它和也取出批處理選項中的空格 – 2015-01-09 23:34:17

0

你錯過了幾步:

首先創建一個文件選擇

JFileChooser fileChooser = new JFileChooser(); 

表現出來,並得到結果

int result = fileChooser.showOpenDialog(this); 

而且,如果用戶打開一個文件,你可以得到它,做你想做的事

if (result == JFileChooser.APPROVE_OPTION) { 
    File file = fileChooser.getSelectedFile(); 
    ... 
}