gpt4 book ai didi

java - JFrame 不重绘

转载 作者:行者123 更新时间:2023-12-02 06:32:55 25 4
gpt4 key购买 nike

我已经尝试了至少一个小时来刷新我的简单 Jframe。我尝试过 repaint() revalidate; 以及互联网上的其他任何内容。

这是我的整个类(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();


}
}

我做错了什么?

最佳答案

有两件事出了问题。您的第一个线程不断地检测事件队列的更新请求,速度可能比事件队列处理它们的速度要快,这最终可能会淹没它,从而降低系统的性能。

其次,您实际上从未更改 textLabel 的文本

例如...

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();

}
}

现在,您可以考虑使用 SwingWorker 而不是 Thread...

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();

}
}

关于java - JFrame 不重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19917668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com