这是文档:
导致 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);
}
});
}
没有调用Later
public static void main(String[] args) {
Example Ventana=new Example();
}
谢谢你的建议