透明的JTextField .setOpaque(false)不起作用

huangapple 未分类评论46阅读模式
英文:

Transparent JTextField .setOpaque(false) doesn't work

问题

  1. JTextField textbox1;
  2. textbox1 = new JTextField();
  3. textbox1.setBounds((549+x),(61+y),295,17);
  4. textbox1.setOpaque(false);
  5. Main.panel.add(textbox1);

我需要一个位于图像上方的文本框,以便在文本框内输入内容时能够看到图像。我尝试过使用 textbox1.setOpaque(false) 方法,但没有改变任何内容,并且没有报错。如果代码格式不正确,我很抱歉,我尝试过了,但我很少使用这个网站。

英文:
  1. JTextField textbox1;
  2. textbox1 = new JTextField();
  3. textbox1.setBounds((549+x),(61+y),295,17);
  4. textbox1.setOpaque(false);
  5. Main.panel.add(textbox1);

I need a Textbox on top of an image to show the image underneath but still be able to be typed in. I've tried using the textbox1.setOpaque(false) method but it didn't change anything and didn't throw and error. Sorry if i didn't format the code properly I tried but I just don't use this site very often.

答案1

得分: 0

设置文本框的背景为透明

  1. setBackground(new Color(0,0,0,0));

使用

  1. setOpaque(false);

只有当您的文本框内部有子组件时,您想要使子组件可见,但不包括文本框本身,以避免产生伪影。

以下示例在红色圆圈上方放置了一个文本框。

  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class JunkStop{
  4. public static void main(String[] args){
  5. JFrame frame = new JFrame("wakka");
  6. JPanel layout = new JPanel(null);
  7. JPanel background = new JPanel(){
  8. public void paintComponent(Graphics g){
  9. super.paintComponent(g);
  10. g.setColor(Color.RED);
  11. g.fillOval(100, 100, 200, 200);
  12. }
  13. };
  14. background.setOpaque(false);
  15. JTextField field = new JTextField("testing");
  16. field.setBackground( new Color(0, 0, 0, 0) );
  17. layout.add(field);
  18. layout.add(background);
  19. field.setSize(new Dimension(200, 20));
  20. field.setBounds(100, 150, 200, 20);
  21. background.setBounds(0, 0, 400, 400);
  22. frame.setContentPane(layout);
  23. frame.setVisible(true);
  24. frame.setSize(400, 400);
  25. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  26. }
  27. }
英文:

set only the background of the text box to transparent

  1. setBackground(new Color(0,0,0,0));

Use

  1. setOpaque(false);

Only when your text field has child components inside the textfield you want to make visible but not the textfield itself to avoid artifacts

The following example has a text field over a red circle.

  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class JunkStop{
  4. public static void main(String[] args){
  5. JFrame frame = new JFrame("wakka");
  6. JPanel layout = new JPanel(null);
  7. JPanel background = new JPanel(){
  8. public void paintComponent(Graphics g){
  9. super.paintComponent(g);
  10. g.setColor(Color.RED);
  11. g.fillOval(100, 100, 200, 200);
  12. }
  13. };
  14. background.setOpaque(false);
  15. JTextField field = new JTextField("testing");
  16. field.setBackground( new Color(0, 0, 0, 0) );
  17. layout.add(field);
  18. layout.add(background);
  19. field.setSize(new Dimension(200, 20));
  20. field.setBounds(100, 150, 200, 20);
  21. background.setBounds(0, 0, 400, 400);
  22. frame.setContentPane(layout);
  23. frame.setVisible(true);
  24. frame.setSize(400, 400);
  25. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  26. }
  27. }

huangapple
  • 本文由 发表于 2020年7月27日 14:27:52
  • 转载请务必保留本文链接:https://java.coder-hub.com/63109763.html
匿名

发表评论

匿名网友

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

确定