Java SWING Flashcards
(11 cards)
What two libraries must you import at the beggining of a java GUI project?
import java.awt.;
import java.swing.;
What is the first thing you do in a java UI application and why?
You crate a JFrame Object because the entire app is wrapped in a JFrame Object.
What do you do imediately after creating a JFrame?
set the size and set the default close operation as so:
```java
frame.setSize(width, hieght);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
~~~
How do you create a frame in java.swing?
```java
JFrame frame = new JFrame(frameTitle)
~~~
u
How do you implement a layout on a frame or pannel?
You use the set layout method as such:
```java
.setLayout(new Layout())
~~~
What layouts exists in java.swing and java.awt
FlowLayout → Arranges components in a row, left to right, and wraps when needed → Used in simple toolbars or small panels.
BorderLayout → Divides space into five regions: NORTH, SOUTH, EAST, WEST, CENTER → Used in main windows where CENTER takes most of the space.
GridLayout → Arranges components in a grid of equal-sized cells → Used in keypads, calculators, or evenly spaced forms.
BoxLayout → Aligns components vertically or horizontally → Used in vertical forms or horizontal toolbars.
GridBagLayout → Flexible grid that allows components to span rows/columns with custom size and alignment → Used in complex forms with varying component sizes.
CardLayout → Stacks components and shows one at a time → Used in wizards, tab switching, or login/signup screens.
GroupLayout → Groups components horizontally and vertically (often auto-generated) → Used in advanced UIs made with GUI builders like NetBeans.
SpringLayout → Positions components using custom distance constraints (springs) → Used in highly customized layouts, rarely used manually.
How do you set a grid layout in swing?
new GridLayout(rows, columns)
How do you create a JComboBox?
```java
JComboBox<DataType> = new JComboBox<>();
~~~</DataType>
How to you add options to a JComboBox object?
```java
comboboxObj.addItem(item)
~~~
What must you do at the end of every Java GUI project?
Make the frame visible by typing:
```java
frame.setVisible(true);
~~~
How do you ensure and app closes when the window closes in SWING?
```frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE):