首页 > 解决方案 > 无法使图像在 jpanel 上消失

问题描述

我正在努力解决这个问题。我想通过按下按钮并在按钮侦听器中设置一个标志来使面板上的图像消失。关键的布尔变量是 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");


            }
        }

    }



}

标签: javaswing

解决方案


老实说,你的代码是一团糟。我运行了您的代码,但没有图像显示如此。无需创建嵌套的 ImageArea 类,您只需将图像放入标签中。

 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 类的完整代码并确保图像正在显示,我可以尝试更具体地帮助您。


推荐阅读