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

有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条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?