英文:
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);
}
}
以下是程序的输出:
第一次运行程序时:
当我将鼠标悬停在两个按钮上方时的程序效果:
英文:
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:
Program after I hover my mouse over both buttons:
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论