Podcast
Questions and Answers
What is Java Swing?
What is Java Swing?
Which of the following is a Java Swing component?
Which of the following is a Java Swing component?
What does JFrame allow you to do in Java Swing?
What does JFrame allow you to do in Java Swing?
What is the purpose of JPanel in Java Swing?
What is the purpose of JPanel in Java Swing?
Signup and view all the answers
Which method in JFrame is used to set the title of the frame?
Which method in JFrame is used to set the title of the frame?
Signup and view all the answers
What type of text display component is JLabel in Java Swing?
What type of text display component is JLabel in Java Swing?
Signup and view all the answers
Which method is used to set text on a Java JLabel?
Which method is used to set text on a Java JLabel?
Signup and view all the answers
What does the getText() method of a Java JButton return?
What does the getText() method of a Java JButton return?
Signup and view all the answers
Which alignment options are available for setVerticalAlignment() in a Java JLabel?
Which alignment options are available for setVerticalAlignment() in a Java JLabel?
Signup and view all the answers
What does the setEnabled(Boolean b) method do in a Java Button (JButton)?
What does the setEnabled(Boolean b) method do in a Java Button (JButton)?
Signup and view all the answers
What component allows editing of a single line of text in Java Swing?
What component allows editing of a single line of text in Java Swing?
Signup and view all the answers
Which method is used to change the color of a Java Label (JLabel)?
Which method is used to change the color of a Java Label (JLabel)?
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.
Description
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.