Basic GUI Flashcards

1
Q

What form of programming do graphical user interfaces tend to use?

A

Event-driven programming

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

What are the GUI packages used in Java?

A

AWT is the older framework. It involves “heavy-weight” components that use native system GUI elements

Swing is the newer version. Most components are lightweight.

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

What are heavyweight GUI components?

A

Those that use native system GUI elements

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

What is the naming convention for Java GUI frameworks?

A

AWT elements have no prefix i.e. “Component”

Swing components use J as a prefix i.e. “JComponent”

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

What are the basic GUi components?

A

Windows - top level containers i.e. frame and dialog box

Components - GUI widgets i.e. button, label

Containers - logical grouping for components i.e panel

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

How are dialog boxes created?

A

Using the static methods of JOptionPane i.e. .showInputDialog(“…”) and .showMessageDialog(null, “…”)

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

How does the showInputDialog static method work?

A

Returns the string that the user enters or null if they press cancel.

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

How does the showMessageDialog static method work?

A

First argument is where to display the alert. Use null for centre.

The second argument is the message to display.

Third argument is the title

Fourth is the type of message to display, using enums i.e. JOptionPane.ERROR_MESSAGE

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

What GUI component allows colours to be selected? What does it return?

A

JColorChooser.

It returns a color of type Color, or null if the user presses cancel

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

What is a frame?

A

A frame (i.e. JFrame) is a graphical window that holds other components. It includes a border and title bar.

The visible area of the Frame is a Container. It can be accessed by calling getContentPane()

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

How are components added to a frame?

A

getContentPane().add(component)

This returns a Container

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

What are the steps for creating a new frame?

A
// Create it
JFrame frame = new JFrame()
// Set close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
// add components
frame.getContentPane().add(...)

// make it visible

frame. setSize(400, 200)
frame. setVisible(true)

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

What GUI component is used to group GUI elements?

A

A JPanel

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

How are components added to panels?

A

With .add() i.e. panel.add(new JButton(“OK”))

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

How are labels created?

A

JLabel label = new JLabel(“Hello”)

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

How are buttons created?

A

JButton button = new JButton(“hello”)

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

What are layout managers?

A

Layout managers arrange GUI elements in a container for layout purposes i.e. to support resizing.

They implement the LayoutManager interface

18
Q

How is the layout manager specified?

A
Container pane = getContentPane();
pane.setLayout(new FlowLayout())
19
Q

What does the BorderLayout layout manger do?

A

Allows elements to be placed at the top (PAGE_START), bottom (PAGE_END), left (LINE_START), right (LINE_END) or middle (CENTER)

Components can be added to the pane with pane.add(button, BorderLayout.PAGE_START) once the layout manager of the pane has been set.

20
Q

How can the position of components in a BorderLayout manager be specified?

A

.add(component, BorderLayout.CENTER) is preferred. However, these arguments can be reversed or .add(“Centre”, button) can be used

21
Q

What does the FlowLayout manager do?

A

It places components over several lines from left-to-right, top-to-buttom.

The default constructor is centre aligned; FlowLayout.RIGHT or .LEFT can be used to right/left align

22
Q

How can custom painting inside a JPanel be implemented?

A

By overriding paintComponent(Graphics g)

n.b. this method is called automatically by the Java runtime when the frame is first displayed, resized, or covered and then returned to (some platforms)

23
Q

What are the basic graphics methods?

A

dawLine(), drawRect(), fillRect(), drawOval(), fille(Oval), drawString() etc.

n.b. drawABC() is jus the outline, fillABC() fills it in

setStroke() changes the BasicStroke. Use setPaint() to change the color i.e. Color.RED

24
Q

What are the basic types of nested classes?

A

Static and Non-Static (Inner class)

25
Q

What access does an inner class have?

A

Complete accès to the field and methods of an enclosing class, even if they are declared private

26
Q

Where are inner classes saved after compilation?

A

In their own .class file i.e. OuterClass$InnerClass.class

27
Q

Why are nested classes used?

A

They logically group classes that are only used in one place. This increases encapsulation and makes code more readable/maintainable

28
Q

How do static nested classes work?

A

The nested class can be used to make the objects of the nester class independent from the objects of the enclosing class

It can access static members and variables of its outer class (even if private). However, it cannot refer to instance members.

The outer class has full access to the static nested class.

It can also be instantiated/accessed without an instance of the outer class.

NOTE: You still instantiate it (super weird?)

29
Q

Can you instantiate static classes?

A

Only if they are inner classes!!!!!!!

30
Q

Terst

A

Test

31
Q

What are inner classes and what types do they come in?

A

They are non-static classes i.e. those that are associated with an instant of the outer class.

Member class: defined within the body of a class
Local class: defined within the method of a class
Anonymous class: local class that is declared implicitly by creating a variable of it
32
Q

What are member classes?

A

They are not declared static and are declared in the body of a class.

They cannot declare static variable and methods. They have access to all instance and class variables and objects of the outer class / any other inner classes.

The outer class has access to all variables and methods in the inner class

33
Q

What are local classes?

A

They are defined within a method and exist only until the end of that method/block.

Thy are never declared with access modifiers - scope is always restricted to their block. They cannot include static variables or methods.

They can access the fields of the containing class and the local variables of the method they are declared in.

34
Q

What kind of inner nested classes can be interfaces?

A

Only static members

35
Q

What kind of inner nested classes can be interfaces?

A

Only static members

36
Q

What methods are required to implement ActionListeners?

A

actionPerformed(ActionEvent e)

37
Q

What methods are required to implement ActionListeners?

A

actionPerformed()

38
Q

How are action listeners registered?

A

First create an instance as a variable, such as an anonymous class.

Then register it with component.addActionListener(handler);

39
Q

How is the text on buttons/labels edited?

A

With getText() and setText()

40
Q

What are the components for text entry?

A

JTextField is single line, JTextArea is multiple lines.

Where colours in the number of letters, they are created with:

JTextField(int columns) and JTextArea(int lines, int columns)

41
Q

How are strings made uppercase?

A

With stringVar.toUpperCase(). This returns the result.