突出显示JTextArea中的一个特定行/行 [英] Highlight one specific row/line in JTextArea

查看:131
本文介绍了突出显示JTextArea中的一个特定行/行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图突出显示 JTextArea 中的一个特定行,但我不知道要去做什么。我需要获取特定的行然后突出显示它。我已经阅读了其他帖子,但我仍然不明白如何将它们结合在一起来解决我的问题...帮助将非常感激。

I am trying to highlight just one specific row in JTextArea, but I have no idea as to going about it. I need to get the specific row and then highlight it. I've read the other posts, but I still do not understand how to bring it together to solve my problem...help would be much appreciated.

推荐答案

试试这个代码示例,并询问是否有不明确的事情:

Try your hands on this code example, and do ask if something is not clear :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class TextHighlight
{
    private JTextArea tarea;
    private JComboBox cbox;
    private JTextField lineField;
    private String[] colourNames = {"RED", "ORANGE", "CYAN"};

    private Highlighter.HighlightPainter painter;

    private void createAndDisplayGUI()
    {
        final JFrame frame = new JFrame("Text HIGHLIGHT");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createEmptyBorder(5, 5, 5, 5), "Highlighter JTextArea"));

        tarea = new JTextArea(10, 10);
        JScrollPane scrollPane = new JScrollPane(tarea);
        contentPane.add(scrollPane);

        JButton button = new JButton("HIGHLIGHT TEXT");
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                int selection = JOptionPane.showConfirmDialog(
                        frame, getOptionPanel(), "Highlighting Options : ", JOptionPane.OK_CANCEL_OPTION
                                                , JOptionPane.PLAIN_MESSAGE);
                if (selection == JOptionPane.OK_OPTION)                             
                {
                    System.out.println("OK Selected");
                    int lineNumber = Integer.parseInt(lineField.getText().trim());
                    try
                    {
                        int startIndex = tarea.getLineStartOffset(lineNumber);
                        int endIndex = tarea.getLineEndOffset(lineNumber);
                        String colour = (String) cbox.getSelectedItem();

                        if (colour == colourNames[0])
                        {
                            System.out.println("RED Colour");
                            painter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
                            tarea.getHighlighter().addHighlight(startIndex, endIndex, painter);
                        }
                        else if (colour == colourNames[1])
                        {
                            System.out.println("ORANGE Colour");
                            painter = new DefaultHighlighter.DefaultHighlightPainter(Color.ORANGE);
                            tarea.getHighlighter().addHighlight(startIndex, endIndex, painter);
                        }
                        else if (colour == colourNames[2])
                        {
                            System.out.println("CYAN Colour");
                            painter = new DefaultHighlighter.DefaultHighlightPainter(Color.CYAN);
                            tarea.getHighlighter().addHighlight(startIndex, endIndex, painter);
                        }
                    }
                    catch(BadLocationException ble)
                    {
                        ble.printStackTrace();
                    }
                }
                else if (selection == JOptionPane.CANCEL_OPTION)
                {
                    System.out.println("CANCEL Selected");
                }
                else if (selection == JOptionPane.CLOSED_OPTION)
                {
                    System.out.println("JOptionPane closed deliberately.");
                }
            }
        });

        frame.add(contentPane, BorderLayout.CENTER);
        frame.add(button, BorderLayout.PAGE_END);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel getOptionPanel()
    {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 2, 5, 5));

        JLabel lineNumberLabel = new JLabel("Enter Line Number : ");
        lineField = new JTextField(10);

        JLabel colourLabel = new JLabel("Select One Colour : ");
        cbox = new JComboBox(colourNames);

        panel.add(lineNumberLabel);
        panel.add(lineField);
        panel.add(colourLabel);
        panel.add(cbox);

        return panel;
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new TextHighlight().createAndDisplayGUI();
            }
        });
    }
}

以下是它的输出:

这篇关于突出显示JTextArea中的一个特定行/行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆