[英]Make JTextField semi-transparent
問題確實說明了一切,我希望使JTextField的背景半透明,並且正在使用計時器使其閃爍。
因此,我發現使用傳統的textField.setBackground()會產生奇怪的圖形故障,其中每次閃爍時,文本字段都比原先要暗。 (見下文)
因此,在在線搜索之后,我嘗試使用以下代碼覆蓋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布爾值設置為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();
}
這是一個經過修改的代碼,其中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();
}
}
}
因此,在在線搜索后,我嘗試覆蓋繪畫方法
另外,有人建議我將文本字段opaque布爾值設置為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,了解為什么這樣做是必需的,以及可以在任何組件上使用而無需每次都進行自定義繪制的簡單可重用解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.