如何使 JTextField 透明?

huangapple go评论100阅读模式
英文:

How to have a transparent JTextField?

问题

这是我在这里的第一篇帖子,所以如果这不是正确的提问方式,我很抱歉。我想要改进的建议。

谈到问题,我看了一个关于透明JTextField的YouTube视频(https://www.youtube.com/watch?v=2Ruc5rkDg1E),但是相同的代码没有产生相同的输出。

  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class TextField3 {
  4. public TextField3() {
  5. ImageIcon icon = new ImageIcon("pic1.jpg");
  6. Color back = new Color(0, 0, 0, 80);
  7. Color fore = new Color(255, 255, 255);
  8. Font font = new Font("Times New Roman", Font.BOLD, 24);
  9. JFrame f1 = new JFrame("My First TextField");
  10. f1.setBounds(150, 50, 900, 500);
  11. f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12. f1.getContentPane().setLayout(null);
  13. JTextField tf = new JTextField("TextField");
  14. tf.setBounds(275, 150, 350, 60);
  15. tf.setHorizontalAlignment(JTextField.CENTER);
  16. tf.setBackground(back);
  17. tf.setForeground(fore);
  18. tf.setFont(font);
  19. f1.add(tf);
  20. f1.setVisible(true);
  21. }
  22. public static void main(String[] args) {
  23. TextField3 tf = new TextField3();
  24. }
  25. }

视频中的输出。

我的输出

上面的代码与视频中的代码不完全相同。实际上,视频中的代码生成了一个空白的JFrame,然后我将代码中的JLabel部分移动到了JFrame的下方,然后才产生了第二张图片中的输出。

我搜索了关于这个问题,并发现将setOpaque设置为false可以解决这个问题。但是当我尝试这样做时,JTextField完全消失,只显示背景图像。有人能帮助解决这个问题吗?

英文:

This is my first post here, so I apologize if this is not the right way to ask. I would like suggestions to improve.

Coming to the question, I saw a youtube video(https://www.youtube.com/watch?v=2Ruc5rkDg1E) about transparent Jtextfield, but the same code isn't giving the same output.

  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class TextField3 {
  4. public TextField3() {
  5. ImageIcon icon = new ImageIcon("pic1.jpg");
  6. //JLabel l1 = new JLabel();
  7. //l1.setBounds(0, 0, 900, 500);
  8. //l1.setIcon(icon);
  9. Color back = new Color(0, 0, 0, 80);
  10. Color fore = new Color(255, 255, 255);
  11. Font font = new Font("Times New Roman", Font.BOLD, 24);
  12. JFrame f1 = new JFrame("My First TextField");
  13. f1.setBounds(150, 50, 900, 500);
  14. //f1.setVisible(true);
  15. f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. f1.getContentPane().setLayout(null);
  17. JLabel l1 = new JLabel();
  18. l1.setBounds(0, 0, 900, 500);
  19. l1.setIcon(icon);
  20. JTextField tf = new JTextField("TextField");
  21. //tf.setOpaque(false);
  22. tf.setBounds(275, 150, 350, 60);
  23. tf.setVisible(true);
  24. tf.setHorizontalAlignment(JTextField.CENTER);
  25. tf.setBackground(back);
  26. tf.setForeground(fore);
  27. tf.setFont(font);
  28. f1.add(l1);
  29. f1.add(tf);
  30. f1.setVisible(true);
  31. }
  32. public static void main(String[] args) {
  33. TextField3 tf = new TextField3();
  34. }
  35. }

The output from the youtube video.

My output

The code above isn't exactly same as the video's code. Actually the code in the video generated a blank JFrame, then I moved the JLabel portion of the code below the JFrame's part, then it produced the output in the 2nd image.

I searched about this issue and found that setOpaque to false will solve this. But when I tried it, the JTextField disappears completely and only the background image is shown. Can someone help in solving this issue?

答案1

得分: 2

Swing设计用于与布局管理器一起使用。您不应该使用null布局,也不应该使用setBounds()方法。

Swing基于父子关系进行设计。如果要让文本字段显示在标签的上方,那么不要将两个组件都添加到框架中。相反,您需要创建这样的结构:

  • 框架
    • 背景标签
      • 文本字段

因此,您的基本代码应该类似于:

  1. JTextField textField = new JTextField("text");
  2. textField.setBackground(...);
  3. JLabel background = new JLabel(...);
  4. background.setLayout(new GridBagLayout());
  5. background.add(textField, new GridBagConstraints());
  6. frame.add(background, BorderLayout.CENTER);

现在您将看到文本字段位于标签的中心。

但是,您仍然会遇到文本字段的半透明背景问题,因为Swing无法正确处理半透明。有关更多信息和解决方案,请查看:https://stackoverflow.com/questions/63362910/why-does-the-alpha-in-this-timer-draw-on-top-of-itself-in-this-java-swing-panel/63364379#63364379

英文:

Swing was designed to be used with layout managers. You should NOT be using a null layout and you should NOT be using setBounds().

Swing is designed on a parent/child relationship. If you want the text field to appear on top of the label then don't add both components to the frame. Instead you need a structure like:

  1. - frame
  2. - background label
  3. - text field

So your basic code should be something like:

  1. JTextField textField = new JTextField("text");
  2. textField.setBackground(...);
  3. JLabel background = new JLabel(...);
  4. label.setlayout( new GridBagLayout() );
  5. label.add(textField, new GridBagConstraints());
  6. frame.add(label, BorderLayout.CENTER);

Now you will see the text field in the center of the label.

However, you will still have a problem with the semi-transparent background of the text field, since Swing does not handle semi-transparency properly. For more information and a solution check out: https://stackoverflow.com/questions/63362910/why-does-the-alpha-in-this-timer-draw-on-top-of-itself-in-this-java-swing-panel/63364379#63364379

答案2

得分: 0

你需要使用 setOpaque(false) 方法来使你的 JTextField 变为透明。那应该可以正常工作。

英文:

You need to use setOpaque(false) method to make your JTextField transparent. That should work.

huangapple
  • 本文由 发表于 2020年8月15日 01:33:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63417528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定