小编MaC*_*aCo的帖子

JFileChooser - 关于"打开"和"取消"按钮.Java的

我在使用JFileChooser时遇到了一些麻烦.每当我运行程序时,如果我立即单击"取消"按钮而不选择文件,它将显示"你好",如果我点击打开,它将不会做任何事情.另一方面,如果我选择一个文件并单击打开它将开始显示"Hello"(调用createFile方法),如果单击"取消",将显示"hello".

我的问题是如何找出单击了哪个按钮并为每个按钮执行特定操作,如单击取消时调用die函数并在单击打开时调用createFile函数.

我在想类似的东西

if(e.getSource() == "Something_I_Dont_know") { do this}
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Grading{

public static void main(String[] arg){

 new MFrame();

}


}// end of class Grading

class MFrame extends JFrame{

private JCheckBox cum,uc,ucs;
private JButton calc, clear, exit;
private ButtonGroup bg;
private JTextArea display;
private JFileChooser input;

public MFrame(){

    setVisible( true );
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(550,550);

    input = new JFileChooser();
    add( input );
    input.addActionListener(
        new ActionListener(){
            public void actionPerformed( ActionEvent e ){
                //die();
                createFile(); …
Run Code Online (Sandbox Code Playgroud)

java swing jfilechooser jframe disambiguation

2
推荐指数
1
解决办法
3万
查看次数

标签 统计

disambiguation ×1

java ×1

jfilechooser ×1

jframe ×1

swing ×1