[英]Unwanted JPanel border in JFrame
I am trying to fill JFrame with a JPanel and colour that JPanel, but for some reason I get a border on the bottom right edges. 我试图用一个JPanel填充JFrame并为该JPanel着色,但是由于某种原因,我在右下角有一个边框。
My code: 我的代码:
public class Main extends JFrame {
public MyPanel myPanel = new MyPanel();
public static void main(String args[])
{
Main main = new Main();
main.init();
}
public void init()
{
this.setSize(300,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(myPanel);
this.setVisible(true);
}
@Override
public void paint(Graphics g)
{
myPanel.paintComponents(g);
}
class MyPanel extends JPanel
{
@Override
public void paintComponents(Graphics g)
{
g.fillRect(0, 0, getWidth(), getHeight());
}
}
}
Shows: 显示:
You're short-circuiting the paint chain with this method 您使用这种方法使油漆链短路
@Override
public void paint(Graphics g) {
myPanel.paintComponents(g);
}
This isnt really doing anything useful so you can remove it. 这实际上并没有做任何有用的事情,因此您可以将其删除。 Also you need to override paintComponent
rather than paintComponents
in MyPanel
另外,您还需要在MyPanel
重写paintComponent
而不是paintComponents
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(0, 0, getWidth(), getHeight());
}
Aside: 在旁边:
setBackground
to get this functionality 您可能只需创建一个面板并调用setBackground
即可获得此功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.