Как подсветить синтаксис в своем блокноте? - Java

Узнай цену своей работы

Формулировка задачи:

Всем привет, такая проблема, есть написанный блокнот, надо сделать так, чтоб там, подсвечивался синтаксис java кода, не могу понять как это можно реализовать, новичок в этом...( Всем заранее большое спасибо!

Решение задачи: «Как подсветить синтаксис в своем блокноте?»

textual
Листинг программы
  1. package avallon;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.awt.BorderLayout;
  6. import java.io.*;
  7. import java.util.Scanner;
  8.  
  9.  
  10.  
  11. public class MorozovPAD extends JFrame {
  12.  
  13.    private JEditorPane textArea = new JEditorPane();
  14.     JTextPane jTextPanel = new JTextPane();
  15.     JTextArea theText = new JTextArea();
  16.     JPanel contentPanel;
  17.     BorderLayout borderLayout = new BorderLayout();
  18.     JMenu jMenuFile = new JMenu();
  19.     JMenuBar jMenuBar = new JMenuBar();
  20.     JLabel statusBar = new JLabel();
  21.     JMenu File = new JMenu();
  22.     JMenuItem open = new JMenuItem();
  23.     JMenuItem save = new JMenuItem();
  24.     JMenuItem exit = new JMenuItem();
  25.  
  26.     JMenu View= new JMenu();
  27.     JMenuItem bold = new JMenuItem();
  28.     JMenuItem italic = new JMenuItem();
  29.  
  30.     JMenu Copytext= new JMenu();
  31.     JMenuItem copy = new JMenuItem();
  32.     JMenuItem past = new JMenuItem();
  33.     JMenuItem cut = new JMenuItem();
  34.  
  35.         JMenu Textoption= new JMenu();
  36.     JMenuItem textj= new JMenuItem();
  37.     JMenuItem texti= new JMenuItem();
  38.     JMenuItem textp= new JMenuItem();
  39.  
  40.     JMenu Help= new JMenu();
  41.     JMenuItem popi = new JMenuItem();
  42.  
  43.  
  44.     public MorozovPAD() throws Exception {
  45.         this.setSize (500,500);
  46.         this.setTitle("MorozovPAD");
  47.         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  48.         this.textArea.setFont(new Font("", Font.PLAIN, 14));
  49.         this.getContentPane().setLayout(new BorderLayout());
  50.         this.getContentPane().add(textArea);
  51.  
  52.         jMenuBar.add(File);
  53.         jMenuBar.add(Copytext);
  54.         jMenuBar.add(Textoption);
  55.         jMenuBar.add(Help);
  56.         setJMenuBar(jMenuBar);
  57.  
  58.  
  59.                 File.setText("Файл");
  60.         open.setText("Открыть");
  61.         File.add(open);
  62.         save.setText("Сохранить");
  63.         File.add(save);
  64.         exit.setText("Выход");
  65.         File.add(exit);
  66.  
  67.         exit.addActionListener(new ActionListener() {
  68.  
  69.             public void actionPerformed(ActionEvent e)
  70.            {
  71.                 System.exit(0);
  72.             }
  73.         });
  74.    save.addActionListener(new ActionListener() {
  75.     public void actionPerformed(ActionEvent arg0) {
  76.         JFileChooser save = new JFileChooser();
  77.         int option = save.showSaveDialog(save);
  78.         if (option == JFileChooser.APPROVE_OPTION) {
  79.             try {
  80.                 BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
  81.                 out.write(textArea.getText());
  82.                 out.close();
  83.             } catch (Exception ex) {
  84.                 System.out.println(ex.getMessage());
  85.                 }
  86.         }
  87.       }
  88.     });
  89.  
  90.  
  91.     open.addActionListener(new ActionListener(){
  92.       public void actionPerformed(ActionEvent arg0) {
  93.          JFileChooser open = new JFileChooser();
  94.                         int option = open.showOpenDialog(open);
  95.                         if (option == JFileChooser.APPROVE_OPTION) {
  96.                                 theText.setText("");
  97.                                 try {
  98.                                         Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
  99.                                         while (scan.hasNext())
  100.                                                 textArea.setText(scan.nextLine() + "\n");
  101.                                 } catch (Exception ex) {
  102.                                         System.out.println(ex.getMessage());
  103.                                 }
  104.                         }
  105.       }
  106.     });
  107.  
  108.                
  109.                 Copytext.setText("Копирование");
  110.         copy.setText("Копировать");
  111.         Copytext.add(copy);
  112.         past.setText("Вставить");
  113.         Copytext.add(past);
  114.         cut.setText("Вырезать");
  115.         Copytext.add(cut);
  116.                 copy.addActionListener(new ActionListener() {
  117.  
  118.             public void actionPerformed(ActionEvent e) {
  119.                 textArea.copy();
  120.             }
  121.         });
  122.         past.addActionListener(new ActionListener() {
  123.  
  124.             public void actionPerformed(ActionEvent e) {
  125.                 textArea.paste();
  126.             }
  127.         });
  128.         cut.addActionListener(new ActionListener() {
  129.  
  130.             public void actionPerformed(ActionEvent e) {
  131.                 textArea.cut();
  132.             }
  133.         });
  134.  
  135.  
  136.                  Textoption.setText("Изменение шрифта");
  137.         textj.setText("Жирный");
  138.         Textoption.add(textj);
  139.         texti.setText("Курсивный");
  140.         Textoption.add(texti);
  141.         textp.setText("Стандартный");
  142.         Textoption.add(textp);
  143.         texti.addActionListener(new ActionListener() {
  144.  
  145.             public void actionPerformed(ActionEvent ae) {
  146.                 textArea.setFont(new Font ("TAHOMA", Font.ITALIC, 14));
  147.             }
  148.         });
  149.         textj.addActionListener(new ActionListener() {
  150.  
  151.         public void actionPerformed(ActionEvent ae) {
  152.                textArea.setFont(new Font ("", Font.BOLD, 14));
  153.             }
  154.         });
  155.         textp.addActionListener(new ActionListener() {
  156.  
  157.         public void actionPerformed(ActionEvent ae) {
  158.                textArea.setFont(new Font ("", Font.PLAIN, 14));
  159.             }
  160.         });
  161.  
  162.  
  163.                 Help.setText("Помощь");
  164.         popi.setText("Подсказка");
  165.         Help.add(popi);
  166.         popi.addActionListener(new ActionListener() {
  167.  
  168.             public void actionPerformed(ActionEvent e)
  169.             {
  170.             JOptionPane.showMessageDialog(exit, "группа ИС42");
  171.             }
  172.         });
  173.     }
  174.  
  175.     public static void main (String args[]) throws Exception{
  176.         MorozovPAD app = new MorozovPAD();
  177.         app.setVisible(true);
  178.            }
  179. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 4.5 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут