Java任务没有更新jlabel

时间:2016-10-21 11:07:44

标签: java

我的问题是我正在尝试使用计时器更新我的gui(这可以改变mypic的图像但是,它不会更新mytext标签因为某些奇怪的原因,任何帮助都会非常感激!

*我应该补充说,自从引入计时器后,mytext根本没有显示在我的gui上......但mypic确实????

package widget;

import com.sun.awt.AWTUtilities;
import com.sun.xml.internal.ws.client.sei.ResponseBuilder;
import javafx.geometry.HorizontalDirection;
import javafx.scene.shape.Ellipse;

import javax.swing.*;
import javax.swing.Timer;
import javax.xml.parsers.ParserConfigurationException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.xml.sax.SAXException;
import widget.weather;

import static java.awt.Color.*;

/**
 * Created by xxxxxxzz on 10/19/2016.
 */
public class Widget extends JFrame {

    String icon_image = null;
    String temp = null;
    JLabel myText = null;
    JLabel mypic = null;


    Timer SimpleTimer = new Timer(5000, new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                icon_image = weather.weather_pic();
                temp = weather.temp();
                URL url = new URL(icon_image);
                ImageIcon img = new ImageIcon(url);

                myText = new JLabel(temp);
                //Tried setting it like this and still doesn't work
                // myText = new JLabel("HOT");

                mypic = new JLabel();

                myText.setText(temp);
                mypic.setIcon(img);



            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }
    });

    public Widget() throws IOException, URISyntaxException {
        setUndecorated(true);

        setSize(150,150);


        temp = weather.temp();
        icon_image = weather.weather_pic();



        URL url = new URL(icon_image);



        ImageIcon img = new ImageIcon(url);
        myText = new JLabel(temp);
        mypic = new JLabel();

        myText.setText(temp);
        mypic.setIcon(img);

        myText.setHorizontalAlignment(JLabel.CENTER);


        mypic.setHorizontalAlignment(JLabel.CENTER);


        add(myText);
        add(mypic);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);

        Shape shape = new Ellipse2D.Float(0,0,150,150);
        AWTUtilities.setWindowShape(this, shape);
        SimpleTimer.start();
    }





    public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
        new Widget();
    }


}

更新建议仍然无效..

package widget;

import com.sun.awt.AWTUtilities;
import com.sun.xml.internal.ws.client.sei.ResponseBuilder;
import javafx.geometry.HorizontalDirection;
import javafx.scene.shape.Ellipse;

import javax.swing.*;
import javax.swing.Timer;
import javax.xml.parsers.ParserConfigurationException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.xml.sax.SAXException;
import widget.weather;

import static java.awt.Color.*;

/**
 * Created by jsnow on 10/19/2016.
 */
public class Widget extends JFrame {

    String icon_image = null;
    String temp = null;
    JLabel myText = new JLabel();
    JLabel mypic = new JLabel();


    Timer SimpleTimer = new Timer(5000, new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                icon_image = weather.weather_pic();
                temp = weather.temp();
                URL url = new URL(icon_image);
                ImageIcon img = new ImageIcon(url);


                myText.setText(temp);
                mypic.setIcon(img);



            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }
    });

    public Widget() throws IOException, URISyntaxException {
        setUndecorated(true);

        setSize(150,150);


        temp = weather.temp();
        icon_image = weather.weather_pic();



        URL url = new URL(icon_image);



        ImageIcon img = new ImageIcon(url);


        myText.setText(temp);
        mypic.setIcon(img);

        myText.setHorizontalAlignment(JLabel.CENTER);


        mypic.setHorizontalAlignment(JLabel.CENTER);


        add(myText);
        add(mypic);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);

        Shape shape = new Ellipse2D.Float(0,0,150,150);
        AWTUtilities.setWindowShape(this, shape);
        SimpleTimer.start();
    }





    public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
        new Widget();
    }


}

1 个答案:

答案 0 :(得分:0)

这是一个可以根据您的需要量身定制的工作解决方案。原因似乎是您试图显示组件而不是动作监听器中的代码。

当您最初设置myText时,您的方法只是将myPic替换为JFrame。相反,您需要使用布局管理器。我给出的例子是使用JLayeredPane覆盖。如果这不是预期的结果,您可能希望使用布局管理器,请参阅使用布局管理器的Oracle Tutorial

小工具类

package widget;

import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Widget extends JFrame {

    private static final int UPDATE_TICK = 5_000;
    private static final int WIDGET_WIDTH = 150;
    private static final int WIDGET_HEIGHT = 150;

    private JLabel myText = new JLabel();
    private JLabel myPic = new JLabel();
    private Weather weather = new Weather();
    private Timer simpleTimer;

    public void createAndShow() {
        setUndecorated(true);
        setSize(WIDGET_WIDTH, WIDGET_HEIGHT);
        Shape shape = new Ellipse2D.Float(0, 0, WIDGET_WIDTH, WIDGET_HEIGHT);
        // AWTUtilities.setWindowShape(this, shape);
        setShape(shape); // do this instead


        myText.setHorizontalAlignment(JLabel.CENTER);
        myPic.setHorizontalAlignment(JLabel.CENTER);

        JLayeredPane layered = new JLayeredPane();
        myText.setBounds(0, 0, WIDGET_WIDTH, WIDGET_HEIGHT);
        myPic.setBounds(0, 0, WIDGET_WIDTH, WIDGET_HEIGHT);
        layered.add(myPic, 1, 0);
        layered.add(myText, 2, 0);
        add(layered);

        simpleTimer = new Timer(UPDATE_TICK, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                update();
            }
        });

        update();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        simpleTimer.start();
    }

    private void update() {        
        myText.setText(weather.getTemp());
        myPic.setIcon(new ImageIcon(weather.getImage(WIDGET_WIDTH, WIDGET_HEIGHT)));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Widget w = new Widget();
                w.createAndShow();;
            }
        });
    }

}

用于演示目的的气候类

package widget;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.Random;

public class Weather {

    private static final Random rand = new Random();
    private static final Color[] randColors = {Color.YELLOW,
        Color.GREEN,
        Color.WHITE};

    public String getTemp() {
        return Integer.toString(rand.nextInt(100));
    }

    public BufferedImage getImage(int width, int height) {
        int colorIndex = rand.nextInt(randColors.length);
        BufferedImage img = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < img.getWidth(); x++) {
            for (int y = 0; y < img.getHeight(); y++) {
                img.setRGB(x, y, randColors[colorIndex].getRGB());
            }
        }
        return img;
    }
}