forked from dr-cs/intro-oop-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJack.java
More file actions
70 lines (59 loc) · 2.07 KB
/
Jack.java
File metadata and controls
70 lines (59 loc) · 2.07 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class Jack extends JFrame {
private JLabel imageLabel;
private class JackWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
int choice = JOptionPane.showConfirmDialog(
Jack.this,
"Do you really want to exit?",
"Exit for reals?",
JOptionPane.OK_CANCEL_OPTION
);
if (choice == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
}
public Jack() {
// Need to set DO_NOTHING_ON_CLOSE so we can handle window closing
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
// Confirm exit before exiting program
addWindowListener(new JackWindowListener());
imageLabel = new JLabel();
add(imageLabel, BorderLayout.CENTER);
add(createButtonBox(), BorderLayout.SOUTH);
pack();
}
private Box createButtonBox(){
JButton jackButton = new JButton("Show Jack of Hearts");
ImageIcon buttonIcon = new ImageIcon("New16.gif");
jackButton.setIcon(buttonIcon);
ActionListener jackListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
ImageIcon jackIcon = new ImageIcon("JACK-HEARTS.png");
imageLabel.setIcon(jackIcon);
pack();
}
};
jackButton.addActionListener(jackListener);
Box buttonBox = new Box(BoxLayout.X_AXIS);
buttonBox.add(jackButton);
return buttonBox;
}
public static void main(String[] args) {
Jack j = new Jack();
j.setVisible(true);
}
}