完成一个简易文本编辑器,要求界面如图,可以将文本区域(放在滚动面板中)中的信息保存在文件中,或将文件中信息读入到文本区域。

该文章展示了一个JavaSwing应用程序,其中实现了JFileChooser组件来处理文件的打开和保存操作。用户界面包含一个文件菜单,菜单中有打开、保存和退出选项。当用户点击‘打开’或‘保存’时,JFileChooser对话框会弹出,允许用户选择文件。
摘要由CSDN通过智能技术生成

使用文件选择器的使用

JFileChooserjf =new JFileChooser();

文件选择器将目录窗格和一些典型按钮组成一个便捷界面。

int i=jf.showOpenDialog(父组件名或null ); //显示打开对话框

int i=jf.showSaveDialog(父组件名或null );  //显示保存对话框)

 

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Myframe extends JFrame implements ActionListener {
    private JMenuBar bar;
    private JMenu menu;
    private JMenuItem item1;
    private JMenuItem item2;
    private  JMenuItem item3;
    private JFileChooser jf;

    public Myframe(){
        this.setSize(500,500);

        this.setTitle("登录界面");
        this.setLayout(new BorderLayout());

        //this.setAlwaysOnTop(true);

        this.setLocationRelativeTo(null);

        this.setDefaultCloseOperation(3);

        this.setResizable(false);

        this.init();

        this.setVisible(true);
    }
    public void init(){
        bar =new JMenuBar();

        menu=new JMenu("文件");

        item1=new JMenuItem("打开");
        item1.addActionListener(this);
        item2=new JMenuItem("保存");
        item2.addActionListener(this);
        item3=new JMenuItem("退出");
        item3.addActionListener(this);

        menu.add(item1);
        menu.add(item2);
        menu.add(item3);

        bar.add(menu);
        this.add(bar,BorderLayout.NORTH);

        jf=new JFileChooser();


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==item1){
            int i=jf.showOpenDialog(null);
        }
        if(e.getSource()==item2){
            int i=jf.showOpenDialog(null);
        }
        if(e.getSource()==item3){
            System.exit(0);
        }

    }
}
public class demo {
    public static void main(String[] args) {
        Myframe myframe=new Myframe();
    }
}

 

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值