gpt4 book ai didi

java - 使用和不使用 invokeLater 显示 JFrame 有什么区别?

转载 作者:行者123 更新时间:2023-11-30 07:14:24 25 4
gpt4 key购买 nike

这是文档:

Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread. This will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI. In the following example the invokeLater call queues the Runnable object doHelloWorld on the event dispatching thread and then prints a message.

但我想知道它在代码中的含义

我总是制作这样的程序:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.BufferedImage;

/**
*
* @author Robin
*/

public class Example {

JFrame Frame=new JFrame();


public Example() {

Frame.setTitle("Example");
Frame.setName("Example");
Frame.setSize(300, 300);
Frame.setResizable(false);
Frame.setUndecorated(false);
Frame.setLayout(null);
Frame.setLocationRelativeTo(null);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setIconImage(CrearIcono(Color.decode("#F4141D")).getImage());
Frame.getContentPane().setBackground(Color.WHITE);
Formato();
Accion();
Mover(Frame.getGlassPane());
Frame.setVisible(true);
}


private void Formato() {


}

private void Accion() {


}

public ImageIcon Imagen( String dir){return new ImageIcon(getClass().getResource("/lib/"+dir));}

public static void Mover(final Component objeto) {
MouseInputAdapter d=new MouseInputAdapter() {int x,X,y,Y;
@Override public void mousePressed(MouseEvent e){x=e.getXOnScreen();X=objeto.getLocation().x;y=e.getYOnScreen();Y=objeto.getLocation().y;}
@Override public void mouseDragged(MouseEvent e){objeto.setLocation(X+(e.getXOnScreen()-x), Y+(e.getYOnScreen()-y));}};
objeto.addMouseListener(d);objeto.addMouseMotionListener(d);
}

public int CentrarX(int AnchoObjeto, int AnchoRespectoA){return (AnchoRespectoA/2)-(AnchoObjeto/2);}
public int CentrarY(int LargoObjeto, int LargoRespectoA){return (LargoRespectoA/2)-(LargoObjeto/2);}
public int ImgA(JLabel imagen){return imagen.getIcon().getIconWidth();}
public int ImgL(JLabel imagen){return imagen.getIcon().getIconHeight();}

public static ImageIcon CrearIcono(Color color) {
int WIDTH = 32;
int HEIGHT = 32;
BufferedImage img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
int[] xPoints = {WIDTH, 0, 0, WIDTH / 2};
int[] yPoints = {0, WIDTH / 2, WIDTH, WIDTH};
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(color);
g2.fillPolygon(xPoints, yPoints, xPoints.length);
g2.dispose();

ImageIcon icon = new ImageIcon(img);
return icon;
}

public static void main(String[] args) {

Example Ventana=new Example();
}


}

什么比较好?有什么不同?

SwingUtilities.invokeLater

public static void main(String[] args) {



SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
pantalla principal=new pantalla();
Calendario s=new Calendario(1);
}
});
}

没有 invokeLater

public static void main(String[] args) {

Example Ventana=new Example();
}

谢谢你的建议

最佳答案

第一个使用 invokeLater(..) 更好,因为它是创建 GUI 的正确方法。

参见 Concurrency in Swing (特别是“初始线程”)以获取更多详细信息。

关于java - 使用和不使用 invokeLater 显示 JFrame 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18473440/

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