如何在JFrame中刷新JLabel

时间:2018-09-25 19:44:13

标签: jframe jlabel repaint

我如何刷新JFrame中的JLabel以更新百分比,这是下面的代码

public void start1(){

    URL resource = getClass().getResource("Graphitebackground.v2.jpg");


    ImageIcon i2a = new ImageIcon(resource);
    URL resource3 = getClass().getResource("Graphitebackground.v4.jpg");
    JFrame TFrame = new JFrame();
    TFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    TFrame.setVisible(true);
    int JW = TFrame.getWidth();
    int JH = TFrame.getHeight();
    TFrame.dispose();


    ImageIcon i3a = new ImageIcon(resource3);
    URL resource1 = getClass().getResource("Graphitebackground.v3.jpg");
    ImageIcon i1a = new ImageIcon(resource1);  
    Image image = i2a.getImage();
    Image newimg = image.getScaledInstance(JW, JH,  java.awt.Image.SCALE_SMOOTH);
    i2a = new ImageIcon(newimg);
    URL resource4 = getClass().getResource("Loading1.gif");
    ImageIcon i4a = new ImageIcon(resource4); 
    Image image1 = i4a.getImage();
    int JW2 = JW*40;
    JW2 = JW2/100;
    int JH2 = JH*40;
    JH2 = JH2/100;
    int diffrence = JW2-JH2;
    JW2= JW2-diffrence;
   Image newimg1 = image1.getScaledInstance(JW2, JH2, java.awt.Image.SCALE_DEFAULT);
   i4a = new ImageIcon(newimg1);
   final ImageIcon i6a = i2a;
   final ImageIcon i5a = i4a;
   try {
                FileOutputStream saveFile = new FileOutputStream("Loader.sav");
                ObjectOutputStream save = new ObjectOutputStream(saveFile);
                save.writeObject(i5a);



                save.close();
            } catch (Exception exc) {
                exc.printStackTrace();
            }
    Thread t3 = new Thread(new Runnable() {
                public void run() {
    try{
    FileInputStream saveFile = new FileInputStream("Done.sav");
    ObjectInputStream save = new ObjectInputStream(saveFile);
    done = (int) save.readObject();





    save.close();
    }
    catch(Exception exc){
    exc.printStackTrace();
    }

   JLabel loading = new JLabel(i5a);

    int Percentage = 90;
    System.out.println(done);
    Percentage =done*100;
    Percentage = Percentage/total;
    System.out.println(Percentage);
    loading.setVerticalTextPosition(JButton.CENTER);
    loading.setHorizontalTextPosition(JButton.CENTER);
    loading.setForeground(Color.red);
    loading.setFont(new Font("Arial", Font.BOLD,50));
    String text="Loading "+Percentage+"%";
    loading.setText(text);


    JPanel panel = new JPanel();
    JLabel p = new JLabel(i6a);
     p.setLayout(new GridBagLayout());








    p.add(loading);
    panel.add(p);

    JFrame f1 = new JFrame();
    f1.add(panel);
    f1.setExtendedState(JFrame.MAXIMIZED_BOTH);
    f1.setVisible(true);
    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    try {

        TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Frametemplate.class.getName()).log(Level.SEVERE, null, ex);
        }

    if (done>=total){
        try {

        TimeUnit.MILLISECONDS.sleep(3000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Frametemplate.class.getName()).log(Level.SEVERE, null, ex);
        }


        new MainMenu().Start1();
        try {

        TimeUnit.MILLISECONDS.sleep(3000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Frametemplate.class.getName()).log(Level.SEVERE, null, ex);
        }
        f1.dispose();


    }
    else{
        f1.dispose();
        this.run();



    }
                }
    });
    t3.start();









}

基本上,代码会获取图像来调整图像的大小以适合屏幕尺寸,但是与其关闭旧框架并制作新框架,我还需要帮助来刷新JLabel,我说了其他问题,这些问题只不过是设置文本,但这是行不通的我尝试绘制它,但是很难将gif设置为可以运行并且它位于正确的位置,并且它仍然没有以任何方式重新绘制,因此请任何人帮助。谢谢

0 个答案:

没有答案