简体   繁体   中英

Java Netbeans override paint() method in a JPanel

Hi I'm a newbie programmer
I want to develop a Java program's GUI using Netbeans IDE

Using Netbeans GUI Builder,
First, I create a new JFrame Form
Then, I add a JPanel from the toolbar/palette

Question is,
How can I override the paint() function of the newly created JPanel ?

I want to draw a background and some spheres inside the JPanel ,
I tried using getGraphics() function to paint and draw, it does the job, but it won't draw anymore when I call repaint()

Should I create a new class implementing JPanel or JComponent , with my custom paint() function, instead ?
(If it so, how can I do it with Netbeans GUI Builder?)

Similar Question :

how to use jpanel with paint (or repaint)

(but it doesn't use Netbeans GUI Builder)

The usual way to do this is to create your own JPanel subclass (eg MyJPanel ) and implement the paint() method there.

After you implemented that class, switch to your form, select the panel and then use "Custom creation code" in the the "Code" tab of the panel's properties to create an instance of MyJPanel instead of JPanel

自定义创建代码

This has the slight disadvantage that you will need to cast the instance variable to MyJPanel each time you want to access methods that are defined in MyJPanel but not in JPanel . If you never need to do that, this is the quickest method.

If you want to access additional methods in your panel class (without casting the instance variable each time), it's easier to remove the existing JPanel and add a "Bean" using the new class.

This is done by clicking on the "Choose Bean" button in the palette:

选择豆子

Once you click OK you can place the panel on your form and NetBeans will create an instance variable of the type MyJPanel (instead of JPane) and you can access all methods defined in that class. Note that the class must be compiled before you can add it that way!

Hi I'm a newbie programmer I want to develop a Java program's GUI using Netbeans IDE

Hi! Since you are a newbie programmer, use Netbeans just as a regular text editor. In order to do it, create plain Java classes instead of Forms and do the coding yourself. The reason is that you have to understand Swing first, before having to deal with Netbeans approach.

Should I create a new class implementing JPanel or JComponent, with my custom paint() function, instead ?

You could certainly only extend JPanel or JComponent, since they are not Interfaces.

If you insist on using Netbeans Mattise GUI editor, then create a new JPanel Form and override it's paintComponent method.

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    // do your painting here...
    g.setColor(Color.RED);
    g.fillRect(6, 20, 12, 120);
    g.setColor(Color.WHITE);
    g.drawString("test", 50, 25);
}

Finally just add the component to your JFrame Form's constructor:

    add(new MyNewJPanel());
    pack();

Make sure that your JFrame's ContentPane has an appropriate layout.

You shall not implement paint, if I understand well, because it invoke paintBorder, paintChildren and paintComponent. Use paint if you you want to handle the border and the children, with component, but it is not recommended. Use paintComponent instead.

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