Java Swing Components Quiz

FluentHyena avatar
FluentHyena
·

Start Quiz

Study Flashcards

12 Questions

What is Java Swing?

A built-in package in Java used for creating window-based applications

Which of the following is a Java Swing component?

JTextField

What does JFrame allow you to do in Java Swing?

Create a window-based application

What is the purpose of JPanel in Java Swing?

To store a group of components

Which method in JFrame is used to set the title of the frame?

setTitle(String a)

What type of text display component is JLabel in Java Swing?

Simply display text

Which method is used to set text on a Java JLabel?

setText(String s)

What does the getText() method of a Java JButton return?

The text the button will display

Which alignment options are available for setVerticalAlignment() in a Java JLabel?

TOP, CENTER, BOTTOM

What does the setEnabled(Boolean b) method do in a Java Button (JButton)?

Used to enable or disable the button

What component allows editing of a single line of text in Java Swing?

Java TextField (JTextField)

Which method is used to change the color of a Java Label (JLabel)?

setForeground(Color c)

Study Notes

Java Swing

  • A built-in package in Java used to create a window-based application or GUI application.
  • An enhanced and improved version of AWT.

Java Swing Components

  • Components include: JPanel, JCheckBox, JLabel, JButton, JTextField, JList, JTable, JPasswordField, and more.

Java Frame (JFrame)

  • A Java Swing container that allows creation of a window.
  • Commonly used methods in JFrame:
    • setTitle(String a) sets the title of the Frame.
    • setSize(width, height) customizes the size of the Frame.
    • setLocationRelativeTo(null) sets the frame on center/input after setSize.
    • add(Component comp) adds a swing component in JFrame.
    • remove(Component comp) removes a swing component in JFrame.
    • setResizable(Boolean resizable) controls whether the user can resize the frame.
    • setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) with options: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, EXIT_ON_CLOSE, DISPOSE_ON_CLOSE.
    • setVisible(Boolean e) makes the frame visible on screen (true) or hides it (false).

Java Panel (JPanel)

  • A Java Swing container that stores a group of components.
  • Commonly used methods in JPanel:
    • setSize(width, height) customizes the size of the panel.
    • add(Component comp) adds a swing component in panel.
    • setResizable(Boolean resizable) controls whether the user can resize the panel.

Java Label (JLabel)

  • A Java Swing component that displays text and can display an image.
  • Can be customized according to its Font, Size, and Color.
  • Commonly used methods in JLabel:
    • setText(String s) sets the text on the label.
    • getText() returns the text the label will display.
    • setIcon(Icon i) sets the icon that the label will display.
    • getIcon() returns the image that the label displays.
    • setFont(new Font(“Font Name”, Font.BOLD, FontSize)) customizes font.
    • setHorizontalAlignment(SwingConstants.) sets horizontal alignment (LEFT, CENTER, RIGHT, LEADING, TRAILING).
    • setVerticalAlignment(SwingConstants.) sets vertical alignment (TOP, CENTER, BOTTOM).
    • setForeground(Color c) sets the color.

Java Button (JButton)

  • A labeled button that generates an event when pressed or clicked.
  • Commonly used methods in JButton:
    • setText(String s) sets the text on the label.
    • getText() returns the text the label will display.
    • setIcon(Icon i) sets the icon that the label will display.
    • getIcon() returns the image that the label displays.
    • setEnabled(Boolean b) enables or disables the button.
    • addActionListener(this) adds an action listener to this object.
    • addActionListener(new ActionListener(){}) for multiple buttons.

Test your knowledge on Java Swing and its components such as JFrame, JPanel, JLabel, and JButton. Explore the basics of creating window-based applications and GUIs using Java Swing.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Java Swing GUI Components
10 questions

Java Swing GUI Components

RespectableMagnesium avatar
RespectableMagnesium
Java GUI Components: AWT and Swing
30 questions

Java GUI Components: AWT and Swing

FlourishingPyramidsOfGiza avatar
FlourishingPyramidsOfGiza
Java Swing Tutorial
11 questions
Use Quizgecko on...
Browser
Browser