java - actionPerformed 按钮上的异常

标签 java swing actionlistener

这是一个正在 build 中的平开窗。问题出在一个 Action 中。 当我对“打开”按钮执行操作时,我调用一个方法 OpenFile()

此方法似乎一直有效,直到最后抛出 UnsupportedOperationException 异常。

import static alphareader.AlphaReaderBackup.maxGap;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;


public class AlphaReader extends JFrame 
implements ActionListener {

    final JFileChooser fc = new JFileChooser();

    private final JButton open=new JButton("Open File.");
    private final JButton exitButton = new JButton(" Exit ");

    JLabel welcomeMsg=new JLabel("Welcome to alpha reader. ");


    //BoxLayout boxy = new BoxLayout();
    GridLayout gridy = new GridLayout(3,2);
    FlowLayout flow = new FlowLayout();
    GridLayout rowsGrid=new GridLayout();


    Dimension btn=new Dimension(132,32);

    public static void main(String[] args){
        AlphaReader alpha=new AlphaReader();
        alpha.setSize(800,800);
        alpha.createGui();
        alpha.setVisible(true);
    }

    private void createGui(){
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container window=getContentPane();
    window.setLayout(gridy);
    JPanel row1=new JPanel();
    JPanel row2=new JPanel();
    JPanel row3=new JPanel();


    window.add(row1);
    row1.setLayout(new FlowLayout(flow.LEFT,10,10));
    row1.add(welcomeMsg); 
    welcomeMsg.setPreferredSize(new Dimension(400,32));
    welcomeMsg.setAlignmentX(LEFT_ALIGNMENT);
    row1.add(open);
    open.setPreferredSize(btn);
    open.addActionListener(this);
    exitButton.setPreferredSize(btn);
    row1.add(exitButton);
    exitButton.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent ev) {
    Object source = new Object();
    source=ev.getSource();
    if(source==open){
        openFile();
    }
        else if(source==exitButton){
                System.exit(0);
                }
    throw new UnsupportedOperationException("Not supported yet.");
    }

    private void openFile(){
        int returnVal = fc.showOpenDialog(AlphaReader.this);
        //fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();} //I get the exception here.
    }

}

最佳答案

改变

Object source = new Object();
source=ev.getSource();
if(source==open){
    openFile();
}
else if(source==exitButton){
    System.exit(0);
}
throw new UnsupportedOperationException("Not supported yet.");

JButton source = (JButton) ev.getSource();
if(source == open){
    openFile();
}
else if(source == exitButton){
    System.exit(0);
}
else
    throw new UnsupportedOperationException("Not supported yet.");

如果没有 else 语句,将始终抛出 UnsupportedOperationException

关于java - actionPerformed 按钮上的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34576505/

相关文章:

java - 如何正确清除、添加或编辑 JTable 中的数据?以及如何控制TableModelListener?

java - 对象流到 Canvas

java - 删除剩余参数

java - 这个 GUI 线程安全吗(使用 Swing)?

java - JToolBar 防止最小化

java-me - PopupChoiceGroup Action 监听器不适用于 Asha 501

java - 从嵌套的 Java ActionListener 中调用它

java - 反射:如何比较未知类型的两个对象

Java反射

java - intellij - 如何将 JProgressbar 添加到 swing 调色板?