JLabel不更新?

时间:2018-10-12 04:42:33

标签: java swing jlabel

我相对较不熟悉编程,因此我试图更新JLabel,但是不幸的是,每次重置文本时,JLabel都不会在JFrame上更新。我做错什么了吗? h.run(); actionlistener中的segment只是在另一个线程中递减整数。然后h.gethealth返回该值。

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;   
import java.io.IOException; 

public class Main {

    JFrame healthcounter = new JFrame();
    JLabel number = new JLabel();

    public void startHealthFrame(int health){
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        System.out.println("health: "+health);
        if(health==500) {
            healthcounter.setLocation((int) dim.getWidth() - 300, 0);
            healthcounter.setUndecorated(true);
            healthcounter.setOpacity(1);
            healthcounter.setSize(300,100);

            number.setFont(new Font("Arial", Font.BOLD,50));
            number.setText("Health: "+Integer.toString(health));

            healthcounter.getContentPane().add(number);
            healthcounter.setVisible(true);
        }else {

            number.setText("Health: "+Integer.toString(health));
        }
    }

    public static void main(String args[]){

        Main m = new Main();
        Health h = new Health();
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        JFrame q1 = new JFrame();
        JFrame q2 = new JFrame();
        JFrame q4 = new JFrame();
        JFrame q3 = new JFrame();

        m.startHealthFrame(500);

        q1.setUndecorated(true);
        q2.setUndecorated(true);
        q3.setUndecorated(true);
        q4.setUndecorated(true);
        JButton button1 = new JButton();
        JButton button2 = new JButton();
        JButton button3 = new JButton();
        JButton button4 = new JButton();
        button1.setBorder(null);
        button2.setBorder(null);
        button3.setBorder(null);
        button4.setBorder(null);
        try {
            Image topleft = ImageIO.read(Main.class.getResource("/resources/upperleft.png"));
            Image topright = ImageIO.read(Main.class.getResource("/resources/upperright.png"));
            Image bottomleft = ImageIO.read(Main.class.getResource("/resources/bottomleft.png"));
            Image bottomright = ImageIO.read(Main.class.getResource("/resources/bottomright.png"));

            button1.setIcon(new ImageIcon(topleft));
            button2.setIcon(new ImageIcon(topright));
            button3.setIcon(new ImageIcon(bottomleft));
            button4.setIcon(new ImageIcon(bottomright));
        } catch (IOException e) {
            e.printStackTrace();
        }

        q1.add(button1);
        q2.add(button2);
        q4.add(button4);
        q3.add(button3);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                h.run();
                Main m = new Main();
                m.startHealthFrame(h.gethealth());
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                h.run();
                Main m = new Main();
                m.startHealthFrame(h.gethealth());
            }
        });
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                h.run();
                Main m = new Main();

                m.startHealthFrame(h.gethealth());
            }
        });
        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                h.run();
                Main m = new Main();

                m.startHealthFrame(h.gethealth());
            }
        });
        q1.setSize(250,250);
        q2.setSize(250,250);
        q3.setSize(250,250);
        q4.setSize(250,250);
        q1.setDefaultCloseOperation(q1.EXIT_ON_CLOSE);
        q2.setDefaultCloseOperation(q2.EXIT_ON_CLOSE);
        q3.setDefaultCloseOperation(q3.EXIT_ON_CLOSE);
        q4.setDefaultCloseOperation(q4.EXIT_ON_CLOSE);

        q1.setLocation((dim.width/2-q1.getSize().width/2)-125, (dim.height/2-q1.getSize().height/2)-125);
        q2.setLocation(q1.getX()+q1.getWidth()-8,q1.getY());
        q3.setLocation(q1.getX(),q1.getY()+q1.getHeight()-8);
        q4.setLocation(q1.getX()+q1.getWidth()-8,q1.getY()+q1.getHeight()-8);

        q1.setVisible(true);
        q2.setVisible(true);
        q4.setVisible(true);
        q3.setVisible(true);

        try
        {
            Clip clip = AudioSystem.getClip();
            clip.open(AudioSystem.getAudioInputStream(Main.class.getResource("/resources/megalovania.wav")));
            clip.start();
            clip.loop(clip.LOOP_CONTINUOUSLY);
        }
        catch (Exception exc)
        {
            exc.printStackTrace(System.out);
        }
        Time time = new Time(q1,q2,q3,q4);
        time.start();
    }
}

0 个答案:

没有答案