Reputation: 97
I found this code strange. Because there is another function in before the parenthesis closed. Can you explain how it works?
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
------ The full code is below. This code is not belong to me. I found it while I am googling.
package pkginterface;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Interface extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
public Interface() {
// set flow layout for the frame
this.getContentPane().setLayout(new FlowLayout());
JButton button = new JButton("Java Code Geeks - Java Examples");
//add Button
add(button);
//set action listeners for buttons
button.addActionListener(this);
// define a custom short action command for the button
button.setActionCommand("Geeks");
// add button to frame
add(button);
}
@Override
public void actionPerformed(ActionEvent ae) {
String action = ae.getActionCommand();
if (action.equals("Geeks")) {
System.out.println("Button pressed!");
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new Interface();
//Display the window.
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Upvotes: 2
Views: 630
Reputation: 9971
It posts an event to the Swing's Event Dispatch Thread. The event causes the createAndShowUi() method to be called on the EDT Thread.
Upvotes: 1