forked from dr-cs/intro-oop-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreetingGui.java
More file actions
42 lines (33 loc) · 1.19 KB
/
GreetingGui.java
File metadata and controls
42 lines (33 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GreetingGui extends JFrame {
private JLabel greetingLabel;
private class GreetButtonListener implements ActionListener {
private JLabel greetingLabel;
public GreetButtonListener(JLabel greetingLabel) {
this.greetingLabel = greetingLabel;
}
public void actionPerformed(ActionEvent e) {
Greeter greeter = new Greeter("bob");
String greeting = greeter.greet();
greetingLabel.setText(greeting);
}
}
public GreetingGui() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
greetingLabel = new JLabel("Greeting will go here.");
JButton button = new JButton("Greet!");
button.addActionListener(new GreetButtonListener(greetingLabel));
add(greetingLabel, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
pack();
}
public static void main(String[] args) {
GreetingGui gg = new GreetingGui();
gg.setVisible(true);
}
}