无法使图像在jpanel上消失

时间:2018-12-06 23:18:51

标签: java swing

我正在努力解决这个问题。我想通过按一个按钮并在按钮侦听器中设置一个标志来使面板上的图像消失。关键的布尔变量是showImage,我希望它将防止加载图像。看来我无法从Butoon侦听器创建新图像。     包com.pumpfundamentals.calc;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.DecimalFormat;

import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import com.pumpfundamentals.calc.CalcVel;



public class Calc extends JFrame{

    private static final long serialVersionUID = -6408828418879466758L;
    //private static final String imageURL = null;
    private static JFrame frame;
    private JPanel panel, panel3;
    private JButton buttonCalc, buttonView;
    private JLabel labelDia, labelFlow, labelVel, labelVelRes, labelRe, labelReRes;
    private JLabel labelFrict_par, labelFrict_parRes, labelFrict_fact, labelFrict_factRes;
    private JTextField diaInput, flowInput;

    public double v, dia, fl, re, f_par, fr_100, f_fact, eps=0.00015, v_round;
    public  boolean showImage = false;

    public Image vel_formula = null;

    private static BufferedImage image = null;
    private JLabel labelImage;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new Calc("img/vel_formula.png");
        }
        });
    }

    public Calc(String fileName) {

        this.setSize(1000, 800);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setTitle("Pipe friction calculations");


        //frame = new JFrame();
        //panel = new JPanel();
        JPanel btnPanel  = new JPanel(new FlowLayout());

        //Box theBox = Box.createHorizontalBox();

        //panel3 = new JPanel();
        //panel.setLayout(new BorderLayout());

        //frame.add(panel);
        buttonView = new JButton("VIEW");
        buttonCalc = new JButton("CALC");
        labelDia = new JLabel("  Dia.  ");
        diaInput = new JTextField(5);
        diaInput.setText("0");
        labelFlow = new JLabel("  flow  ");
        flowInput = new JTextField(5);
        flowInput.setText("0");
        labelVel = new JLabel("  velocity (ft/s)  ");
        labelVelRes = new JLabel("___");
        labelVelRes.setForeground(Color.red);
        labelRe = new JLabel("  Re no.  ");
        labelReRes = new JLabel("___");
        labelReRes.setForeground(Color.red);
        labelFrict_par = new JLabel("  frict.param.  ");
        labelFrict_parRes = new JLabel("___");
        labelFrict_parRes.setForeground(Color.red);

        labelFrict_fact = new JLabel("  frict. fact. (ft/100 ft)  ");
        labelFrict_factRes = new JLabel("___");
        labelFrict_factRes.setForeground(Color.red);

        btnPanel.add(buttonView);
        btnPanel.add(buttonCalc);
        btnPanel.add(labelDia);
        btnPanel.add(diaInput);
        btnPanel.add(labelFlow);
        btnPanel.add(flowInput);
        btnPanel.add(labelVel);
        btnPanel.add(labelVelRes);
        btnPanel.add(labelRe);
        btnPanel.add(labelReRes);
        btnPanel.add(labelFrict_par);
        btnPanel.add(labelFrict_parRes);
        btnPanel.add(labelFrict_fact);
        btnPanel.add(labelFrict_factRes);

        //buttonPanel.add(theBox);
        //this.add(buttonPanel, BorderLayout.NORTH);


        //this.add(new ImageArea(fileName), BorderLayout.CENTER);

        ImageArea image = new ImageArea(fileName);

        Container cp = getContentPane();
        cp.setLayout(new BorderLayout());
        cp.add(image, BorderLayout.CENTER);
        cp.add(btnPanel, BorderLayout.NORTH);

        ListenForButton lForButton = new ListenForButton();
        buttonCalc.addActionListener(lForButton);
        buttonView.addActionListener(lForButton);
        //this.pack();
        this.setVisible(true);
    }

    @SuppressWarnings("serial")
    private class ImageArea extends JPanel{

        public ImageArea(String fileName) {

            try {
                image = ImageIO.read(new File(fileName));
                //System.out.println(showImage + " 3");
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }

        public void paint(Graphics g) {
            //boolean show = false;
            Graphics2D g2 = (Graphics2D)g;
            System.out.println(showImage + " 1");
            if(showImage) g2.drawImage(image, 0, 10, 577, 743, null);
            /*
            if(!showImage) {
            g2.setColor(Color.BLACK);
            g2.fillRect(0, 10, 800, 440);
            }


            */
        }

    }

    private class ListenForButton implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == buttonCalc) {
                CalcVel vel = new CalcVel();
                v = vel.CalcVel(diaInput.getText(), flowInput.getText());
                labelVelRes.setText("  " + v);
                CalcRe rey = new CalcRe();
                re = rey.CalcRe(v, diaInput.getText());
                DecimalFormat df = new DecimalFormat("0.000E0");
                String reString = df.format(re).toLowerCase();
                labelReRes.setText("  " + reString);
                CalcFrict_par f_param = new CalcFrict_par();
                f_par = f_param.CalcFrict_par(eps, diaInput.getText(), re);
                labelFrict_parRes.setText("  " + f_par);
                CalcFrictFact f_fac = new CalcFrictFact();
                f_fact = f_fac.CalcFrictFact(f_par, v, diaInput.getText());
                labelFrict_factRes.setText("  " + f_fact);

            }
            else if(e.getSource() == buttonView) {
                //System.out.println("img/vel_formula.png");
                showImage = true;

                System.out.println(showImage + " 2");
                ImageArea image = new ImageArea("img/vel_formula.png");


            }
        }

    }



}

2 个答案:

答案 0 :(得分:1)

说实话,您的代码是一团糟。我运行了您的代码,没有图像显示出如此大的变化。无需创建嵌套的ImageArea类,只需将图像放入Label中即可。

 labelImage = new JLabel();
 labelImage.setIcon(new ImageIcon(fileName));
 this.add(labelImage); // not sure in what panel you add the image

稍后按下按钮时,您将执行以下操作:

else if (e.getSource() == buttonView)
    {
        // inverting the boolean
        showImage = !showImage;
        // setting the image visible or not
        labelImage.setVisible(showImage);
    }
}

所以您不需要重涂任何东西。

如果您提供完整的代码(包括CalcVel类)并确保显示了图像,那么我可以尝试为您提供更具体的帮助。

答案 1 :(得分:1)

在JPanel的子类中,您不应覆盖paint()方法。 您应该改写paintComponent()方法,并且其中的第一条语句应该是对super.paintComponent()的调用。