这是文档记录:


  使doRun.run()在AWT事件上异步执行
  调度线程。这将在所有未决的AWT事件发生后发生
  已处理。当应用程序线程时应使用此方法
  需要更新GUI。在以下示例中,invokeLater调用
  将Runnable对象doHelloWorld排队在事件调度上
  线程,然后打印一条消息。


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

我总是制作这样的程序:

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(尤其是“初始线程”)。

10-08 01:23