Friday Java Quiz: What Will The GUI Show?
1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 5 public class Foo { 6 public static void main(final String[] args) { 7 EventQueue.invokeLater(new Runnable() { 8 public void run() { 9 swingMain(); 10 } 11 }); 12 } 13 14 private static void swingMain() { 15 JFrame frame = new JFrame("Demo"); 16 frame.setLayout(new FlowLayout()); 17 18 JTextField tf1 = new JTextField(20); 19 final JTextField tf2 = new JTextField(20); 20 JButton b = new JButton("Click Me"); 21 22 tf1.addFocusListener(new FocusAdapter() { 23 @Override 24 public void focusLost(FocusEvent e) { 25 tf2.setText("Lost Focus!"); 26 } 27 }); 28 29 b.addActionListener(new ActionListener() { 30 public void actionPerformed(ActionEvent e) { 31 tf2.setText("Clicked!"); 32 } 33 }); 34 35 frame.add(tf1); 36 frame.add(tf2); 37 frame.add(b); 38 frame.pack(); 39 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 40 frame.setVisible(true); 41 } 42 }
Q: If, after starting the above Swing program, you click on the JButton b, what will the JTextField jt2 show?
Lost Focus!
or
Clicked!
Strict rule apply: no compiling and running, no Googling, and your first answer is the final answer. You are allowed to read the Javadoc of the relevant classes though.