Swing: Как кодировать «установить цвета» для текста со стороны пользователя? - PullRequest
0 голосов
/ 11 января 2019

Я должен сделать проект для обучения (домашнее задание), где мы обновляем коммунальные услуги. Одна моя особенность - «раскраска текста».

Я никогда этого не делал, но у меня маленькая идея. Я хочу связать, например, $ 0 до $ 9 с цветами: BLUE,RED,BLACK,WHITE... (снимок экрана). Когда я подтверждаю настройку «$ x» не отображается, но текст должен был изменить цвет.

Мне нужна информация, статья или что-то, чтобы научиться это делать. Я знаю, я должен сделать новый класс с конструктором цветов и 2-й класс, где у меня есть многострочный текст и однострочный текст, где я должен реализовать эти цвета - вот и все.

Спасибо за каждую помощь. Привет.

PS. Извините за мой не идеальный английский

https://imgur.com/a/QUdy7NT

enter image description here

Классы, где я должен реализовать эту опцию с цветами:

package com.horstmann.violet.product.diagram.propertyeditor.baseeditors;

    import javax.swing.*;
    import javax.swing.text.JTextComponent;

    import com.horstmann.violet.product.diagram.property.text.LineText;
    import com.horstmann.violet.product.diagram.property.text.MultiLineText;

    /**
     * A property editor for the MultiLineText type.
     */
    public class MultiLineTextEditor extends LineTextEditor
    {
        protected void setSourceEditor()
        {
            this.source = (MultiLineText) getValue();
        }
        protected LineText getSourceEditor()
        {
            return this.source;
        }
        protected JTextComponent createTextComponent()
        {
            return new JTextArea(ROWS, COLUMNS);
        }

        protected JComponent createScrollPanel(JTextComponent textComponent)
        {
            return new JScrollPane(textComponent);
        }

        private MultiLineText source;

        private static final int ROWS = 5;
    }


    package com.horstmann.violet.product.diagram.propertyeditor.baseeditors;

    import com.horstmann.violet.product.diagram.property.text.LineText;
    import com.horstmann.violet.product.diagram.property.text.SingleLineText;

    import javax.swing.*;
    import javax.swing.text.JTextComponent;

    public class SingleLineTextEditor extends LineTextEditor
    {
        protected void setSourceEditor()
        {
            this.source = (SingleLineText) getValue();
        }
        protected LineText getSourceEditor()
        {
            return this.source;
        }
        protected JTextComponent createTextComponent()
        {
            return new JTextField(COLUMNS);
        }

        private SingleLineText source;
    }

    public abstract class LineTextEditor extends PropertyEditorSupport
    {
        protected abstract void setSourceEditor();
        protected abstract LineText getSourceEditor();
        protected abstract JTextComponent createTextComponent();

        public boolean supportsCustomEditor()
        {
            return true;
        }

        public Component getCustomEditor()
        {
            setSourceEditor();
            final JPanel panel = new JPanel();
            panel.add(getTextEditorComponent());
            return panel;
        }

        private JComponent getTextEditorComponent()
        {
            if (this.textEditorComponent == null)
            {
                final JTextComponent textComponent = createTextComponent();

                textComponent.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, tab);
                textComponent.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, shiftTab);

                textComponent.setText(getSourceEditor().toEdit());
                textComponent.getDocument().addDocumentListener(new DocumentListener()
                {
                    public void insertUpdate(DocumentEvent e)
                    {
                        getSourceEditor().setText(textComponent.getText());
                        firePropertyChange();
                    }

                    public void removeUpdate(DocumentEvent e)
                    {
                        getSourceEditor().setText(textComponent.getText());
                        firePropertyChange();
                    }

                    public void changedUpdate(DocumentEvent e)
                    {
                    }
                });
                this.textEditorComponent = createScrollPanel(textComponent);
            }
            return this.textEditorComponent;
        }

        protected JComponent createScrollPanel(JTextComponent textComponent)
        {
            return textComponent;
        }

        protected JComponent textEditorComponent;

        protected static final int COLUMNS = 30;

        protected static Set<KeyStroke> tab = new HashSet<KeyStroke>(1);
        protected static Set<KeyStroke> shiftTab = new HashSet<KeyStroke>(1);
        static
        {
            tab.add(KeyStroke.getKeyStroke("TAB"));
            shiftTab.add(KeyStroke.getKeyStroke("shift TAB"));
        }
    }
...