我正在为一个多用户绘图程序开发一个项目,但我遇到了我的 GUI 锁定问题。
除了这个错误之外,我已经完成了大部分程序,它是一个相当大的程序,所以我试图在一个较小的程序中重现该错误。
在这个较小的程序中,我有 2 个 JFrame。两者都可以通过单击和拖动鼠标来绘制。辅助 JFrame 是一个休眠 10 秒的线程,然后将您绘制的内容发送到另一个要显示的帧。
但是,一旦主框架接收到来自辅助框架的图像,GUI 就会锁定,并且无法再绘制主框架。
我目前正在使用 SwingUtilities.invokeLater() 方法。在寻找答案时,我找到了 SwingWorker 类,但我想看看是否有一个简单的解决方案,然后再对我的代码进行大量重写以尝试使其与 SwingWorker 一起使用。
谢谢阅读。我的代码如下。另外,这是我第一次在这里发帖。我似乎在格式化代码时遇到了一些问题,所以如果出现错误,我提前道歉。我会尽力修复它。
package gui_thread_test;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JComponent;
public class DrawPanel extends JComponent
{
private Image canvas;
private int x, y, prevX, prevY;
Graphics2D g2;
public DrawPanel()
{
setDoubleBuffered(false);
addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent e )
{
prevX = e.getX();
prevY = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter()
{
@Override
public void mouseDragged(MouseEvent e)
{
x = e.getX();
y = e.getY();
g2.drawLine(prevX, prevY, x, y);
repaint();
prevX = x;
prevY = y;
}
});
}
@Override
public void paintComponent(Graphics g)
{
if (canvas == null)
{
canvas = createImage(getSize().width, getSize().height);
g2 = (Graphics2D) canvas.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
g.drawImage(canvas, 0, 0, null);
}
public synchronized void updateCanvas(Image _canvas)
{
canvas = _canvas;
repaint();
}
public Image getImage()
{
return canvas;
}
}
-
package gui_thread_test;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class ThreadTest extends JFrame implements Runnable
{
private Image canvas;
private Graphics2D g2;
private DrawPanel panel;
private DrawPanel threadPanel;
public ThreadTest(DrawPanel _panel)
{
super("Secondary");
panel = _panel;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(500,500));
this.setMinimumSize(new Dimension(500,500));
this.setVisible(true);
threadPanel = new DrawPanel();
this.add(threadPanel);
}
public void paintComponent(Graphics g)
{
if (canvas == null)
{
canvas = createImage(getSize().width, getSize().height);
g2 = (Graphics2D) canvas.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
g.drawImage(canvas, 0, 0, null);
}
@Override
public void run() {
//Sleep thread for 10 seconds to give time to draw on the image
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Logger.getLogger(ThreadTest.class.getName()).log(Level.SEVERE, null, ex);
}
SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
//Posts the message to the server chat window
panel.updateCanvas(threadPanel.getImage());
}
});
}
}
-
package gui_thread_test;
import java.awt.Dimension;
import javax.swing.JFrame;
public class GUI_Thread_Test {
public static void main(String[] args)
{
JFrame window = new JFrame("Main");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setPreferredSize(new Dimension(500,500));
window.setMinimumSize(new Dimension(500,500));
DrawPanel draw = new DrawPanel();
window.add(draw);
Thread testThread = new Thread(new ThreadTest(draw));
testThread.start();
window.pack();
window.setVisible(true);
}
}