将GIF图像插入JFrame并根据需要使其可见和不可见 [英] Insert a GIF image into JFrame and make it visible and invisible on demand

查看:343
本文介绍了将GIF图像插入JFrame并根据需要使其可见和不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我正在从项目DB中提取数据.当我这样做时,我想通知用户数据提取正在进行中.我想通过使当前帧view.getFrame2()不可见并使view.getFrame3()可见来实现此目的.第3帧将具有GIF图像,其标题为提取正在进行中".我可以实现目标,但是无法查看框架中的图像.它是空白.

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.

下面是我正在使用的代码段;一类用于视图,另一类用于控制器,另一类用于模型.我也有一个主类来初始化所有M-V-C类.我不想通过创建更多的类来使代码复杂化.

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.

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**/

控制器

/** 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!!");
                }



            }


    }

}

推荐答案

我想通过使当前帧view.getFrame2()不可见并使view.getFrame3()可见来实现这一点.

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

不要隐藏/显示其他窗口.用户不喜欢框架消失.相反,您可以简单地在主框架顶部显示一个包含图像的模式JDialog.

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.

或者另一种方法是使用GlassPane在主框架的顶部进行绘制.有关此方法的示例,请参见禁用玻璃窗格.现有示例未使用图标,但是您可以轻松地对其进行更改.

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.

这篇关于将GIF图像插入JFrame并根据需要使其可见和不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆