java提示没有main,有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+"

");

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 新手求大神

问题解答:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值