开发者

Artifacts when changing background colour of JTextArea

开发者 https://www.devze.com 2023-04-12 06:32 出处:网络
I\'m having a problem when setting the background colour of aJTextArea after I set its text. The code is as follows:

I'm having a problem when setting the background colour of a JTextArea after I set its text. The code is as follows:

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();
        thi开发者_运维技巧s.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();
    }
}

If I click the button, the background of the JTextArea changes, but I also get some artifacts in the text area. The "repaint" seems to fix it, but in my application example, it doesn't help, so I was wondering whether there is a better solution to this.

Artifacts when changing background colour of JTextArea


You're just missing one text i think

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


It's because you are using a partially transparent color for the component's background. Try setting your background color's alpha channel value to 255 and see if the artifacts still show up. The call to repaint() fixes the issue because it forces the underlying buffer to be filled with your background color prior to painting the text (I think).


I had the same issue with a project I worked on for school recently. You have to call repaint on the frame too (so I changed the ActionListener to take a JFrame in the constructor). I also rearranged the code to use the content pane of the JFrame. This seems to work for me:

 public Test() {
    this.area = new JTextArea();

    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(area, BorderLayout.CENTER);

    JButton button = new JButton(new MyClickAction(this));
    button.setText("Click Me!");

    this.getContentPane().add(button, BorderLayout.SOUTH);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setPreferredSize(new Dimension(500, 200));

    this.area.setText("this is just a test");

    this.pack();
    this.setVisible(true);
}

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

private class MyClickAction extends AbstractAction 
{
    private JFrame frame;

    public MyClickAction(JFrame frame) {
        this.frame = frame;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        area.setBackground(new Color(0, 0, 123, 138));
        frame.repaint();
    } 
}


I had similar problems and resolved them by using the validate() method on the component in question. So many things it could be... maybe I'll get slammed for this but - speaking as one who has just spent an entire year laboring with Swing - I say to you: RUN!! Swing is just about deprecated.

Learn JavaFx 2.0 and help bury Swing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号