gpt4 book ai didi

java - JFileChooser 崩溃 - Java 7

转载 作者:太空宇宙 更新时间:2023-11-04 06:17:43 29 4
gpt4 key购买 nike

我试图让我的程序使用 JFileChooser 加载 txt 文件,但它似乎不起作用。当我按下 JButton 时,控制台会出现很多错误。这是到目前为止的完整代码:

    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 方法来自另一个名为 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)

我不太确定该怎么做,因为我真的不知道这意味着什么。有什么指点吗?

编辑:尝试 David 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

最佳答案

一方面,您没有正确处理 JFileChooser。编辑:将此关键字更改为 null。

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();
}

关于java - JFileChooser 崩溃 - Java 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27870852/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com