✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.
The following code is intended to show quotes; it provides a window, a button, a text (JLabel), and another button:
JFrame frame = new JFrame("Quote"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setSize(400, 300); frame.setLayout(new BorderLayout()); JButton quoteButton = new JButton("Show Quote"); frame.add(quoteButton,BorderLayout.NORTH); JLabel quoteText = new JLabel("Quote coming soon ..."); quoteText.setHorizontalAlignment(SwingConstants.CENTER); frame.add(quoteText,BorderLayout.CENTER); JButton anotherQuoteButton = new JButton("Show Quote");
frame.setVisible(true);
Assume you want to show the quote in the text field when pressing a button with text "Show Quote". Which of the following statements will enable this? (Hint: Note the visibility of GUI elements and whether the listener will alter the text field.)
a)
quoteText.addActionListener(e -> { quoteText.setText("<html> There are 10 kind of people,<br> those who understand binary, <br> and those who don’t.</html>"); frame.repaint(); });
b)
quoteButton.addActionListener(e-> { quoteButton.setText("<html> There are 10 kind of people,<br> those who understand binary, <br> and those who don’t.</html>"); frame.repaint(); });
c)
anotherQuoteButton.addActionListener(e-> { quoteText.setText("<html> There are 10 kind of people,<br> those who understand binary, <br> and those who don’t.</html>"); frame.repaint(); });
d)
quoteButton.addActionListener(e-> { quoteText.setText("<html> There are 10 kind of people,<br> those who understand binary, <br> and those who don’t.</html>"); frame.repaint(); });