如何使 JTextField 透明?

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

How to have a transparent JTextField?

问题

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

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

import javax.swing.*;
import java.awt.*;

public class TextField3 {
   public TextField3() {
      ImageIcon icon = new ImageIcon("pic1.jpg");

      Color back = new Color(0, 0, 0, 80);
      Color fore = new Color(255, 255, 255);
      Font font = new Font("Times New Roman", Font.BOLD, 24);

      JFrame f1 = new JFrame("My First TextField");
      f1.setBounds(150, 50, 900, 500);
      f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f1.getContentPane().setLayout(null);

      JTextField tf = new JTextField("TextField");
      tf.setBounds(275, 150, 350, 60);
      tf.setHorizontalAlignment(JTextField.CENTER);

      tf.setBackground(back);
      tf.setForeground(fore);
      tf.setFont(font);

      f1.add(tf);
      f1.setVisible(true);
  }
  public static void main(String[] args) {
      TextField3 tf = new TextField3();
  }
}

视频中的输出。

我的输出

上面的代码与视频中的代码不完全相同。实际上,视频中的代码生成了一个空白的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.

    import javax.swing.*;
    import java.awt.*;

    public class TextField3 {
       public TextField3() {
          ImageIcon icon = new ImageIcon("pic1.jpg");
          //JLabel l1 = new JLabel();
          //l1.setBounds(0, 0, 900, 500);
          //l1.setIcon(icon);

          Color back = new Color(0, 0, 0, 80);
          Color fore = new Color(255, 255, 255);
          Font font = new Font("Times New Roman", Font.BOLD, 24);

          JFrame f1 = new JFrame("My First TextField");
          f1.setBounds(150, 50, 900, 500);
          //f1.setVisible(true);
          f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          f1.getContentPane().setLayout(null);

          JLabel l1 = new JLabel();
          l1.setBounds(0, 0, 900, 500);
          l1.setIcon(icon);

          JTextField tf = new JTextField("TextField");
          //tf.setOpaque(false);
          tf.setBounds(275, 150, 350, 60);
          tf.setVisible(true);
          tf.setHorizontalAlignment(JTextField.CENTER);

          tf.setBackground(back);
          tf.setForeground(fore);
          tf.setFont(font);

          f1.add(l1);
          f1.add(tf);
          f1.setVisible(true);
      }
      public static void main(String[] args) {
          TextField3 tf = new TextField3();
      }
    }

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

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

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

JTextField textField = new JTextField("text");
textField.setBackground(...);

JLabel background = new JLabel(...);
background.setLayout(new GridBagLayout());
background.add(textField, new GridBagConstraints());

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:

- frame
    - background label
        - text field

So your basic code should be something like:

JTextField textField = new JTextField("text");
textField.setBackground(...);

JLabel background = new JLabel(...);
label.setlayout( new GridBagLayout() );
label.add(textField, new GridBagConstraints());

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:

确定