-
Notifications
You must be signed in to change notification settings - Fork 2
/
BackButton.java
74 lines (64 loc) · 2.36 KB
/
BackButton.java
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
71
72
73
74
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.CardLayout;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;
import java.awt.Dimension;
public class BackButton extends JButton implements ActionListener
{
//necessary for switching between cards
private CardLayout layout;
private JPanel parentPanel;
private String prevPanelName; //the name of the panel for this button
//to switch to. Note that it doesn't have to be the previous one, but
//it would make more sense to use it to switch to the previous panel
//because it is a "back" button afterall.
//constructs a back button which takes in the nessecary variables for switching
//between panels in a card layout.
public BackButton(CardLayout layoutIn, JPanel parentPanelIn, String prevPanelNameIn)
{
setText("Back");
setBackground(new Color(184, 71, 42));
setFont(new Font(FlightSimulator.FONTSTYLE, Font.PLAIN, 30));
setForeground(Color.WHITE);
layout = layoutIn;
parentPanel = parentPanelIn;
prevPanelName = prevPanelNameIn;
this.addActionListener(this);
setFocusPainted(false);
setBorderPainted(false);
setPreferredSize(new Dimension(150, 50));
}
//different constructor for a non-specific destination for the back button
public BackButton(CardLayout layoutIn, JPanel parentPanelIn)
{
setText("Back");
setBackground(new Color(184, 71, 42));
setFont(new Font(FlightSimulator.FONTSTYLE, Font.PLAIN, 30));
setForeground(Color.WHITE);
layout = layoutIn;
parentPanel = parentPanelIn;
prevPanelName = null;
this.addActionListener(this);
setFocusPainted(false);
setBorderPainted(false);
setPreferredSize(new Dimension(150, 50));
}
public void setDestination(String name)
{
prevPanelName = name;
}
//checks if the previous name variable is null, if it isn't then switch to that
//panel, if it is null, simply show the previous panel in the card layout.
public void actionPerformed(ActionEvent e)
{
if (prevPanelName != null)
layout.show(parentPanel, prevPanelName);
else
{
layout.previous(parentPanel);
}
}
}