使JTextField半透明

时间:2017-09-28 08:36:50

标签: java swing user-interface colors

问题说明了一切,我想让JTextField的背景半透明,我正在使用计时器让它闪现。

所以我发现使用传统的textField.setBackground()会产生一个奇怪的图形故障,每次闪存时文本字段都应该比它应该更暗。 (见下文)

strange graphical glitch

因此,在线搜索后,我尝试使用以下代码覆盖JTextField的paint方法:

name = new JTextField(15) {
        @Override
        protected void paintComponent(Graphics g) {
            g.setColor(this.getBackground());
            g.fillRect(getX(), getY(), getWidth(), getHeight());
            super.paintComponent(g);
        }
    };

另外,有人建议我将文本字段opaque boolean设置为false。这个我做了也无济于事,现在甚至没有任何红色闪烁,我只是得到了这个:

fields with field.setOpaque(false);

以防它有帮助,这是我用来使字段闪现的代码。

    public void flashField(JTextField field, Color flashColor, final int flashDelay, final int numberOfFlashes) {
    timers.add(new Timer(flashDelay, new ActionListener() {

        int counter = 0;

        @Override
        public void actionPerformed(ActionEvent e) {
            counter++;
            if (counter % 2 == 0)
                field.setBackground(
                        new Color(flashColor.getRed(), flashColor.getBlue(), flashColor.getGreen(), 50));
            else
                field.setBackground(Color.WHITE);

            if (counter == (numberOfFlashes * 2) + 1) {
                ((Timer) e.getSource()).stop();
            }

            field.repaint();
        }
    }));

    timers.get(timers.size() - 1).start();

}

2 个答案:

答案 0 :(得分:0)

这是一个改编的代码,其中Textfield每200ms闪烁一次并且是透明的。

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

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

public TransparentTextFieldExample() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }

            JFrame frame = new JFrame("Testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            JPanel panel = new JPanel();
            panel.setLayout(new GridBagLayout());
            TransparentTextField ttf = new TransparentTextField("Some text!", 20);
            panel.add(ttf);
            frame.add(panel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            flashField(ttf, Color.RED, 1, 20);
        }
    });

}

public void flashField(JTextField field, java.awt.Color flashColor, final int flashDelay, final int numberOfFlashes) {

    Timer tm = new Timer(flashDelay, new ActionListener() {

        int counter = 0;

        @Override
        public void actionPerformed(ActionEvent e) {
            if (counter % 2 == 0) {
                field.setBackground(flashColor);
            } else {
                field.setBackground(Color.WHITE);
            }
            if (counter == (numberOfFlashes * 2) + 1) {
                System.out.println("Inside");
                //((Timer) e.getSource()).stop();
                //  break;
            }

            field.repaint();
            try {
                Thread.sleep(200l);
            } catch (Exception ex) {
            }
            counter++;
        }
    });
    tm.start();

}

public class TransparentTextField extends JTextField {

    public TransparentTextField(String text) {
        super(text);
        init();
    }

    public TransparentTextField(int columns) {
        super(columns);
        init();
    }

    public TransparentTextField(String text, int columns) {
        super(text, columns);
        init();
    }

    protected void init() {
        setOpaque(false);
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
        super.paint(g2d);
        g2d.dispose();
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setColor(getBackground());
        g2d.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g2d);
        g2d.dispose();
    }

}
}

答案 1 :(得分:0)

  

因此,在线搜索后,我试图覆盖绘制方法

     

另外,有人建议我将文本字段opaque boolean设置为false。

你需要同时做两件事。类似的东西:

JPanel panel = new JPanel()
{
    protected void paintComponent(Graphics g)
    {
        g.setColor( getBackground() );
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g);
    }
};
panel.setOpaque(false); // background of parent will be painted first
panel.setBackground( new Color(255, 0, 0, 20) );
frame.add(panel);

查看Background With Transparency了解为什么这是必要的,以及一个简单的可重用解决方案,您可以在任何组件上使用,而无需每次都进行自定义绘制。