Lecture 8 - Swing Flashcards

1
Q

What is an ActionListener

A
is a class that is responsible for handling all action events (ie. when the user clicks on a component or presses enter after inputting text).
User in control
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you use an ActionListener?

Give an example.

A
// Put it in the class title
public class Ex extends JFrame implements ActionListener {

// add it to the reference of the button & add it to panel
public Ex(){
button1.addActionListener(this);
this.add(button1);

//public void actionPerformed(ActionEvent e){
..
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you track how many times a button has been clicked?

A
public class Ex extends JFrame implements ActionListener {
int num = 0; // counter
private JButton b = new JButtom("click me"); 

public Ex(){

b. addActionListener(this);
this. add(b);

public void actionPerformed(ActionEvent e){
num++;
b.setText(String.format(“I was clicked %d times”, num));

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

Create a button on the west panel & add a method so that it is handled.

A
public class b extends JFrame implements ActionListener{ 
JButton button1,

public b(){
button1 = new JButton(“Please stay”);
this.add(button1, BorderLayout.WEST);
button1.addActionListener(this);

actionPerformed():
public void actionPerformed(ActionEvent e){
..}

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

What does Integer.toString() do?

A

returns a String object representing this Integer’s value.

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

Write an actionPerformed() that does something if button1 is pressed & something else if button 2 is pressed.

A

public void actionPerformed(ActionEvent e) {

if (e.getSource() == button1) {
clickCount++;
button2.setText(Integer.toString(clickCount));
} 
else if (e.getSource() == button2) {
clickCount--;
beepAndEnd(button2,clickCount);
}}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you turn a button off?

A

b.setEnabled(false);

// turn a button on
button.setEnabled(true);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you end a program?

A

if(e.getSource() == quitButtonExample);
System.exit(0);

// if its 0 it is a normal exit
// if its !=0 it tells the operating system something went wrong
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you enable a GUI class to handle action events?

A
  1. import java.awt.event.
  2. Add implements ActionListener to the class header
  3. Use the addActionListener() for each button that fires an event
    myButton.addActionListener(this);
  4. Write an actionPerformed method containing the event handling code, using e.getSource to determine which event occured
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you create a component in its own class & why is this useful?

A
By extending the JPanel
Include extends JPanel in the class header

Useful because we can create a Panel seperate to the JFrame and then attach it to the JFrame

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

How do you access private variables?

A

Using getters & setters

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

What do we need for JTextField events?

A

A JTextField does not generate an event until a user presses ENTER.

To listen for events like this we need:

  1. addActionListener();
  2. event gets delivered to the actionPerformed();
  3. use getSource() to find out which textfield sent the event
  4. call getText() of JTextField (to find out the text that the user entered)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do different numbers in .setLocation() change the location of the JFrame window?

A

Constants:
final int WIDTH = 0;
final int HEIGHT = 0;
this.setLocation(WIDTH, HEIGHT)

  • The bigger the width value is, the further to the RHS of the screen it is.
  • The bigger the height value is, the further down the screen the frame is.

Eg., this.setLocation(1400, 400);
and this.setSize(500,400)
The frame is in the bottom RHS corner of the screen, but this will vary depending on the size of your screen & the size of the Frame itself.

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

How do different numbers in .setSize change the shape of the JFrame window?

A

p.setSize(WIDTH, HEIGHT);

width is horizontal
height is vertical

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

Why does the frame still print the right size when you enter 0 for either the row or the column?

A

Because Java reads it as “use as many rows or columns as needed”.

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

What does getSource() do?

A

returns the object on which the Event initially occurred.

Eg., (if e.getSource() === button){
/// do something
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What do you add in the main method to create a GUI and make sure it can be seen?

A
Example1 myGUI = new Example1();
myGUI.setVisible(true);
18
Q

What does setText() do?

A

It lets you set the text of the thing that is calling it

Eg.,
public class void actionPerformed(ActionEvent e){
b.setText(“I was clicked”);

19
Q

What import do you need for a beep?

A

import java.awt.Toolkit;

20
Q

What does .addActionListener(this); say?

A

It says add the ActionListener which is this class

21
Q

How do you make a beep?

A

Toolkit.getDefaultToolkit().beep();

That is just a better way of writing:
Toolkit tk = Toolkit.getDefaultToolkit();
tk.beep();

22
Q

public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
clickCount++;
button2.setText(Integer.toString(clickCount));
}
else if (e.getSource() == button2) {
// user has pressed the right button… so count down
// and beep
Toolkit.getDefaultToolkit().beep();
clickCount = 0;

			//Toolkit.beep(); - cannot make a static reference to the static method beep
			if (clickCount > 0) {
				button2.setText(Integer.toString(clickCount));
			} else {
				System.exit(0);
			}
		}
	}
A

If we click button 1, it increments button 2.

Also hear a beep everytime we click

If we click button2, it reduces the same number until it gets less than one.

23
Q

What are these examples doing?

  1. textField1.addActionListener(parent)
  2. component.addActionListener(instanceOfListenerclass);
A

Register an instance of the event handler class as a listener on one or more components.

24
Q

How do you convert a String to an integer?

A

int a = Integer.parseInt()

25
Q

How do you convert a String to a double?

A

double d = Double.parseDouble(myString);

26
Q

How do you convert an Integer to a String?

A

Integer.toString(int)

Eg.,
int clickCount = 0;
Integer.toString(clickCount);

27
Q

Explain the purpose of these 2 classes?

public class Ex extends JFrame implements ActionListener {

public class ExPanel extends JPanel {

A

ExPanel creates a component in its own class
-passes another class as the variable to the constructor
public ExPanel(ActionListener parent) {
……creates some objects
…addActionListener();
-creates get() so another class can access the objects

Because ExPanel has created the objects, Ex can focus on the ActionListener.
-Creates a panel object globally using the constructor from the ExPanel class.
ExPanel p = new ExPanel(this);
- add this panel in the constructor & looks into this panel & looks at the components on it
add(p);

Now all that is needed is the
actionPerformed(ActionEvent e).. 
Since the buttons, textfields etc are in a different class it will use:
if(e.getSource() == p.getTextField());
...
28
Q

“Implements ActionListener” in the class name lets Java know….

A

It knows its expecting events from another class

29
Q

Write an if statement that checks if the user’s input is “red” & if it is it sets the background colour to red, if its anything else set it to white.

A
if (panel.getTextField1().getText().equals("red"))
panel.setBackground(Color.red);
else
panel.setBackground(Color.white);
}
30
Q

A JTextField only delivers strings from the user to the program. What do we do if we want to enter a number?

A
  1. getText() to give us it as a String
  2. Convert the string to a number.

int a = Integer.parseInt()
double d = Double.parseDouble(myString);

Eg.,
panel.getButton1().setText(Integer.toString(clickCount));

31
Q

How do you remove white space from a String in a textField

A

myString.trim();

32
Q

What might happen if you use:

int a = Integer.parseInt()
double d = Double.parseDouble(myString)

in a TextField?

A

It might throw an unchecked exception so we need to include a try/catch block.

33
Q

How do we put a number into our text field?

A

setText(“” + num)

34
Q

How do we disable a text field?

A

.setEditable(false)

35
Q

What is the JOptionPane class used for?

(Not for this course, for masters project)!

A

It is used to create ‘temporary’ windows (pop up windows).

ie., error message window

Once a user has delt with the JOptionPane they disappear!

36
Q

What are JOptionPane static methods?

(Not for this course, for masters project, look these up in week 7, video SwingCont 1_3).

A
  1. showMessageDialog - tell the user about something that has happened
  2. showConfirmDialog - ask a confirming question like yes/no/cancel
  3. showInputDialog - Prompt for some input
  4. showOptionDialog - The grand unification of the above three
37
Q

What is the model view design of programming?

A

The Model – View – Controller style of programming is a way of deciding who does what in a GUI program in three parts:

  1. Model - the application logic, independent of user interaction
  2. View - the graphical display to the user, the components
  3. Controller - the code for dealing with user input, which appears as events
38
Q

Why is BorderLayout useful?

A

Can be used as a component for another component to fill the space in the center

39
Q

How would I amend it so that there was a horizontal and vertical gap of 1/2 unit
between each panel (as in Fig 5 on sheet)?

A

this.setLayout(new GridLayout(3,2,UNIT/2,UNIT/2));

40
Q

What does this mean?

button2.addActionListener(this);

A

Action Listener is this class and we’re adding it to button2

41
Q

If there are 2 buttons being pressed. What should you consider?

A
  1. Which button is being pressed
  2. If the each button is pressed, what is the outcome? It might affect the other button.
  3. When does the program end ie., when the counter is 0 exit.