22 import javax.swing. *; 2 import java.awt.*; 3 import java.awt.event.*; 5 public class MyFavoriteAuthor extends JPanel implements ActionListener, ItemListener ! JPanel pl = new JPanel(); JPanel p2 - new JPanel(); 9 String author() - ("Stephen King","H. G. Wells", 10 "Isaac Asimov", "Orson Scott Card"); 11 JComboBox<String> cb = new JComboBox<String>(author); 12 JLabel 11 = new JLabel("My favorite author: "); 13 JLabel 12 = new JLabel("My favorite author is Stephen King."); 14 JTextField tf = new JTextField("",15); 15 JButton b = new JButton ("Add"); 16 Font font = new Font ("Verdana", Font.PLAIN, 18); 17 18 MyFavoriteAuthor() { 19 11. setFont (font); 20 12. setFont (font); 21 cb.set Font (font); tf. setFont (font); 23 set Layout (new GridLayout(2,1)); 24 pl.add (11); 25 pl.add(cb); 26 pl.add(tf); 27 pl.add(b); 28 p2.add(12); 29 add (pl); 30 add (p2); 31 b.addActionListener(this); cb.addItemListener(this); 33 } 34 public void actionPerformed (ActionEvent e) { if((e.getSource() == b) && (!tf.getText().isBlank())) { cb.addItem(tf.getText()); 37 } ) 39 public void itemStateChanged (ItemEvent e) { if (e.getSource() == cb) { 41 12.setText ("My favorite author is + cb.getSelectedItem() + "."); //System.out.println("State is changed."); 44 45 } public static void main(String[] args) { JFrame f = new JFrame ("My Favorite Authors"); f.add(new MyFavoriteAuthor()); 49 f.setLocation (40,150); f.setSize (800, 150); 51 f.setVisible(true); 52 f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ; 53 O DODOM 32 35 36 ا ام ا لم لا لا لا لا لا لا لا ه ه ه ه ه ه ه ه ه ه ا ا ا ا ا 38 40 42 43 46 47 OOO OM 48 50 54
6.1 What is the purpose of checking the following condition? Explain your reasoning. (2 points) if((e.getSource() == b) && (!tf.getText().isBlank()) 6.2 If you uncomment the statement at line 43 and select another item, you will get a message “State is changed" at the console two times, as shown in figure below. Explain the reason you get the message two times instead of one. (2 points) Problems Javadoc Declaration Console MyFavorite Author Llava Application] CWUsers\001011\.pz\poolplugins\org adipse justj, operidk hotspotjre.fullwin32.x86_64.15.0.1,v20201027-0507)jreyt State is changed. State is changed. My Favorite Authors My favorite author: H. G. Wells Add My favorite author is H. G. Wells. Witable Smartsert 44:13: 1232 6.3 Describe what happens when a user keys in some words in the text field and presses Enter on the keyboard. Explain your reasoning. (2 points) 6.4 When is the method itemStateChanged(ItemEvent e) called? By whom is it called? (1 point)
22 import javax.swing. *; 2 import java.awt.*; 3 import java.awt.event.*; 5 public class MyFavoriteAuthor extends JPanel
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
22 import javax.swing. *; 2 import java.awt.*; 3 import java.awt.event.*; 5 public class MyFavoriteAuthor extends JPanel
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!