简体   繁体   中英

JPanel - Error changing background color of a JPanel

I have written the following code:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends JFrame{

    public Test() {
        this.setTitle("Test");
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        JPanel jPanel = new JPanel() {
            public void paint(Graphics g) {
                g.setColor(Color.GREEN);
                g.fillOval(100, 100, 100, 100);
                g.setColor(Color.BLACK);
                g.drawLine(0, 0, 100, 100);
            }
        };
        jPanel.setSize(500, 500);
        jPanel.setBackground(Color.RED);
        this.add(jPanel);
        this.setVisible(true);
        this.setSize(500, 500);
    }

    public static void main(String[] args) {
        Test test = new Test();
    }
}

I want to draw a circle, the line, and change the background color of the screen to red, but when I run the program the screen looks as follows: 屏幕

Where is the error in the code?. Tank you

First, don't override paint , but paintComponent .

Then, the javadoc of setBackground says :

Direct subclasses of JComponent must override paintComponent to honor this property.

So calling this method will do nothing by itself in your case, unless you call the paintComponent of the parent class (or paint the background yourself, but that would be overkill).

In any case it is advised to call super.paintComponent , because the parent implementation may perform some cleaning or other useful operations.

Here is a modified version of your code that takes all of this into account :

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends JFrame {

    public Test() {
        this.setTitle("Test");
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(final WindowEvent e) {
                System.exit(0);
            }
        });

        JPanel jPanel = new JPanel() {
            @Override
            public void paintComponent(final Graphics g) {
                // call parent method
                super.paintComponent(g);

                // OR paint background yourself :
                // g.setColor(getBackground());
                // g.fillRect(0, 0, getWidth(), getHeight());


                // paint other things
                g.setColor(Color.GREEN);
                g.fillOval(100, 100, 100, 100);
                g.setColor(Color.BLACK);
                g.drawLine(0, 0, 100, 100);
            }
        };
        jPanel.setSize(500, 500);
        jPanel.setBackground(Color.RED);
        this.add(jPanel);
        this.setVisible(true);
        this.setSize(500, 500);
    }

    public static void main(final String[] args) {
        Test test = new Test();
    }
}

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