[英]PaintComponent() being called but JComponent not being painted
Bascially I'm having an issue with painting a custom component I made. 基本上我在绘制我制作的自定义组件时遇到了问题。 Whenever repaint() is called the paintComponent() of my Button class is called but nothing shows up in my frame.
每当调用repaint()时,我的Button类的paintComponent()被调用,但我的框架中没有显示任何内容。 I also know the component is the right size and is in the right location because I set up a border to check this.
我也知道组件的大小合适,并且位于正确的位置,因为我设置了一个边框来检查它。
The following is my custom component class: 以下是我的自定义组件类:
public class Button extends JComponent {
protected static final Color BUTTON_COLOR = Color.black;
protected Point position;
protected Dimension size;
public Button(int posX, int posY, int width, int height) {
super();
position = new Point(posX, posY);
size = new Dimension(width, height);
setBounds(position.x, position.y, size.width, size.height);
setBorder(BorderFactory.createTitledBorder("Test"));
setOpaque(true);
}
@Override
protected void paintComponent(Graphics g) {
setBounds(position.x, position.y, size.width, size.height);
drawButton(g);
super.paintComponent(g);
}
@Override
public Dimension getPreferredSize() {
return size;
}
public void drawButton(Graphics g) {
selectColor(g, BUTTON_COLOR);
g.fillRect(position.x, position.y, size.width, size.height);
g.setColor(Color.black);
g.drawRect(position.x, position.y, size.width, size.height);
}}
This is the JPanel that my custom component is being added to: 这是我的自定义组件添加到的JPanel:
public class MainMenu extends JPanel {
public MainMenu() {
setBackground(Color.BLACK);
setLocation(0,0);
setPreferredSize(new Dimension(800,600));
setDoubleBuffered(true);
setVisible(true);
this.setFocusable(true);
this.requestFocus();
}}
Finally, I add the following components into a MainMenu JPanel: 最后,我将以下组件添加到MainMenu JPanel中:
main_menu.add(new Button(200, 200, 150, 50));
dropdown = new JComboBox<File>() {
@Override
public void paintComponent(Graphics g) {
dropdown.setLocation(new Point(400, 200));
super.paintComponent(g);
}
};
main_menu.add(dropdown);
What's strange is when the repaint() is called on main_menu, the JComboBox is painted but not the Button even though the Button's paintComponent() is called. 奇怪的是当在main_menu上调用repaint()时,即使调用了Button的paintComponent(),JComboBox也会被绘制而不是Button。
Several problems: 几个问题:
setBounds(...)
within a painting method such as paintComponent
. paintComponent
方法(如paintComponent
setBounds(...)
调用setBounds(...)
。 This method is for painting and painting only. drawButton
method should be drawing at 0, 0: g.drawRect(0, 0, size.width, size.height);
drawButton
方法应该绘制为0,0: g.drawRect(0, 0, size.width, size.height);
setBounds
shouldn't be called regardless. setBounds
都不应该调用setBounds
。 That is the layout managers job. getPreferredSize()
to help set the component's best size (edit -- which you already do, albeit in a very simple fashion). getPreferredSize()
来帮助设置组件的最佳大小(编辑 - 你已经做过,虽然以一种非常简单的方式)。 super.paintComponent(g)
should probably be called first in the paintComponent override. super.paintComponent(g)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.