JComponents未正确显示

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

JComponents not displaying properly

问题

我的JFrame上显示的两个JButton在我更改了它们的颜色后没有正确显示。当我将鼠标悬停在它们上方时,文本重叠在一起。

以下是我的程序代码:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class ButtonExample extends JFrame {
  4. public static void main(String[] args) {
  5. // 创建JFrame
  6. JFrame frame = new JFrame();
  7. frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  8. frame.setSize(400, 250);
  9. frame.setLocation(5, 5);
  10. frame.setVisible(true);
  11. frame.setLayout(new FlowLayout());
  12. // 创建第一个按钮
  13. JButton button = new JButton("第一个按钮");
  14. button.setBackground(new Color(0, 0, 0, 0));
  15. button.setForeground(new Color(0, 0, 0, 250));
  16. frame.add(button);
  17. // 创建第二个按钮
  18. JButton button2 = new JButton("按钮2");
  19. button2.setBackground(new Color(0, 0, 0, 0));
  20. button2.setForeground(new Color(0, 0, 0, 250));
  21. frame.add(button2);
  22. }
  23. }

以下是程序的输出:

第一次运行程序时:

JComponents未正确显示

当我将鼠标悬停在两个按钮上方时的程序效果:

JComponents未正确显示

英文:

My two jbuttons displayed on my JFrame are not displaying properly after I changed the colors of the JButtons. The text overlaps each other as i hover my mouse over.

Here's my code for the program:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class ButtonExample extends JFrame
  4. {
  5. public static void main (String [] args)
  6. {
  7. //Create jframe
  8. JFrame frame = new JFrame();
  9. frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  10. frame.setSize(400, 250);
  11. frame.setLocation(5, 5);
  12. frame.setVisible(true);
  13. frame.setLayout(new FlowLayout());
  14. //Create first button
  15. JButton button = new JButton("First button");
  16. button.setBackground(new Color(0, 0, 0, 0));
  17. button.setForeground(new Color(0, 0, 0, 250));
  18. frame.add(button);
  19. //Create second button
  20. JButton button2 = new JButton("button 2");
  21. button2.setBackground(new Color(0, 0, 0, 0));
  22. button2.setForeground(new Color(0, 0, 0, 250));
  23. frame.add(button2);
  24. }
  25. }

And here's the output of the program

Program when I first run it:

JComponents未正确显示

Program after I hover my mouse over both buttons:

JComponents未正确显示

答案1

得分: 0

如果您希望按钮是透明的,那么不要设置背景。

而是可以使用:

  1. button.setOpaque(false);

现在 Swing 会在绘制按钮之前自动绘制父组件,因此绘制瑕疵将被移除。

英文:

If you want the button to be transparent then don't set the background.

Instead you can use:

  1. button.setOpaque( false );

Now Swing will automatically paint the parent component first before painting the button so painting artifacts will be removed.

huangapple
  • 本文由 发表于 2020年8月7日 00:51:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63288331.html
匿名

发表评论

匿名网友

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

确定