简体   繁体   中英

JFrame not repainting

I've been trying for at least an hour on refreshing my simple Jframe. I have tried repaint() revalidate; and just about anything else on the internet.

here is my entire class:

    import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;


public final class BRUTEFORCE {
    static int Passwords = 0;
    static JFrame frame;
    public static void main(String[] args) {
        //Create and set up the window. 
        frame = new JFrame("Simple GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

        JLabel textLabel = new JLabel("Passwords tried: " + Passwords,SwingConstants.CENTER); 
        textLabel.setPreferredSize(new Dimension(300, 100)); 
        frame.getContentPane().add(textLabel, BorderLayout.CENTER); 

        //Display the window. 
        frame.setLocationRelativeTo(null); 
        frame.pack();
        frame.setVisible(true); 
        Passwords++;

        new Thread("Refresh") {
            public void run () {
                while(true){
                    frame.invalidate();
                    frame.validate();
                    frame.repaint();
                }
            }
        }.start();
        new Thread("Test") {
            public void run () {
                while(true) Passwords++;
            }
        }.start();


    }
}

What am I doing wrong?

Two things are going wrong. You're first thread is constantly feeling the Event Queue with update requests, probably faster then the Event Queue can process them which may eventually flood it, degrading the performance of the system.

Secondly, you never actually change the text of the textLabel

For example...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class BruteForce {

    static transient int Passwords = 0;
    static JFrame frame;

    public static void main(String[] args) {
        //Create and set up the window. 
        frame = new JFrame("Simple GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JLabel textLabel = new JLabel("Passwords tried: " + Passwords, SwingConstants.CENTER);
        textLabel.setPreferredSize(new Dimension(300, 100));
        frame.getContentPane().add(textLabel, BorderLayout.CENTER);

        //Display the window. 
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
        Passwords++;

        new Thread("Test") {
            public void run() {
                while (true) {
                    try {
                        Passwords++;
                        SwingUtilities.invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                textLabel.setText("Passwords tried: " + Passwords);
                            }
                        });
                        Thread.sleep(5);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(BruteForce.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }.start();

    }
}

Now, instead of a Thread , you might consider using a SwingWorker instead...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class BruteForce {

    static transient int Passwords = 0;
    static JFrame frame;

    public static void main(String[] args) {
        //Create and set up the window. 
        frame = new JFrame("Simple GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JLabel textLabel = new JLabel("Passwords tried: " + Passwords, SwingConstants.CENTER);
        textLabel.setPreferredSize(new Dimension(300, 100));
        frame.getContentPane().add(textLabel, BorderLayout.CENTER);

        //Display the window. 
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);

        SwingWorker worker = new SwingWorker<Integer, Integer>() {

            @Override
            protected void process(List<Integer> chunks) {

                // Only care about the last one..
                int value = chunks.get(chunks.size() - 1);
                textLabel.setText("Passwords tried: " + value);

            }

            @Override
            protected Integer doInBackground() throws Exception {
                while (true) {
                // Perform long running process...
                    // Forced delay to simulate long running process
                    Thread.sleep(5);
                    Passwords++;
                    publish(Passwords);
                }
            }
        };
        worker.execute();

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM