Insert a GIF image into JFrame and make it visible and invisible on demand

感情迁移 提交于 2019-12-14 03:29:05

问题


In my application I am extracting data from a project DB. When I do that I want to signal to user that data extraction is in progress. I want to achieve this by making my current frame view.getFrame2() invisible and making view.getFrame3() visible. Frame 3 will have GIF image with frame titled as "Extraction is in progress". I am able to achieve my target but I am not able to view the image in the frame. It's blank.

Below is the code snippet I am using; one class is used for view, another one for controller and yet another one for the model. I also have a main class to initialize all the M-V-C classes. I don't want to complicate the code by creating more classes.

View

My View class:-
/** View**/
package mvc.views;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import javax.swing.*;

public class View {
 JFrame frame2, frame3;
  JPanel  panel3;
  Toolkit tk; 
  Image   icon;
  Color   background;
  public View() {
 /** processing image view **/

    frame3 =new JFrame();

    frame3.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame3.setTitle( "Extraction in process...." );

      try {
            icon = new ImageIcon("C:\\Users\\pc033k\\Desktop\\gears_animated.gif").getImage();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 


    panel3 = new JPanel();
    background = Color.white;
    panel3.setOpaque(true);
    frame3.setContentPane(panel3);;            
    frame3.setBounds(100, 100, 400, 400);
    frame3.setLocationRelativeTo(null);
    frame3.setVisible(false);
    /** End of processing image view **/ 
}
public void paint (Graphics g)
  {
    panel3.paint(g);

    panel3.setBackground(background);
    g.drawImage(icon,100,100,500,500,null);

    }
 } 
 /** End of View**/

Controller

/** Start Controller**/
package mvc.controllers;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import javax.swing.JTextField;

import mvc.models.*;
import mvc.views.*;
public class Controller {
 public Model model;
 public View view;
 public ActionListener myButtonListener;
 //public MyDateListener listener;
 boolean status, process1;
 public String user, password, FN, LN, type;

 JTextField text1, text2;
 JCalendarCombo cal1, cal2;


 public Date date1, date2;

 public Controller(Model model, View view) {

    this.model = model;
    this.view = view;
}

    public class MyButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            if(e.getSource() == view.getGetcrmdataButton()){

                FN = view.getUserText1().getText();
                LN = view.getUserText2().getText();
                date1 = view.getCalendar2().getDate();
                date2 = view.getCalendar3().getDate();
                type = (String) view.getComb1().getSelectedItem();
                view.getFrame2().dispose();
                view.getFrame3().setVisible(true);
                view.getFrame3().repaint();



                try {
                     process1 = model.CRMDataExtract(FN, LN, date1, date2, type);

                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                if(process1 == true){

                    view.getFrame3().setVisible(false);
                    view.getFrame2().setVisible(true);
                    JOptionPane.showMessageDialog((Component) (e.getSource()),
                            "pealse Check the output file for the data");



                }
                else
                {
                    view.getFrame3().setVisible(false);
                    view.getFrame2().setVisible(true);
                    JOptionPane.showMessageDialog((Component) (e.getSource()),
                            " No Records found or Data Extraction failed!!");
                }



            }


    }

}

回答1:


I want to achieve this by making my current frame view.getFrame2() invisible and make a view.getFrame3() visible.

Don't be hiding/showing different windows. Users don't like frames disappearing. Instead you can simply display a modal JDialog containing your image on top of your main frame.

Or another approach is to use a GlassPane that paints over top of your main frame. See Disabled Glass Pane for an example of this approach. The existing example doesn't use an Icon, but you can easily change that.




回答2:


You can use a label for insert the image. Also I think CardLayout more suitable to your work. Here is some sample code. Consider that the Sample is a JFrame. You will see the Panel changes as you press the "Next" button.

 public Sample() {

        p = new JPanel();
        p.setLayout(new CardLayout());

        p1 = new JPanel();
        p1.setLayout(new BorderLayout());
        p1.add(new JLabel(new ImageIcon(new URL("http://icons.iconarchive.com/icons/chrisbanks2/cold-fusion-hd/128/ace-of-spades-icon.png"))));

        p2 = new JPanel();
        p2.setLayout(new BorderLayout());
        p2.add(new JLabel(new ImageIcon(new URL("http://icons.iconarchive.com/icons/chrisbanks2/cold-fusion-hd/128/ace-of-hearts-icon.png"))));

        p3 = new JPanel();
        p3.setLayout(new BorderLayout());
        p3.add(new JLabel(new ImageIcon(new URL("http://icons.iconarchive.com/icons/chrisbanks2/cold-fusion-hd/128/angrybirds-2-icon.png"))));

        p.add(p1, "0");
        p.add(p2, "1");
        p.add(p3, "2");

        b = new Button("Next");
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                rotator++;
                if (rotator == 3) {
                    rotator = 0;
                }
                CardLayout layout = (CardLayout) p.getLayout();
                layout.show(p, "" + rotator);
                System.out.println("Rotator " + rotator);

            }
        });

        add(p, "Center");
        add(b, "North");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


来源:https://stackoverflow.com/questions/27580177/insert-a-gif-image-into-jframe-and-make-it-visible-and-invisible-on-demand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!