gpt4 book ai didi

java - 如何给JTextArea添加渐变背景?

转载 作者:行者123 更新时间:2023-12-02 03:01:59 25 4
gpt4 key购买 nike

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 呢?

最佳答案

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

所以我会像这样扩展视口(viewport):

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());
}

}

再次在视口(viewport)的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());
}

}

关于java - 如何给JTextArea添加渐变背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42328547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com