【问题标题】:What is the difference showing JFrame using a invokeLater and without?使用和不使用 invokeLater 显示 JFrame 有什么区别?
【发布时间】:2013-08-30 16:09:12
【问题描述】:

这是文档:

导致 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();
        }

感谢您的建议

【问题讨论】:

    标签: java swing jframe invokelater


    【解决方案1】:

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

    有关详细信息,请参阅Concurrency in Swing(尤其是“初始线程”)。

    【讨论】:

    • @WearFox 正确,因为它更安全。它确保您的 UI 在 EDT 的上下文中初始化。特别是,您可以从 Andrew 提供的链接中查看Inital Threads,并在 Google 上搜索“Swing 单线程规则”以获取更多详细信息
    猜你喜欢
    • 2011-07-29
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2021-11-29
    相关资源
    最近更新 更多