落后_追 2017-06-11 13:13 采纳率: 100%
浏览 3410
已采纳

有main方法却还是提示没有

package 对话框;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JFileChooser;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

public class JFileChooserDemo extends JFrame{
private JPanel p;
private JScrollPane sp;
private JButton btnOpen,btnSave,btnClear;
private JTextArea txtContent;

public JFileChooserDemo(){
    super("JFileChooser文件对话框");
    p=new JPanel();
    btnOpen=new JButton("打开");
    btnSave=new JButton("保存");
    btnClear=new JButton("清除");
    txtContent=new JTextArea(20,10);
    sp=new JScrollPane(txtContent);
    btnOpen.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            openFile();
        }
    });
    btnSave.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            saveFile();
        }
    });
    btnClear.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            txtContent.setText("");
        }
    });
    p.add(btnOpen);
    p.add(btnSave);
    p.add(btnClear);
    this.add(sp);
    this.add(p,BorderLayout.SOUTH);
    this.setSize(400,300);
    this.setLocation(400,300);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
}
private void openFile(){
    JFileChooser fc=new JFileChooser();
    int rVal=fc.showOpenDialog(this);
    if(rVal==JFileChooser.APPROVE_OPTION){
    String fileName=fc.getSelectedFile().getName();
    String path=fc.getCurrentDirectory().toString();
    try{
        FileReader fread=new FileReader(path+"/"+fileName);
        BufferedReader bread=new BufferedReader(fread);
        String line=bread.readLine();
        while(line!=null){
            txtContent.append(line+"\n");
            line=bread.readLine();
        }
        bread.close();
        fread.close();

    }catch(Exception e){
        e.printStackTrace();
    }
    }
}
private void saveFile(){
    JFileChooser fc=new JFileChooser();
    int rVal=fc.showSaveDialog(this);
    if(rVal==JFileChooser.APPROVE_OPTION){
        String fileName=fc.getSelectedFile().getName();
        String path=fc.getCurrentDirectory().toString();
        try{
            FileWriter fwriter=new FileWriter(path+"/"+fileName);
            fwriter.write(txtContent.getText());
            fwriter.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
public static void main(String[] args){
    new JFileChooserDemo(); 
}

}

错误: 在类 对话框.JFileChooserDemo 中找不到 main 方法, 请将 main 方法定义为:

public static void main(String[] args)

否则 JavaFX 应用程序类必须扩展javafx.application.Application

java 新手求大神

  • 写回答

4条回答 默认 最新

  • love1code 2017-06-11 13:14
    关注

    你的代码使用的全部是Swing,但是根据你的错误来看,你确实把它当做JavaFx应用程序来编译运行的。如果是想写JavaFx,则类必须扩展javafx.application.Application

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格