毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> java技术 >> 正文

Eclipse运行一个模仿win7系统自带记事本的代码显示不出GUI界面

更新时间:2012-5-30:  来源:毕业论文

import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.*;

public class NotepadFrame extends JFrame {
    private JMenuBar menuBar;
    private JTextArea textArea;
    private JScrollPane scrollPane;
    private Font font = getFont();

    NotepadFrame() {
        initFrame();
    }

    private void initText() {
        textArea = new JTextArea();
        textArea.setFont(new Font("宋体", Font.PLAIN, 25));
        scrollPane = new JScrollPane(textArea);
        scrollPane
                .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        getContentPane().add(scrollPane);

    }

    private void initBar() {
        menuBar = new MyMenuBar();
        this.setJMenuBar(menuBar);
    }

    private void initFrame() {
        setSize(830, 600);
        setTitle("记事本");
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        initBar();
        initText();
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        new NotepadFrame();
    }

    private class MyMenuBar extends JMenuBar {
        private JMenu fileMenu;
        private JMenu editMenu;
        private JMenu formatMenu;
        private JMenu viewMenu;
        private JMenu helpMenu;
        private JMenuItem newFile = new JMenuItem("新建(N)           Ctrl+N",
                KeyEvent.VK_N);
        private JMenuItem open = new JMenuItem("打开(O)...         Ctrl+O",
                KeyEvent.VK_O);
        private JMenuItem save = new JMenuItem("保存(S)            Ctrl+S",
                KeyEvent.VK_S);
        private JMenuItem aSave = new JMenuItem("另存为(A)...", KeyEvent.VK_A);
        private JMenuItem uSetup = new JMenuItem("页面设置(U)...", KeyEvent.VK_U);
        private JMenuItem print = new JMenuItem("打印(P)...          Ctrl+P",
                KeyEvent.VK_P);
        private JMenuItem exit = new JMenuItem("退出(X)", KeyEvent.VK_X);
        private JMenuItem backout = new JMenuItem(
                "撤销(U)                Ctrl+Z", KeyEvent.VK_U);
        private JMenuItem shear = new JMenuItem("剪切(T)                Ctrl+X",
                KeyEvent.VK_T);
        private JMenuItem copy = new JMenuItem("复制(C)                Ctrl+C",
                KeyEvent.VK_C);
        private JMenuItem paste = new JMenuItem("粘贴(P)                Ctrl+V",
                KeyEvent.VK_P);
        private JMenuItem delete = new JMenuItem(
                "删除(L)                     Del", KeyEvent.VK_D);
        private JMenuItem find = new JMenuItem("查找(F)                Ctrl+F",
                KeyEvent.VK_F);
        private JMenuItem nextFind = new JMenuItem("查找下一个(N)            F3",
                KeyEvent.VK_N);
        private JMenuItem replace = new JMenuItem(
                "替换(R)...             Ctrl+H", KeyEvent.VK_R);
        private JMenuItem goTo = new JMenuItem("转到(G)...             Ctrl+G",
                KeyEvent.VK_G);
        private JMenuItem allChoose = new JMenuItem(
                "全选(A)               Ctrl+A", KeyEvent.VK_A);
        private JMenuItem date = new JMenuItem("日期/时间(D)             F5",
                KeyEvent.VK_D);
        private JMenuItem wrap = new JCheckBoxMenuItem("自动换行(W)   ");
        private JMenuItem font = new JMenuItem("字体(F)...   ", KeyEvent.VK_F);
        private JMenuItem status = new JMenuItem("状态栏(S)     ", KeyEvent.VK_S);
        private JMenuItem help = new JMenuItem("查看帮助(H)     ", KeyEvent.VK_H);
        private JMenuItem about = new JMenuItem("关于记事本(A)   ", KeyEvent.VK_A);
        private ActionListener fileListener = new FileMenuListener();
        private ActionListener formatListener = new FormatListener();
        boolean ifWrap = false;
       
       
        MyMenuBar() {
            initMenu();
            addActionListener();
            this.add(fileMenu);
            this.add(editMenu);
            this.add(formatMenu);
            this.add(viewMenu);
            this.add(helpMenu);
        }

        private void addActionListener() {
            newFile.addActionListener(fileListener);
            open.addActionListener(fileListener);
            save.addActionListener(fileListener);
            wrap.addActionListener(formatListener);
            font.addActionListener(formatListener);
        }

        private void initMenu() {
            Insets margin = new Insets(1, 3, 1, 3);
            fileMenu = new JMenu("文件(F)");
            editMenu = new JMenu("编辑(E)");
            formatMenu = new JMenu("格式(O)");
            viewMenu = new JMenu("查看(V)");
            helpMenu = new JMenu("帮助(H)");
            fileMenu.setMargin(margin);
            editMenu.setMargin(margin);
            formatMenu.setMargin(margin);
            viewMenu.setMargin(margin);
            helpMenu.setMargin(margin);
            fileMenu.add(newFile);
            fileMenu.add(open);
            fileMenu.add(save);
            fileMenu.add(aSave);
            fileMenu.addSeparator();
            fileMenu.add(uSetup);
            fileMenu.add(print);
            fileMenu.addSeparator();
            fileMenu.add(exit);
            editMenu.add(backout);
            editMenu.addSeparator();
            editMenu.add(shear);
            editMenu.add(copy);
            editMenu.add(paste);
            editMenu.add(delete);
            editMenu.addSeparator();
            editMenu.add(find);
            editMenu.add(nextFind);
            editMenu.add(replace);
            editMenu.add(goTo);
            editMenu.addSeparator();
            editMenu.add(allChoose);
            editMenu.add(date);
            formatMenu.add(wrap);
            formatMenu.add(font);
            viewMenu.add(status);
            helpMenu.add(help);
            helpMenu.addSeparator();
            helpMenu.add(about);
        }

        private class FileMenuListener implements ActionListener {

            public void actionPerformed(ActionEvent ae) {
                if (ae.getSource() == open) {
                    open(textArea);
                } else if (ae.getSource() == save) {
                    save(textArea);
                }
            }

            /* 打开 */
            public void open(JTextArea area) {
                JFileChooser fileChooser = new JFileChooser(
                        new File("").getAbsolutePath());

                if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {

                    String path = fileChooser.getSelectedFile()
                            .getAbsolutePath();
                    BufferedReader bufferedR = null;
                    String line;

                    try {
                        bufferedR = new BufferedReader(new FileReader(path));
                        line = bufferedR.readLine();
                        while (line != null) {
                            area.append(line + "\n");
                            line = bufferedR.readLine();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            bufferedR.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }

            /* 保存 */
            public void save(JTextArea area) {
                JFileChooser fileChooser = new JFileChooser(
                        new File("").getAbsolutePath());

                if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {

                    String path = fileChooser.getSelectedFile()
                            .getAbsolutePath();
                    PrintWriter printW = null;
                    String[] content;
                    content = area.getText().split("\n");
                    try {
                        printW = new PrintWriter(new FileWriter(path));
                        for (int i = 0; i < content.length; i++) {
                            printW.println(content[i]);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            printW.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

        private class FormatListener implements ActionListener {
            public void actionPerformed(ActionEvent ae) {

[1] [2] 下一页

设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。