(JAVA)文本模拟器

package Main;

public class Main {
    public static void main(String[] args) {
        try {
            mainWindow frame = new mainWindow();
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}









package Main;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.DefaultComboBoxModel;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.BufferedWriter;

public class mainWindow extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JPanel mainPane;
    public String selectedStyle = "微软雅黑";
    public int selectedSize = 12;
    public int selectedType = Font.PLAIN;
    public String code = "GBK";
    public Font thisFont = new Font(selectedStyle, selectedType, selectedSize);

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    mainWindow frame = new mainWindow();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public mainWindow() {
        super("文本模拟器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        setLocationRelativeTo(getOwner());
        mainPane = new JPanel();
        mainPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        mainPane.setLayout(new BorderLayout(0, 0));
        setContentPane(mainPane);

        JScrollPane textAreaPanel = new JScrollPane();
        mainPane.add(textAreaPanel, BorderLayout.CENTER);

        JTextArea textArea = new JTextArea();
        textArea.setFont(new Font(selectedStyle, selectedType, selectedSize));
        textAreaPanel.setViewportView(textArea);

        JComboBox<Object> codingTable = new JComboBox<Object>();
        codingTable.setModel(
                new DefaultComboBoxModel<Object>(new String[] { "ASCII", "ISO-8859-1", "UTF-8", "GBK", "Unicode" }));
        codingTable.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                try {
                    String[] str = textArea.getText().split("\n");
                    textArea.setText("");
                    for (int i = 0; i < str.length; i++) {
                        String temp = new String(str[i].getBytes(code),
                                codingTable.getItemAt(codingTable.getSelectedIndex()).toString());
                        textArea.append(temp + "\n");
                    }
                } catch (UnsupportedEncodingException e1) {
                    // TODO 自动生成的 catch 块
                    e1.printStackTrace();
                }
                code = codingTable.getItemAt(codingTable.getSelectedIndex()).toString();
            }

        });
        codingTable.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        mainPane.add(codingTable, BorderLayout.SOUTH);

        JMenuBar menuBar = new JMenuBar();
        mainPane.add(menuBar, BorderLayout.NORTH);

        JMenu Edit = new JMenu("Edit");
        Edit.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        menuBar.add(Edit);

        JMenuItem editFont = new JMenuItem("Font(F)");
        editFont.setMnemonic('F');
        editFont.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                setFont frame = new setFont(thisFont);
                frame.addWindowListener(new WindowListener() {

                    @Override
                    public void windowActivated(WindowEvent arg0) {
                    }

                    public void windowClosed(WindowEvent arg0) {
                        thisFont = frame.getSettedFont();
                        textArea.setFont(thisFont);
                    }

                    public void windowClosing(WindowEvent arg0) {

                    }

                    public void windowDeactivated(WindowEvent arg0) {
                    }

                    public void windowDeiconified(WindowEvent arg0) {
                    }

                    public void windowIconified(WindowEvent arg0) {
                    }

                    public void windowOpened(WindowEvent arg0) {
                    }
                });
            }
        });
        editFont.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        Edit.add(editFont);

        JMenu File = new JMenu("File");
        File.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        menuBar.add(File);

        JMenuItem fileOpen = new JMenuItem("Open(O)");
        fileOpen.setMnemonic('O');
        fileOpen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textArea.setText("");
                JFileChooser jfc = new JFileChooser();
                jfc.showOpenDialog(fileOpen);
                if (jfc.getSelectedFile() != null) {
                    File file = jfc.getSelectedFile();
                    try {
                        BufferedReader br = new BufferedReader(new FileReader(file));
                        String str = br.readLine();
                        while (str != null) {
                            textArea.append(str + "\n");
                            str = br.readLine();
                        }
                        br.close();
                    } catch (IOException e) {
                        // TODO 自动生成的 catch 块
                        e.printStackTrace();
                    }
                }
            }
        });
        fileOpen.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        File.add(fileOpen);

        JMenuItem fileSave = new JMenuItem("Save(S)");
        fileSave.setMnemonic('S');
        fileSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser jfc = new JFileChooser();
                jfc.showSaveDialog(fileSave);
                if (jfc.getSelectedFile() != null) {
                    File file = jfc.getSelectedFile();
                    try {
                        OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file));
                        BufferedWriter bw = new BufferedWriter(write);
                        String[] text = textArea.getText().split("\n");
                        for (int i = 0; i < text.length; i++) {
                            bw.write(text[i] + "\r\n");// \r表示回到光标所在行的最左边(return),\n表示向下一行移动,但并不左右移动
                            bw.flush();
                        }
                        bw.close();
                    } catch (IOException e) {
                        // TODO 自动生成的 catch 块
                        e.printStackTrace();
                    }
                }
            }
        });
        fileSave.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        File.add(fileSave);
    }

    public mainWindow getThisJFrame() {
        return this;
    }

    public Font getThisJFrameFont() {
        return new Font(this.selectedStyle, this.selectedSize, this.selectedType);
    }
}







package Main;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;

public class setFont extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JPanel mainPane;
    public static String selectedStyle = null;
    public static int selectedSize = 6;
    public static int selectedType = Font.PLAIN;
    public static Font initialFont = new Font("微软雅黑", Font.PLAIN, 12);
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;

    /**
     * Launch the application.
     */
    /*
     * public static void main(String[] args) { EventQueue.invokeLater(new
     * Runnable() { public void run() { try { setFont frame = new setFont();
     * frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }
     * }); }
     */

    /**
     * Create the frame.
     */
    public setFont(Font font) {
        super("设置字体");
        selectedSize = font.getSize();
        selectedType = font.getStyle();
        selectedStyle = font.getFontName();
        initialFont = font;
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 316, 373);
        mainPane = new JPanel();
        mainPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(mainPane);
        mainPane.setLayout(null);
        setVisible(true);
        setLocationRelativeTo(getOwner());

        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 310, 345);
        mainPane.add(panel);
        panel.setLayout(null);

        JLabel test = new JLabel("这是样例 aAbBcC");
        test.setFont(initialFont);
        test.setBounds(10, 259, 290, 76);
        test.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(test);

        JScrollPane fontStyle = new JScrollPane();
        fontStyle.setBounds(10, 33, 90, 150);
        panel.add(fontStyle);

        JList<Object> styleList = new JList<Object>();
        styleList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent arg0) {
                selectedStyle = styleList.getSelectedValue().toString();
                textField.setText(selectedStyle);
                test.setFont(new Font(selectedStyle, selectedType, selectedSize));
            }
        });
        styleList.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        styleList.setModel(new AbstractListModel<Object>() {
            /**
             * 
             */
            private static final long serialVersionUID = 1L;
            String[] values = new String[] { "微软雅黑", "宋体", "楷体", "仿宋", "方正姚体", "隶书", "黑体", "幼圆", "新宋体" };

            public int getSize() {
                return values.length;
            }

            public Object getElementAt(int index) {
                return values[index];
            }
        });
        fontStyle.setViewportView(styleList);

        JScrollPane fontSize = new JScrollPane();
        fontSize.setBounds(110, 33, 90, 150);
        panel.add(fontSize);

        JList<Object> sizeList = new JList<Object>();
        sizeList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                selectedSize = Integer.valueOf(sizeList.getSelectedValue().toString());
                textField_1.setText(String.valueOf(selectedSize));
                test.setFont(new Font(selectedStyle, selectedType, selectedSize));
            }
        });
        sizeList.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        sizeList.setModel(new AbstractListModel<Object>() {
            /**
             * 
             */
            private static final long serialVersionUID = 1L;
            String[] values = new String[] { "6", "12", "18", "24", "30", "36", "42", "48", "54", "60", "66", "72",
                    "84", "90", "96" };

            public int getSize() {
                return values.length;
            }

            public Object getElementAt(int index) {
                return values[index];
            }
        });
        fontSize.setViewportView(sizeList);

        JScrollPane fontType = new JScrollPane();
        fontType.setBounds(210, 33, 90, 150);
        panel.add(fontType);

        JList<Object> typeList = new JList<Object>();
        typeList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (typeList.getSelectedIndex() == 0) {
                    selectedType = Font.PLAIN;
                    textField_2.setText("PLAIN");
                } else if (typeList.getSelectedIndex() == 1) {
                    selectedType = Font.BOLD;
                    textField_2.setText("BOLD");
                } else if (typeList.getSelectedIndex() == 2) {
                    selectedType = Font.ITALIC;
                    textField_2.setText("Italic");
                }
                test.setFont(new Font(selectedStyle, selectedType, selectedSize));
            }
        });
        typeList.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        typeList.setModel(new AbstractListModel<Object>() {
            /**
             * 
             */
            private static final long serialVersionUID = 1L;
            String[] values = new String[] { "PLAIN", "BOLD", "Italic" };

            public int getSize() {
                return values.length;
            }

            public Object getElementAt(int index) {
                return values[index];
            }
        });
        fontType.setViewportView(typeList);

        textField = new JTextField();
        textField.setEditable(false);
        textField.setBounds(10, 193, 90, 21);
        textField.setText(selectedStyle);
        panel.add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setEditable(false);
        textField_1.setColumns(10);
        textField_1.setBounds(110, 193, 90, 21);
        panel.add(textField_1);
        textField_1.setText(String.valueOf(selectedSize));

        textField_2 = new JTextField();
        textField_2.setEditable(false);
        textField_2.setColumns(10);
        textField_2.setBounds(210, 193, 90, 21);
        panel.add(textField_2);
        if (selectedType == 0)
            textField_2.setText("PLAIN");
        else if (selectedType == 1)
            textField_2.setText("BOLD");
        else if (selectedType == 2)
            textField_2.setText("Italic");

        JLabel styleLabel = new JLabel("字体样式");
        styleLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        styleLabel.setBounds(30, 10, 54, 15);
        panel.add(styleLabel);

        JLabel sizeLabel = new JLabel("字体大小");
        sizeLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        sizeLabel.setBounds(128, 10, 54, 15);
        panel.add(sizeLabel);

        JLabel typeLabel = new JLabel("字体风格");
        typeLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        typeLabel.setBounds(228, 10, 54, 15);
        panel.add(typeLabel);

        JButton sure = new JButton("确定");
        sure.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        sure.setBounds(10, 226, 90, 23);
        panel.add(sure);
        sure.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (styleList.getSelectedIndex() != -1)
                    selectedStyle = styleList.getSelectedValue().toString();

                if (sizeList.getSelectedIndex() != -1)
                    selectedSize = Integer.valueOf(sizeList.getSelectedValue().toString());

                if (typeList.getSelectedIndex() == 0)
                    selectedType = Font.PLAIN;
                else if (typeList.getSelectedIndex() == 1)
                    selectedType = Font.BOLD;
                else if (typeList.getSelectedIndex() == 2)
                    selectedType = Font.ITALIC;
                initialFont = new Font(selectedStyle, selectedType, selectedSize);
                getThisJFrame().dispose();
            }

        });

        JButton reset = new JButton("重置");
        reset.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                initialFont = new Font("微软雅黑", Font.PLAIN, 12);
                getThisJFrame().dispose();
            }

        });
        reset.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        reset.setBounds(110, 226, 90, 23);
        panel.add(reset);

        JButton cancel = new JButton("取消");
        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                getThisJFrame().dispose();
            }
        });
        cancel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        cancel.setBounds(210, 226, 90, 23);
        panel.add(cancel);

    }

    public setFont getThisJFrame() {
        return this;
    }

    public Font getSettedFont() {
        return initialFont;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值