【问题标题】:Action listener/Throws IOException Conflict动作监听器/引发 IOException 冲突
【发布时间】:2013-04-30 16:37:31
【问题描述】:

我正在尝试为一个程序制作一个 GUI,该程序可以从随机访问文件中读取和写入信息并随意显示它。除了 GUI 之外,我不能操作任何东西,并且必须引用预构建的方法(如果这些方法真的不可用,那么我可以例外)。

我遇到的问题是在写入文件时处理 IOException。

public static class  EdtButtonHandler implements ActionListener 
    {


        public void actionPerformed(ActionEvent e) 
        {

            if (e.getActionCommand().equals("Confirm")) //if the confirm button is pressed
            {

                    writeAll(); //runs all write methods using the strings from the text field

                frameAddMovie.setVisible(false);
            }

writeAll 是指一系列基于传递的字符串从文件写入的方法,这些字符串来自我的 GUI 窗口上的文本字段。它们看起来都像这样;

public static void writeDirector(int recordNum, String directorIn)throws IOException
    {
        int position;
        RandomAccessFile recordFile = new RandomAccessFile("RecordFile", "rw");
        position = recSize * (recordNum-1)+32; // read 32 spaces into the record to the start of the director line
        recordFile.seek(position);
        recordFile.writeBytes(directorIn);

    }

在按下确认按钮并运行 writeAll() 时,抛出了一个 IOException,该方法或类无法捕获该异常。

【问题讨论】:

  • 你能给出像IOException File Not found这样的完整例外吗?

标签: java actionlistener ioexception


【解决方案1】:
if (e.getActionCommand().equals("Confirm")) //if the confirm button is pressed

你不想这样吗

if (e.getSource() = buttonname)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-02
    • 2013-12-24
    • 2022-08-15
    • 2011-09-26
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多