Unit 6 (GUI) Flashcards
Which Java package is used for basic GUI programming?
A) java.util
B) java.awt
C) java.io
D) javax.swing
B) java.awt
What does the JOptionPane class primarily use for?
A) Handling database connections
B) Creating simple dialogues
C) Performing file operations
D) Managing threads
B) Creating simple dialogues
Which method from JOptionPane is used to display a dialog asking the user to enter some text?
A) showMessageDialog
B) showInputDialog
C) showConfirmDialog
D) showOptionDialog
B) showInputDialog
What is the primary purpose of containers in GUI applications?
A) To contain and manage other components
B) To display data in a table format
C) To execute background tasks
D) To provide networking capabilities
A) To contain and manage other components
What type of component is a JButton?
A) Container
B) Basic component
C) Layout manager
D) Event handler
B) Basic component
What will be the output of the following code snippet?
JOptionPane.showMessageDialog(null, “Hello, world!”);
A) Compiles and displays a message dialog with “Hello, world!”
B) Runtime error
C) Compilation error
D) No output
A) Compiles and displays a message dialog with “Hello, world!”
Which layout manager arranges components in a left-to-right flow?
A) BorderLayout
B) GridLayout
C) FlowLayout
D) CardLayout
C) FlowLayout
What is true about the JPanel class?
A) It cannot contain other components.
B) It is not a subclass of Container.
C) It uses BorderLayout as its default layout manager.
D) It is commonly used to group components within a frame.
D) It is commonly used to group components within a frame.
What is a JFrame?
A) A component that functions as a container for other components.
B) A layout manager.
C) A type of event.
D) A basic GUI component.
A) A component that functions as a container for other components.
Which method is used to make a JFrame visible?
A) display()
B) show()
C) setVisible(true)
D) open()
C) setVisible(true)
What will happen if two buttons are added to a frame that uses FlowLayout and the frame is resized?
A) The buttons will overlap.
B) The buttons will adjust to maintain the flow from left to right.
C) Only one button will be visible.
D) The buttons will disappear.
B) The buttons will adjust to maintain the flow from left to right.
What is the correct way to add a JButton to a JFrame?
A) frame.add(new JButton(“Click Me!”));
B) JButton button = “Click Me!”; frame.add(button);
C) frame.setButton(new JButton(“Click Me!”));
D) JButton button = new JButton(); frame.add(button.setText(“Click Me!”));
A) frame.add(new JButton(“Click Me!”));
What does the setForeground method do in a GUI component?
A) Changes the background color of the component.
B) Adjusts the size of the component.
C) Sets the color used to draw the text or graphics within the component.
D) Sets the font of the text within the component.
C) Sets the color used to draw the text or graphics within the component.
What will be the output if the following code is executed? Assume the button is clicked by the user.
JButton button = new JButton(“Press me”);
button.addActionListener(e -> System.out.println(“Button pressed!”));
A) “Press me”
B) “Button pressed!”
C) No output
D) Compiles with an error
B) “Button pressed!”
What is the primary use of a JLabel?
A) To display editable text fields.
B) To create a clickable button.
C) To display a short text string or an image.
D) To select one option from a set of choices.
C) To display a short text string or an image.