为什么在paintComponent(Graphics g)方法中使用if语句会使方法中的所有代码无效?

问题描述 投票:0回答:1

我正在尝试创建一个包含JPanel对象的JFrame对象。在JPanel对象内部,有3个JButton,单击这些按钮可更改JPanel的背景颜色。

我还想绘制一个大小等于JPanel对象的图像,以给人以背景图像的印象,但是正如您可能想像的那样,当用户没有单击任何对象时,我只会第一次淹没它。按钮呢。单击按钮后,我打算调用从Component类继承的repaint()方法,据我所知,该方法应该调用paintComponent(Graphics g)。

鉴于我希望仅当用户未单击任何按钮时在paintComponent(Graphics g)内绘制图像的事实,我试图使用if语句,以便在调用paintComponent(Graphics g)方法时第二次通过repaint()方法,它将在else语句内执行,并简单地调用super.paintComponent(Graphics g)方法,据我所知,该方法应该在没有图像的情况下进行绘制。问题是,一旦将if语句放入paintComponent方法中,它似乎会使该方法中的整个代码无效。

关于这种情况发生的任何建议或解释,将不胜感激。

代码如下:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;   
import javax.swing.*;

public class PruebaEventosSelf {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MarcoBotonSelf marco=new MarcoBotonSelf();
        marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


class MarcoBotonSelf extends JFrame{

    public MarcoBotonSelf() {
        setExtendedState(MarcoBotonSelf.MAXIMIZED_BOTH);
        setTitle("National Aeronautics and Space Administration NASA");
        Image image=Toolkit.getDefaultToolkit().getImage("C:\\Users\\wagne\\OneDrive\\Desktop\\Nasa.png");
        setIconImage(image);
        LaminaBoton lamina=new LaminaBoton();
        add(lamina);
        setVisible(true);
    }
}

class LaminaBoton extends JPanel implements ActionListener {

    JButton botonAzul=new JButton("Blue");
    JButton botonNegro=new JButton("Black");
    JButton botonGris=new JButton("Gris");
    boolean repaint=false;

    public LaminaBoton() {
        botonAzul.addActionListener(this);
        add(botonAzul, Container.CENTER_ALIGNMENT);
        botonNegro.addActionListener(this);
        add(botonNegro, Container.LEFT_ALIGNMENT);
        botonGris.addActionListener(this);
        add(botonGris, Container.CENTER_ALIGNMENT);
    }

    public void paintComponent(Graphics g) {
        if(repaint) {
            super.paintComponent(g);
        }else {
            Image imagen=Toolkit.getDefaultToolkit().getImage("C:\\Users\\wagne\\OneDrive\\Desktop\\NASA.jpg");
            g.drawImage(imagen, 0, 0, this);
        }
    }

    public void actionPerformed(ActionEvent e) {
        Object pulsado=e.getSource();
        if (pulsado==botonAzul){
            repaint=true;
            repaint();
            this.setBackground(Color.blue);
            System.out.println("Blue is working!");
        }else if(pulsado==botonNegro) {
            System.out.println("Black is working!");
            setBackground(Color.BLACK);
        }else {
            System.out.println("Gray is working!");
            setBackground(Color.DARK_GRAY);
        }
    }

}

我尝试过的另一种方式:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;  
import javax.swing.*;

public class PruebaEventosSelf {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MarcoBotonSelf marco=new MarcoBotonSelf();
        marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


class MarcoBotonSelf extends JFrame{

    public MarcoBotonSelf() {
        setExtendedState(MarcoBotonSelf.MAXIMIZED_BOTH);
        setTitle("National Aeronautics and Space Administration NASA");
        Image image=Toolkit.getDefaultToolkit().getImage("C:\\Users\\wagne\\OneDrive\\Desktop\\Nasa.png");
        setIconImage(image);
        LaminaBoton lamina=new LaminaBoton();
        add(lamina);
        setVisible(true);
    }
}

class LaminaBoton extends JPanel implements ActionListener {

    JButton botonAzul=new JButton("Blue");
    JButton botonNegro=new JButton("Black");
    JButton botonGris=new JButton("Gris");
    boolean repaint=false;

    public LaminaBoton() {
        botonAzul.addActionListener(this);
        add(botonAzul, Container.CENTER_ALIGNMENT);
        botonNegro.addActionListener(this);
        add(botonNegro, Container.LEFT_ALIGNMENT);
        botonGris.addActionListener(this);
        add(botonGris, Container.CENTER_ALIGNMENT);
    }

    public void paintComponent(Graphics g) {
        Image imagen=Toolkit.getDefaultToolkit().getImage("C:\\Users\\wagne\\OneDrive\\Desktop\\NASA.jpg");
        g.drawImage(imagen, 0, 0, this);
        if (repaint) super.paintComponent(g);
    }

    public void actionPerformed(ActionEvent e) {
        Object pulsado=e.getSource();
        if (pulsado==botonAzul){
            repaint=true;
            repaint();
            this.setBackground(Color.blue);
            System.out.println("Blue is working!");
        }else if(pulsado==botonNegro) {
            System.out.println("Black is working!");
            setBackground(Color.BLACK);
        }else {
            System.out.println("Gray is working!");
            setBackground(Color.DARK_GRAY);
        }
    }

}

我尝试了另外4种不同的方式,但是即使使用时没有单击任何按钮,它们也似乎都导致相同的结果,即图像不会被淹死。

java user-interface if-statement paintcomponent repaint
1个回答
0
投票

您的paintComponent()方法应始终将super.paintCompnent(g);调用为方法中的第一条语句。然后,仅当repaint变量为false时,才应绘制图像。

[调用该变量paintImage并将其初始设置为true,然后将按钮侦听器将其设置为false,然后仅在paintImage为true时,paintComponent()方法才会绘制图像。

© www.soinside.com 2019 - 2024. All rights reserved.