我想用2创建一个程序JPanel使用BorderLayout.中间面板用于随机绘制矩形,而南面板用于按钮.
JFrame每当我将鼠标光标悬停在北或南按钮上时,我就会看到左上角按钮的奇怪图像.我做了一些研究,发现这可能是拥有透明背景的原因.我尝试使用super.paintComponent(g)面板,但之前绘制的其余矩形消失了.我需要将矩形留在JPanel左上角而不是奇怪的图像中.
我不知道我做错了什么,希望有人可以帮助或提供一些如何解决这个问题的线索.
public class TwoBRandomRec extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
TwoBRandomRec rec = new TwoBRandomRec();
rec.setSize(500,500);
rec.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rec.setVisible(true);
}
public TwoBRandomRec() {
//Create the buttons
JButton north = new JButton("North");
JButton south = new JButton("South");
DrawPanel drawPanel = new DrawPanel(500,500);
JPanel southP = new JPanel();
southP.add(south);
southP.add(north);
this.add(drawPanel, BorderLayout.CENTER);
this.add(southP, BorderLayout.SOUTH);
this.setTitle("TwoButtonRandomRec");
this.pack();
}
public class DrawPanel extends JPanel { …Run Code Online (Sandbox Code Playgroud)