简体   繁体   中英

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. 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:

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:

    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.

Several problems:

  • You should never call setBounds(...) within a painting method such as paintComponent . This method is for painting and painting only.
  • Your bounds and your painting region are the same -- but they represent two very different things. The bounds are the component's position relative to its container, the painting x, y are relative to the JComponent itself. So you're painting out of your bounds.
  • So in the same vein, your drawButton method should be drawing at 0, 0: g.drawRect(0, 0, size.width, size.height);
  • setBounds shouldn't be called regardless. That is the layout managers job. By doing this, you add a whole layer of potential and hard to find bugs.
  • I would override getPreferredSize() to help set the component's best size (edit -- which you already do, albeit in a very simple fashion).
  • You should not set the location of the button inside the button -- again, that's the job of its container's layout manager.
  • The super.paintComponent(g) should probably be called first in the paintComponent override.
  • I would avoid giving my class a name that clashes with a common core class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM