JComponents未正确显示

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

JComponents not displaying properly

问题

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

以下是我的程序代码:

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

public class ButtonExample extends JFrame {
    public static void main(String[] args) {
        // 创建JFrame
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(400, 250);
        frame.setLocation(5, 5);
        frame.setVisible(true);
        frame.setLayout(new FlowLayout());

        // 创建第一个按钮
        JButton button = new JButton("第一个按钮");
        button.setBackground(new Color(0, 0, 0, 0));
        button.setForeground(new Color(0, 0, 0, 250));
        frame.add(button);

        // 创建第二个按钮
        JButton button2 = new JButton("按钮2");
        button2.setBackground(new Color(0, 0, 0, 0));
        button2.setForeground(new Color(0, 0, 0, 250));
        frame.add(button2);
    }
}

以下是程序的输出:

第一次运行程序时:

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:

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

public class ButtonExample extends JFrame
{
   public static void main (String [] args) 
  {
	//Create jframe
	JFrame frame = new JFrame();
	frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	frame.setSize(400, 250);
	frame.setLocation(5, 5);
	frame.setVisible(true);
	frame.setLayout(new FlowLayout());
	
	//Create first button
	JButton button = new JButton("First button");
	button.setBackground(new Color(0, 0, 0, 0));
	button.setForeground(new Color(0, 0, 0, 250));
	frame.add(button);
	
	//Create second button
	JButton button2 = new JButton("button 2");
	button2.setBackground(new Color(0, 0, 0, 0));
	button2.setForeground(new Color(0, 0, 0, 250));
	frame.add(button2);
   }
 }

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

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

而是可以使用:

button.setOpaque(false);

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

英文:

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

Instead you can use:

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:

确定