Lecture 6 - Swing Flashcards

1
Q

What are 5 examples of JFrame methods?

A

setTitle(String)
setSize(int dimx, int dimy) - num of pixels in the x and y directions
setLocation(int x, int y) - distance of top left corner of frame from top left corner of monitor, in pixels. If you don’t include any location method, the location is automatically set to (0,0).
setVisible(boolean)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)- ensure the system exits the application when user closed the Frame

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do we create a GUI in Java swing?

A
  1. The program creates a window object (JFrame)
  2. The program creates several component objects (JComponents)
  3. Layout managers (allows children to overlap)
  4. Event listener objects (handles the events that arrive at each of the components).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do we customise our own JFrame?

A

use inheritence ie. extends JFrame

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What class do we have to import to use JFrame?

A

javax.swing.JFrame

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are 3 examples of layout managers?

A
  1. FlowLayout (horizontal)
  2. BorderLayout (content pane is divided into 5 areas)
  3. GridLayout(centres vertically & horizontally)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Give an example of how you could create a panel & give it a GridLayout?

A

JPanel myPanel = new JPanel( new GridLayout(0,1))

There are actually 2 rows 1 col, but we can add 0 because the computer will interpret it as “use however many rows are needed”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Give an example of how you could create a panel and give it a FlowLayout

A

JPanel mainPanel = new JPanel(FlowLayout())

**don’t need this because FlowLayout is the deafult layout for a JPanel

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Give an example of how you create a label and add it to a panel?

A

Global:
private JLabel label1;

Main:
label1 = new JLabel(“Click here”);
JPanel myPanel = new JPanel();
myPanel.add(label1);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Give an example of how you can create a text field and add it to a panel

A

Global:
private JTextField textField1;

Main:
textField1  = new JTextField("initial text", 15);
JPanel p = new JPanel();
p.add(textField1);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Give an example of how you can add a button?

A

Global:
private JButton button1;

Main:
button1  = new JButton("quit");

JPanel myPanel = new JPanel();

myPanel.add(button1);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Give an example of how to add a label with the BorderLayout() to a panel

A
this.add(new JLabel("NORTH"), BorderLayout.NORTH);
or
p.add(new JLabel("bottom"), BorderLayout.SOUTH);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain a JPanel

A

Its a generic container

Its 1 component, but can store multiple components

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the default layout for JPanel?

A

FlowLayout();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the default layout for JFrame?

A

BorderLayout();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are useful methods for JPanel?

A

setBackground(color)
getHeight()
getWidth()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why is BorderLayout() useful?

A
  • Its like a container that lets us arrange and resize components to fit in 5 regions: north, south, east, west, center.
  • there can be 1 component per region:NORTH, SOUTH, EAST, WEST, CENTER

** note american spelling needed

17
Q

Why do we have to layer things on top of each other to build a GUI?

A

Because we are limited in what we can do in 1 go.

18
Q

How do you set a constant for width and height?

A

Add a global constant:

final int WIDTH = 200;
final int HEIGHT = 100;

// use capitals because we are referring to a constant

19
Q

What does this mean:

public class a extends JFrame{
final int UNIT = 20;
this.setSize(30*UNIT, 30*UNIT)
A

It says that UNIT is a constant int 20. This is the size of 1 mini box.

To get the size of the large box. We need to multiply the rows and columns by 30 because there are 30 mini boxes.

20
Q

What would this look like?

import java.awt.*;
 import java.applet.Applet;
 public class ButtonGrid extends Applet {
     public void init() {
         setLayout(new GridLayout(3,2));
         add(new Button("1"));
         add(new Button("2"));
         add(new Button("3"));
         add(new Button("4"));
         add(new Button("5"));
         add(new Button("6"));
     }
 }
A

Depends on the ComponentOrientation!

This 1 is left to right so it would look like this:

1 2
3 4
5 6

21
Q

How do you create a border?

A
// adding borders 
		Border blackline = BorderFactory.createLineBorder(Color.black);
		topPanel.setBorder(blackline);
		bottomPanel.setBorder(blackline);
		rightPanel.setBorder(blackline);
		leftPanel.setBorder(blackline);
		centerPanel.setBorder(blackline);
22
Q

What is GridBagLayout?

A

Is a flexible layout manager that aligns the components vertically and horizontally without requiring that the components be of the same size.

This is different to gridlayout which requires the rectangles to be divided into equal sizes.

23
Q

How do you set font and change the font of the label?

A

Font f = new Font(“TimesRoman”, Font.PLAIN, 24);

// changing the font of the labels
topPanel.setFont(f);