如何将渐变背景添加到JTextArea?

时间:2017-02-19 14:39:24

标签: java swing background jtextarea

textArea.setBackground(Color.RED); 允许您在其中添加特定颜色,但如果我想为其添加渐变颜色该怎么办

代码如下

public class IncomingTextArea extends JTextArea {

IncomingTextArea(int width,int height){
    super(width,height);
}
@Override
public void paintComponent(Graphics g) {

    Graphics2D g2D = (Graphics2D) g;

    int red = (int) Math.random() * 255;
    int green = (int) Math.random() * 255;
    int blue = (int) Math.random() * 255;
    Color startColor = new Color(red, green, blue);

    red = (int) Math.random() * 255;
    green = (int) Math.random() * 255;
    blue = (int) Math.random() * 255;
    Color endColor = new Color(red, green, blue);

    GradientPaint gradientPaint = new GradientPaint(70, 70, startColor, 150, 150, endColor);
    g2D.**??**
    super.paintComponent(g2D);

}

}

但我无法找到正确的方法来为其分配渐变值。 好.paint(gradientPaint)适用于形状和东西,但整个textArea呢?

2 个答案:

答案 0 :(得分:2)

我错了 - 不要扩展JTextArea,而是扩展保存JTextArea的JScrollPane的JViewport,在其paintComponent方法中绘制,并确保您的JTextArea是非透明的。

所以我像这样扩展视口:

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JViewport;

public @SuppressWarnings("serial") class GradientViewport extends JViewport {
    private Color c1;
    private Color c2;

    public GradientViewport(Color c1, Color c2) {
        this.c1 = c1;
        this.c2 = c2;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        GradientPaint gPaint = new GradientPaint(0, 0, c1, getWidth(), getHeight(), c2, false);
        Graphics2D g2 = (Graphics2D) g;
        g2.setPaint(gPaint);
        g2.fillRect(0, 0, getWidth(), getHeight());
    }

}

再次,在视口的paintComponent方法中绘制渐变。

然后我会这样使用它:

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class GradientTextAreaTest extends JPanel {
    public static final Color C1 = new Color(255, 200, 200);
    public static final Color C2 = new Color(200, 200, 255);
    private JTextArea textArea = new JTextArea(30, 40);

    // create the view port with colors passed into it
    private GradientViewport viewport = new GradientViewport(C1, C2);
    private JScrollPane scrollPane = new JScrollPane();

    public GradientTextAreaTest() {
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);

        // make the JTextArea transparent
        textArea.setOpaque(false);

        // set the viewport's view with your JTextArea
        viewport.setView(textArea);

        // set the JScrollPane's viewport with our viewport object
        scrollPane.setViewport(viewport);

        // add the JScrollPane to our GUI
        add(scrollPane);
    }

    private static void createAndShowGui() {
        GradientTextAreaTest mainPanel = new GradientTextAreaTest();

        JFrame frame = new JFrame("GradientTextAreaTest");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

}

或者显示:

enter image description here

使用上面这样的类:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class GradientTextAreaTest extends JPanel {
    public static final String TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt "
            + "ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut "
            + "aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu "
            + "fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit "
            + "anim id est laborum.";
    public static final int FILLER = 130;
    public static final Color C1 = new Color(255, FILLER, FILLER);
    public static final Color C2 = new Color(FILLER, FILLER, 255);
    private JTextArea textArea = new JTextArea(14, 30);

    // create the view port with colors passed into it
    private GradientViewport viewport = new GradientViewport(C1, C2);
    private JScrollPane scrollPane = new JScrollPane();

    public GradientTextAreaTest() {
        setLayout(new BorderLayout());
        textArea.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 32));
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        for (int i = 0; i < 10; i++) {
            textArea.append(TEXT + "\n");
        }

        // make the JTextArea transparent
        textArea.setOpaque(false);

        // set the viewport's view with your JTextArea
        viewport.setView(textArea);

        // set the JScrollPane's viewport with our viewport object
        scrollPane.setViewport(viewport);

        // add the JScrollPane to our GUI
        add(scrollPane);
    }

    private static void createAndShowGui() {
        GradientTextAreaTest mainPanel = new GradientTextAreaTest();

        JFrame frame = new JFrame("GradientTextAreaTest");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

}

答案 1 :(得分:0)

@Hovercraft Full Of Eels讲述的方法非常好

好吧,我找到了另一种解决办法。 像往常一样用渐变绘制矩形或任何其他对象,即 g2D.setPaint(gradientPaint); g2D.fillRect(0, 0, getWidth(), getHeight());

然后 textArea.setOpaque(false);

我知道的那种打击和审判:p

但是感谢@Hovercraft Full Of Eels