java swing 按钮不显示
发布网友
发布时间:2022-05-16 01:46
我来回答
共2个回答
热心网友
时间:2023-10-08 20:37
你重写的BufferedImage区域会覆盖掉button。(button和BufferedImage区域重叠)g.drawImage(bi, 0,0, this); 改成g.drawImage(bi, 0,130, this); 差不多就可以出现,重写的是多的30 是JFrame的标题栏宽度。追问怎样才能让按钮覆盖在BufferedImage上呢
热心网友
时间:2023-10-08 20:37
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class Test extends JFrame
{
public Test()
{
this.setLocation(0, 0);
this.setSize(700, 700);
this.setLayout(null);
JPanel p1 = new JPanel();
p1.setLayout(null);
p1.setBounds(0, 0, 700, 700);
JButton b1 = new JButton("b1");
b1.setBounds(0, 0, 100, 100);
p1.add(b1);
this.add(p1);
this.setVisible(true);
}
public void paint( Graphics g )
{
super.paint(g);
BufferedImage bi =
new BufferedImage(700, 700, BufferedImage.TYPE_INT_RGB);
Graphics g2 = bi.createGraphics();
g2.setColor(Color.WHITE);
g2.fillOval(150, 150, 100, 100);
g.drawImage(bi, 0, 0, this);
}
/**
* @param args
*/
public static void main( String[] args )
{
Test test = new Test();
}
}