风-fmgao

导航

java的gui编程示例

对话框

package com.sly.uploadfile.base;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Serializable;

/**
 * Created by admin on 2020/6/27.
 */
public class MainFrame extends JFrame implements ActionListener, Serializable {

    public static void main(String[] args) {
        MainFrame mainFrame = new MainFrame();
    }

    private static final long serialVersionUID = 6126254268960773408L;
    private TextArea textArea;
    private JButton btnOK;
    private JButton btnCancel;
    private JMenuItem miOpen;
    private JMenuItem miExit;


    public MainFrame() {
        initFrame();
        this.setVisible(true);
    }

    /**
     * 初始化布局
     */
    private void initFrame() {
        // 标题
        this.setTitle("主窗口");
        // 边界
        this.setBounds(100, 100, 850, 700);
        // 绝对布局
        this.setLayout(null);
        Font font = new Font("微软雅黑", Font.PLAIN, 15);

        // 将文本域添加到滚动面板中
        textArea = new TextArea();
        textArea.setFont(font);

        // 滚动面板
        JScrollPane pane = new JScrollPane(textArea);
        pane.setBounds(0, 0, 800, 500);
        this.add(pane);

        btnOK = new JButton("保存");
        btnOK.setBounds(600, 510, 100, 50);
        btnOK.setFont(font);
        btnOK.addActionListener(this);
        this.add(btnOK);

        btnCancel = new JButton("取消");
        btnCancel.setBounds(710, 510, 100, 50);
        btnCancel.setFont(font);
        btnCancel.addActionListener(this);
        this.add(btnCancel);

        // 添加窗口事件处理程序,使用适配器
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(-1);
            }
        });

        // 添加菜单栏
        JMenuBar menuBar = new JMenuBar();
        // 添加菜单
        JMenu menu = new JMenu("文件");

        miOpen = new JMenuItem("打开");
        miOpen.addActionListener(this);
        // 添加菜单项
        menu.add(miOpen);

        //分隔符
        menu.addSeparator();

        miExit = new JMenuItem("推出");
        miExit.addActionListener(this);
        menu.add(miExit);

        menuBar.add(menu);
        this.setJMenuBar(menuBar);

    }

    public void actionPerformed(ActionEvent e) {
        Object es = e.getSource();
        // 保存文件
        if (es == btnOK) {
            try {
                // 打开保存对话框,定位保存文件的位置
                FileDialog d = new FileDialog(this, "保存", FileDialog.SAVE);
                d.setVisible(true);

                File f = new File(d.getDirectory(), d.getFile());

                String str = textArea.getText();
                FileWriter writer = new FileWriter(f);
                writer.write(str);
                writer.close();
                textArea.setText(null);

            } catch (Exception e1) {
                e1.printStackTrace();
            }

        } else if (es == btnCancel) {
            // 取消
            this.dispose();

        } else if (es == miOpen) {
            // 是否是菜单项
            FileDialog d = new FileDialog(this, "打开", FileDialog.LOAD);
            d.setVisible(true);
            String dir = d.getDirectory();
            String f = d.getFile();
            if (dir != null && f != null) {
                try {
                    textArea.setText(null);
                    FileReader reader = new FileReader(new File(dir, f));
                    char[] buffer = new char[1024];
                    int len = -1;
                    while ((len = reader.read(buffer)) != -1) {
                        textArea.setText(textArea.getText() + new String(buffer, 0, len));

                    }
                    reader.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        } else if (es == miExit) {
            // 退出
            System.exit(-1);
        }
    }
}

 

posted on 2020-06-27 11:35  风-fmgao  阅读(558)  评论(0编辑  收藏  举报