Java Swing Components Quiz

FluentHyena avatar
FluentHyena
·
·
Download

Start Quiz

Study Flashcards

Questions and Answers

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?

<p>To store a group of components</p> Signup and view all the answers

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

<p>setTitle(String a)</p> Signup and view all the answers

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

<p>Simply display text</p> Signup and view all the answers

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

<p>setText(String s)</p> Signup and view all the answers

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

<p>The text the button will display</p> Signup and view all the answers

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

<p>TOP, CENTER, BOTTOM</p> Signup and view all the answers

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

<p>Used to enable or disable the button</p> Signup and view all the answers

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

<p>Java TextField (JTextField)</p> Signup and view all the answers

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

<p>setForeground(Color c)</p> Signup and view all the answers

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.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Quizzes Like This

Java Swing Components Quiz
5 questions

Java Swing Components Quiz

GuiltlessMossAgate6560 avatar
GuiltlessMossAgate6560
Java Swing Components Quiz
12 questions
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
Use Quizgecko on...
Browser
Browser