gpt4 book ai didi

java - 更改 JTextArea 的背景颜色时的工件

转载 作者:搜寻专家 更新时间:2023-10-31 08:04:12 25 4
gpt4 key购买 nike

我在设置文本后设置 JTextArea 的背景颜色时遇到问题。代码如下:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class Test extends JFrame {

private JTextArea area;

public Test() {
this.setLayout(new BorderLayout());
this.add(this.area = new JTextArea(), BorderLayout.CENTER);
this.add(new JButton(clickAction), BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(500, 200));
this.pack();
this.area.setText("this is just a test");
this.setVisible(true);
}

Action clickAction = new AbstractAction("Click") {
@Override
public void actionPerformed(ActionEvent e) {
area.setBackground(new Color(0, 0, 123, 138));
// repaint();
}
};

public static void main(String[] args) {
new Test();
}
}

如果我单击该按钮,JTextArea 的背景会发生变化,但我还会在文本区域中得到一些瑕疵。 “重绘”似乎修复了它,但在我的应用示例中,它没有帮助,所以我想知道是否有更好的解决方案。

example image

最佳答案

我认为你只是漏掉了一段文字

Action clickAction = new AbstractAction("Click") {
@Override
public void actionPerformed(ActionEvent e) {
area.setBackground(new Color(0, 0, 123, 138));
area.repaint();
}
};

关于java - 更改 JTextArea 的背景颜色时的工件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7719941/

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