[Java] java代码生成记事本窗体 →→→→→进入此内容的聊天室

来自 , 2020-02-21, 写在 Java, 查看 120 次.
URL http://www.code666.cn/view/8f19793b
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.FileDialog;
  6. import java.awt.FlowLayout;
  7. import java.awt.Font;
  8. import java.awt.GraphicsEnvironment;
  9. import java.awt.GridLayout;
  10. import java.awt.List;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.InputEvent;
  14. import java.awt.event.MouseAdapter;
  15. import java.awt.event.MouseEvent;
  16. import java.awt.event.WindowAdapter;
  17. import java.awt.event.WindowEvent;
  18. import java.io.BufferedReader;
  19. import java.io.BufferedWriter;
  20. import java.io.FileReader;
  21. import java.io.FileWriter;
  22. import java.io.PrintWriter;
  23.  
  24. import javax.swing.JButton;
  25. import javax.swing.JColorChooser;
  26. import javax.swing.JDialog;
  27. import javax.swing.JFrame;
  28. import javax.swing.JLabel;
  29. import javax.swing.JMenu;
  30. import javax.swing.JMenuBar;
  31. import javax.swing.JMenuItem;
  32. import javax.swing.JOptionPane;
  33. import javax.swing.JPanel;
  34. import javax.swing.JPopupMenu;
  35. import javax.swing.JScrollPane;
  36. import javax.swing.JTextArea;
  37. import javax.swing.KeyStroke;
  38. import javax.swing.ScrollPaneConstants;
  39. import javax.swing.event.DocumentEvent;
  40. import javax.swing.event.DocumentListener;
  41.  
  42.  
  43. public class notepad extends JFrame {
  44.  
  45.     private static final long serialVersionUID = 1L;
  46.     private JTextArea ja = new JTextArea(5, 6);
  47.     String str;
  48.     private String filePath;
  49.     private String fileName;
  50.  
  51.     // 自动换行和状态栏
  52.     int a = 0;
  53.     boolean flag = false;
  54.  
  55.     public notepad() {
  56.  
  57.         super("记事本");
  58.         this.setSize(768, 384);
  59.         this.setLocationRelativeTo(null);
  60.  
  61.         // 菜单栏
  62.         JMenu wj = new JMenu();
  63.         wj.setText("文件");
  64.         JMenu bj = new JMenu("编辑");
  65.         JMenu gs = new JMenu("格式");
  66.         JMenu ck = new JMenu("查看");
  67.         JMenu bz = new JMenu("帮助");
  68.  
  69.         // 工具栏
  70.         JMenuBar gjl = new JMenuBar();
  71.  
  72.         // 菜单项定义
  73.         final JMenuItem open = new JMenuItem();
  74.         open.setText("打开");
  75.         JMenuItem New = new JMenuItem();
  76.         New.setText("新建");
  77.         JMenuItem save = new JMenuItem();
  78.         save.setText("保存");
  79.         final JMenuItem saveas = new JMenuItem();
  80.         saveas.setText("另存为…");
  81.         JMenuItem exit = new JMenuItem();
  82.         exit.setText("退出");
  83.         final JMenuItem copy = new JMenuItem();
  84.         copy.setText("复制");
  85.         final JMenuItem cut = new JMenuItem();
  86.         cut.setText("剪切");
  87.         JMenuItem paste = new JMenuItem();
  88.         paste.setText("粘贴");
  89.         final JMenuItem selectall = new JMenuItem();
  90.         selectall.setText("全选");
  91.         JMenuItem auto = new JMenuItem();
  92.         auto.setText("自动换行");
  93.         final JMenuItem font = new JMenuItem();
  94.         font.setText("字体");
  95.         JMenuItem about = new JMenuItem();
  96.         about.setText("关于记事本…");
  97.         JMenuItem ztl = new JMenuItem();
  98.         ztl.setText("状态栏");
  99.  
  100.         // 菜单项添加(文件菜单)
  101.         wj.add(open);
  102.         wj.add(New);
  103.         wj.add(save);
  104.         wj.add(saveas);
  105.         wj.addSeparator();
  106.         wj.add(exit);
  107.  
  108.         // 菜单项添加(编辑菜单)
  109.         bj.add(cut);
  110.         bj.add(copy);
  111.         bj.add(paste);
  112.         bj.addSeparator();
  113.         bj.add(selectall);
  114.  
  115.         // 菜单项添加(格式菜单)
  116.         gs.add(auto);
  117.         gs.add(font);
  118.  
  119.         // 菜单项添加(查看菜单)
  120.         ck.add(ztl);
  121.  
  122.         // 菜单项添加(帮助菜单)
  123.         bz.add(about);
  124.  
  125.         // 把各个菜单添加到菜单栏
  126.         gjl.add(wj);
  127.         gjl.add(bj);
  128.         gjl.add(gs);
  129.         gjl.add(ck);
  130.         gjl.add(bz);
  131.         this.setJMenuBar(gjl);
  132.         this.add(ja);
  133.  
  134.         // 字体
  135.         JLabel lbfont = new JLabel("字体");
  136.         JLabel lbsize = new JLabel("大小");
  137.         JLabel lbshape = new JLabel("字形");
  138.         JPanel jpfont1 = new JPanel();
  139.         JPanel jpfont2 = new JPanel();
  140.         JPanel jpfont3 = new JPanel();
  141.         JPanel jpfont4 = new JPanel();
  142.         final JFrame fontMain = new JFrame();
  143.         final JDialog fontJd = new JDialog(fontMain, "字体,颜色,调色板", true);
  144.         JButton ok = new JButton("确定");
  145.         final JButton canel = new JButton("取消");
  146.         JButton color = new JButton("颜色");
  147.         final List fontList = new List(10, false);
  148.         final List fontShape = new List(10, false);
  149.         final List sizeList = new List(10, false);
  150.  
  151.         // 加系统字体
  152.         String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment()
  153.                 .getAvailableFontFamilyNames();
  154.         for (int i = 0; i < fontNames.length; i++) {
  155.             fontList.add(fontNames[i]);
  156.         }
  157.  
  158.         // 字体形状
  159.         fontShape.add("常规");
  160.         fontShape.add("粗体");
  161.         fontShape.add("斜体");
  162.         fontShape.add("粗斜体");
  163.  
  164.         // 设置字体对话框
  165.         fontJd.setSize(450, 300);
  166.         jpfont1.add(lbfont);
  167.         jpfont1.add(fontList);
  168.         jpfont2.add(lbsize);
  169.         jpfont2.add(lbshape);
  170.         jpfont3.add(lbshape);
  171.         jpfont3.add(fontShape);
  172.         jpfont4.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
  173.         jpfont4.add(ok);
  174.         jpfont4.add(canel);
  175.         jpfont4.add(color);
  176.  
  177.         fontJd.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
  178.         fontJd.add(jpfont1);
  179.         fontJd.add(jpfont2);
  180.         fontJd.add(jpfont3);
  181.         fontJd.add(jpfont4);
  182.  
  183.         fontList.select(a);
  184.         sizeList.select(4);
  185.         fontShape.select(0);
  186.  
  187.        
  188.  
  189.         // 取消按钮监听器
  190.         canel.addActionListener(new ActionListener() {
  191.  
  192.             public void actionPerformed(ActionEvent e) {
  193.                 fontMain.setVisible(false);
  194.             }
  195.  
  196.         });
  197.         // 字体大小
  198.         final String[] size = { "1", "2", "3", "4", "5", "6", "7", "8", "9",
  199.                 "10" };
  200.         for (int i = 0; i < size.length; i++) {
  201.             sizeList.add(size[i]);
  202.         }
  203.  
  204.         // 设置颜色按钮监听器
  205.         color.addActionListener(new ActionListener() {
  206.  
  207.             public void actionPerformed(ActionEvent e) {
  208.                 JColorChooser jcolor = new JColorChooser();
  209.                 Color fcolor = ja.getForeground();
  210.                 ja.setForeground(jcolor.showDialog(ja, "选择字体颜色", fcolor));
  211.             }
  212.  
  213.         });
  214.         // 确定按钮监听器
  215.         ok.addActionListener(new ActionListener() {
  216.  
  217.             public void actionPerformed(ActionEvent e) {
  218.                 fontMain.setVisible(false);
  219.             }
  220.  
  221.         });
  222.  
  223.         // 字体监听器
  224.         font.addActionListener(new ActionListener() {
  225.  
  226.             public void actionPerformed(ActionEvent e) {
  227.                 fontJd.setLocationRelativeTo(notepad.this);
  228.                 fontJd.setVisible(true);
  229.  
  230.                 int style = 0;
  231.                 if (fontShape.getSelectedItem().equals("常规") == true) {
  232.                     style = Font.PLAIN;
  233.                 }
  234.                 if (fontShape.getSelectedItem().equals("斜体") == true) {
  235.                     style = Font.ITALIC;
  236.                 }
  237.                 if (fontShape.getSelectedItem().equals("粗体") == true) {
  238.                     style = Font.BOLD;
  239.                 }
  240.                 if (fontShape.getSelectedItem().equals("粗斜体") == true) {
  241.                     style = Font.BOLD + Font.ITALIC;
  242.                 }
  243.  
  244.                 // 设置字体大小
  245.                 String[] size = { "1", "2", "3", "4", "5", "6", "7", "8", "9",
  246.                         "10" };
  247.                 for (int i = 0; i < size.length; i++) {
  248.                     if (sizeList.getSelectedItem().equals(size[i]) == true) {
  249.                         ja.setFont(new Font(String.valueOf(fontList
  250.                                 .getSelectedItem()), style, (10 - i) * 10));
  251.                         fontJd.dispose();
  252.                     }
  253.                 }
  254.  
  255.             }
  256.         });
  257.  
  258.         // 文件改变事件
  259.         ja.getDocument().addDocumentListener(new DocumentListener() {
  260.  
  261.             public void changedUpdate(DocumentEvent e) {
  262.                 flag = true;
  263.             }
  264.  
  265.             public void insertUpdate(DocumentEvent e) {
  266.                 flag = true;
  267.             }
  268.  
  269.             public void removeUpdate(DocumentEvent e) {
  270.                 flag = true;
  271.             }
  272.  
  273.         });
  274.  
  275.         // 窗口监听器
  276.         this.addWindowListener(new WindowAdapter() {
  277.             public void windowClosing(WindowEvent w) {
  278.                 fileName = notepad.this.getTitle();
  279.                 if (flag == true) {
  280.                     int returnValue = JOptionPane.showConfirmDialog(null,
  281.                             notepad.this.getTitle() + "文件还没有保存!需要保存?", "记事本",
  282.                             JOptionPane.YES_NO_CANCEL_OPTION);
  283.                     if (returnValue == JOptionPane.YES_OPTION) {
  284.                         save_asMethod();
  285.  
  286.                     } else if (returnValue == JOptionPane.NO_OPTION) {
  287.                         System.exit(0);
  288.  
  289.                     } else
  290.                         setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  291.  
  292.                 }
  293.             }
  294.  
  295.             // 退出时用到的保存文件函数
  296.             public void save_asMethod() {
  297.                 FileDialog fd = new FileDialog(notepad.this, "另存为…",
  298.                         FileDialog.SAVE);
  299.                 fd.setFile("*.txt");
  300.                 fd.setVisible(true);
  301.  
  302.                 filePath = fd.getDirectory();
  303.                 fileName = fd.getFile();
  304.  
  305.                 try {
  306.                     FileWriter fw = new FileWriter(filePath + fileName);
  307.                     BufferedWriter bw = new BufferedWriter(fw);
  308.                     PrintWriter pw = new PrintWriter(bw);
  309.  
  310.                     pw.println(ja.getText());
  311.                     pw.flush();
  312.                     pw.close();
  313.                 } catch (Exception ex) {
  314.                     ex.printStackTrace();
  315.                 }
  316.             }
  317.         });
  318.  
  319.         // 右键快捷方式
  320.         final JPopupMenu jp = new JPopupMenu();
  321.         final JMenuItem jcopy = new JMenuItem("复制");
  322.         final JMenuItem jpaste = new JMenuItem("粘贴");
  323.         final JMenuItem jcut = new JMenuItem("剪切");
  324.         final JMenuItem jselectall = new JMenuItem("全选");
  325.         final JMenuItem jfont = new JMenuItem("字体");
  326.  
  327.         jp.add(jcopy);
  328.         jp.add(jpaste);
  329.         jp.add(jcut);
  330.         jp.addSeparator();
  331.         jp.add(jselectall);
  332.         jp.addSeparator();
  333.         jp.add(jfont);
  334.  
  335.         // 右键菜单之字体的事件
  336.  
  337.         // 右键菜单之复制的事件
  338.         jcopy.addActionListener(new ActionListener() {
  339.  
  340.             public void actionPerformed(ActionEvent e) {
  341.                 ja.copy();
  342.             }
  343.  
  344.         });
  345.  
  346.         // 右键菜单之粘贴的事件
  347.         jpaste.addActionListener(new ActionListener() {
  348.  
  349.             public void actionPerformed(ActionEvent e) {
  350.                 ja.paste();
  351.             }
  352.  
  353.         });
  354.  
  355.         // 右键菜单之剪切的事件
  356.         jcut.addActionListener(new ActionListener() {
  357.  
  358.             public void actionPerformed(ActionEvent e) {
  359.                 ja.cut();
  360.             }
  361.  
  362.         });
  363.  
  364.         // 右键菜单之全选的事件
  365.         jselectall.addActionListener(new ActionListener() {
  366.  
  367.             public void actionPerformed(ActionEvent e) {
  368.                 ja.selectAll();
  369.             }
  370.  
  371.         });
  372.  
  373.         // 右键菜单之增加鼠标事件
  374.         ja.addMouseListener(new MouseAdapter() {
  375.  
  376.             public void mouseReleased(MouseEvent e) {
  377.                 if (e.isPopupTrigger()) {
  378.                     jp.show((Component) (e.getSource()), e.getX(), e.getY());
  379.  
  380.                     String temp = ja.getSelectedText();
  381.                     if (temp != null) {
  382.                         copy.setEnabled(true);
  383.                         cut.setEnabled(true);
  384.                         jcopy.setEnabled(true);
  385.                         jcut.setEnabled(true);
  386.                     } else if (temp == null) {
  387.                         copy.setEnabled(false);
  388.                         cut.setEnabled(false);
  389.                         jcopy.setEnabled(false);
  390.                         jcut.setEnabled(false);
  391.                     }
  392.                     String temp1 = ja.getText();
  393.                     if (temp1 == null) {
  394.                         selectall.setEnabled(false);
  395.                         jselectall.setEnabled(false);
  396.                     } else if (temp1 != null) {
  397.                         selectall.setEnabled(true);
  398.                         jselectall.setEnabled(true);
  399.                     }
  400.                 }
  401.             }
  402.         });
  403.  
  404.         // 复制菜单项
  405.         copy.addActionListener(new ActionListener() {
  406.  
  407.             public void actionPerformed(ActionEvent e) {
  408.                 ja.copy();
  409.             }
  410.  
  411.         });
  412.  
  413.         // 粘贴菜单项
  414.         paste.addActionListener(new ActionListener() {
  415.  
  416.             public void actionPerformed(ActionEvent e) {
  417.  
  418.                 try {
  419.                     ja.paste();
  420.                 } catch (Exception ex) {
  421.                     ex.printStackTrace();
  422.                 }
  423.             }
  424.  
  425.         });
  426.  
  427.         // 剪切菜单项
  428.         cut.addActionListener(new ActionListener() {
  429.  
  430.             public void actionPerformed(ActionEvent e) {
  431.                 try {
  432.                     ja.cut();
  433.                 } catch (Exception ex) {
  434.                     ex.printStackTrace();
  435.                 }
  436.             }
  437.  
  438.         });
  439.  
  440.         // 全选菜单项
  441.         selectall.addActionListener(new ActionListener() {
  442.  
  443.             public void actionPerformed(ActionEvent e) {
  444.                 ja.selectAll();
  445.             }
  446.  
  447.         });
  448.  
  449.         // 换行菜单项
  450.         auto.addActionListener(new ActionListener() {
  451.  
  452.             public void actionPerformed(ActionEvent e) {
  453.                 if (a == 0) {
  454.                     ja.setLineWrap(true);
  455.                     a = 1;
  456.                 } else if (a == 1) {
  457.                     ja.setLineWrap(false);
  458.                     a = 0;
  459.                 }
  460.             }
  461.  
  462.         });
  463.  
  464.         // 新建菜单项
  465.         New.addActionListener(new ActionListener() {
  466.  
  467.             public void actionPerformed(ActionEvent e) {
  468.                 notepad.this.setTitle("无标题——记事本.txt");
  469.                 ja.setText(" ");
  470.             }
  471.  
  472.         });
  473.  
  474.         // 退出菜单项
  475.         exit.addActionListener(new ActionListener() {
  476.  
  477.             public void actionPerformed(ActionEvent e) {
  478.                 fileName = notepad.this.getTitle();
  479.                 if (flag == true) {
  480.                     int returnValue = JOptionPane.showConfirmDialog(null,
  481.                             notepad.this.getTitle() + "文件还没有保存!需要保存?", "记事本",
  482.                             JOptionPane.YES_NO_CANCEL_OPTION);
  483.                     if (returnValue == JOptionPane.YES_OPTION) {
  484.                         save_asMethod();
  485.  
  486.                     } else if (returnValue == JOptionPane.NO_OPTION) {
  487.                         System.exit(0);
  488.  
  489.                     } else
  490.                         setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  491.  
  492.                 }
  493.             }
  494.  
  495.             public void save_asMethod() {
  496.                 FileDialog fd = new FileDialog(notepad.this, "另存为…",
  497.                         FileDialog.SAVE);
  498.                 fd.setFile("*.txt");
  499.                 fd.setVisible(true);
  500.  
  501.                 filePath = fd.getDirectory();
  502.                 fileName = fd.getFile();
  503.  
  504.                 try {
  505.                     FileWriter fw = new FileWriter(filePath + fileName);
  506.                     BufferedWriter bw = new BufferedWriter(fw);
  507.                     PrintWriter pw = new PrintWriter(bw);
  508.  
  509.                     pw.println(ja.getText());
  510.                     pw.flush();
  511.                     pw.close();
  512.                 } catch (Exception ex) {
  513.                     ex.printStackTrace();
  514.                 }
  515.             }
  516.  
  517.         });
  518.  
  519.         // 保存菜单项
  520.         save.addActionListener(new ActionListener() {
  521.  
  522.             public void actionPerformed(ActionEvent e) {
  523.                 fileName = notepad.this.getTitle();
  524.                 try {
  525.                     FileWriter fw = new FileWriter(fileName + ".txt");
  526.                     String save = ja.getText();
  527.                     fw.write(save);
  528.                     fw.close();
  529.                 } catch (Exception ex) {
  530.                     ex.printStackTrace();
  531.                 }
  532.                 JOptionPane.showMessageDialog(null, "文件已经成功保存了!", "文件保存",
  533.                         JOptionPane.INFORMATION_MESSAGE);
  534.             }
  535.  
  536.         });
  537.  
  538.         // 另存菜单项
  539.         saveas.addActionListener(new ActionListener() {
  540.  
  541.             public void actionPerformed(ActionEvent arg0) {
  542.                 FileDialog fd = new FileDialog(notepad.this, "另存为…",
  543.                         FileDialog.SAVE);
  544.                 fd.setFile("*.txt");
  545.                 fd.setVisible(true);
  546.  
  547.                 filePath = fd.getDirectory();
  548.                 fileName = fd.getFile();
  549.  
  550.                 try {
  551.                     FileWriter fw = new FileWriter(filePath + fileName);
  552.                     BufferedWriter bw = new BufferedWriter(fw);
  553.                     PrintWriter pw = new PrintWriter(bw);
  554.  
  555.                     pw.println(ja.getText());
  556.                     pw.flush();
  557.                     pw.close();
  558.                 } catch (Exception ex) {
  559.                     ex.printStackTrace();
  560.                 }
  561.             }
  562.  
  563.         });
  564.  
  565.         // 打开菜单项
  566.         open.addActionListener(new ActionListener() {
  567.  
  568.             public void actionPerformed(ActionEvent e) {
  569.                 FileDialog fd1 = new FileDialog(notepad.this, "打开",
  570.                         FileDialog.LOAD);
  571.                 fd1.setFile("*.txt");
  572.                 fd1.setVisible(true);
  573.  
  574.                 fileName = fd1.getFile();
  575.                 filePath = fd1.getDirectory();
  576.                 notepad.this.setTitle(fileName);
  577.                 try {
  578.                     FileReader fr = new FileReader(filePath + fileName);
  579.                     BufferedReader br = new BufferedReader(fr);
  580.                     String sinput = "";
  581.                     ja.setText("");
  582.                     int lineNum = 0;
  583.                     while ((sinput = br.readLine()) != null) {
  584.                         // System.out.println(sinput);
  585.                         ja.append(sinput + "\r\n");
  586.                         lineNum++;
  587.                     }
  588.                 } catch (Exception ex) {
  589.                     ex.printStackTrace();
  590.                 }
  591.             }
  592.  
  593.         });
  594.  
  595.         // 关于菜单项
  596.         final JFrame jfm = new JFrame();
  597.         JLabel lb1 = new JLabel("本记事本有zyw制作\n");
  598.         JLabel lb2 = new JLabel("版权所有,盗版必究!\n");
  599.         JLabel lb3 = new JLabel("谢谢使用!\n");
  600.  
  601.         jfm.setSize(200, 170);
  602.         jfm.setTitle("关于本软件");
  603.         jfm.add(lb1);
  604.         jfm.add(lb2);
  605.         jfm.add(lb3);
  606.         final JButton qd = new JButton("确定");
  607.         qd.setSize(20, 17);
  608.         jfm.add(qd);
  609.  
  610.         // 关于菜单项
  611.         about.addActionListener(new ActionListener() {
  612.  
  613.             public void actionPerformed(ActionEvent e) {
  614.                 jfm.setLocationRelativeTo(notepad.this);
  615.                 jfm.setVisible(true);
  616.                 jfm.setLayout(new GridLayout(4, 1, 5, 10));
  617.                 jfm.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
  618.  
  619.                 qd.addActionListener(new ActionListener() {
  620.  
  621.                     public void actionPerformed(ActionEvent e) {
  622.                         jfm.setVisible(false);
  623.                     }
  624.  
  625.                 });
  626.             }
  627.  
  628.         });
  629.  
  630.         // 状态栏菜单项
  631.         final JLabel lb = new JLabel();
  632.         ztl.addActionListener(new ActionListener() {
  633.  
  634.             public void actionPerformed(ActionEvent e) {
  635.                 if (a == 1) {
  636.                     lb.setVisible(false);
  637.                     a = 0;
  638.                 } else if (a == 0) {
  639.                     notepad.this.add(lb, BorderLayout.SOUTH);
  640.                     lb.setVisible(true);
  641.                     lb.setText("当前字数: "
  642.                             + String.valueOf(ja.getText().trim().length())
  643.                             + " " + "当前行数: "
  644.                             + String.valueOf(ja.getLineCount()));
  645.                     a = 1;
  646.                 }
  647.             }
  648.  
  649.         });
  650.  
  651.         // 快捷键
  652.         open.setAccelerator(KeyStroke.getKeyStroke('O', InputEvent.CTRL_MASK));
  653.         New.setAccelerator(KeyStroke.getKeyStroke('N', InputEvent.CTRL_MASK));
  654.         save.setAccelerator(KeyStroke.getKeyStroke('S', InputEvent.CTRL_MASK));
  655.         exit.setAccelerator(KeyStroke.getKeyStroke('Q', InputEvent.CTRL_MASK));
  656.  
  657.         copy.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK));
  658.         paste.setAccelerator(KeyStroke.getKeyStroke('V', InputEvent.CTRL_MASK));
  659.         cut.setAccelerator(KeyStroke.getKeyStroke('X', InputEvent.CTRL_MASK));
  660.         selectall.setAccelerator(KeyStroke.getKeyStroke('A',
  661.                 InputEvent.CTRL_MASK));
  662.  
  663.         auto.setAccelerator(KeyStroke.getKeyStroke('H', InputEvent.CTRL_MASK));
  664.         font.setAccelerator(KeyStroke.getKeyStroke('F', InputEvent.ALT_MASK));
  665.  
  666.         // 添加滚动条
  667.         JScrollPane jsp = new JScrollPane(ja);
  668.         Container c = this.getContentPane();
  669.         c.add(jsp);
  670.         jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); // 需要的时候出现
  671.         jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); // 总是存在
  672.  
  673.         this.setVisible(true);
  674.     }
  675.  
  676.     private static String change(String s) {
  677.         byte abyte0[] = s.getBytes();
  678.         char ac[] = new char[s.length()];
  679.         int i = 0;
  680.         for (int k = abyte0.length; i < k; i++) {
  681.             int j = abyte0[i];
  682.             if (j >= 48 && j <= 57)
  683.                 j = ((j - 48) + 5) % 10 + 48;
  684.             else if (j >= 65 && j <= 90)
  685.                 j = ((j - 65) + 13) % 26 + 65;
  686.             else if (j >= 97 && j <= 122)
  687.                 j = ((j - 97) + 13) % 26 + 97;
  688.             ac[i] = (char) j;
  689.         }
  690.  
  691.         return String.valueOf(ac);
  692.     }
  693.  
  694.     public static void main(String[] args) {
  695.  
  696.         notepad jsb = new notepad();
  697.     }
  698.  
  699. }
  700.  

回复 "java代码生成记事本窗体"

这儿你可以回复上面这条便签

captcha