-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPromotionDialog.java
70 lines (57 loc) · 2.17 KB
/
PromotionDialog.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
//class for the dialog box that appears when a pawn is being promoted
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PromotionDialog extends JDialog
{
private int selectedButton;
//static constants for selectedButton
public static final int QUEEN = 1, KNIGHT = 2, BISHOP = 3, ROOK = 4;
private JRadioButton queenRButton, knightRButton, bishopRButton, rookRButton;
public PromotionDialog(JFrame parent)
{
super(parent, true);
JPanel radioPanel = new JPanel();
add(radioPanel, BorderLayout.CENTER);
//create group for radio buttons
ButtonGroup radioGroup = new ButtonGroup();
//create radio buttons for each piece type
queenRButton = new JRadioButton("Queen");
queenRButton.setSelected(true);
radioPanel.add(queenRButton);
radioGroup.add(queenRButton);
knightRButton = new JRadioButton("Knight");
radioPanel.add(knightRButton);
radioGroup.add(knightRButton);
bishopRButton = new JRadioButton("Bishop");
radioPanel.add(bishopRButton);
radioGroup.add(bishopRButton);
rookRButton = new JRadioButton("Rook");
radioPanel.add(rookRButton);
radioGroup.add(rookRButton);
//create okay button
JButton okayButton = new JButton("Okay");
okayButton.addActionListener(new ActionHandler());
add(okayButton, BorderLayout.SOUTH);
//setup this dialog box's appearance/behavior
setSize(300, 200);
setTitle("Promotion");
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
}
private class ActionHandler implements ActionListener
{
//action fired when "Okay" button clicked
public void actionPerformed(ActionEvent e)
{
if(queenRButton.isSelected()) selectedButton = QUEEN;
else if(knightRButton.isSelected()) selectedButton = KNIGHT;
else if(bishopRButton.isSelected()) selectedButton = BISHOP;
else if(rookRButton.isSelected()) selectedButton = ROOK;
setVisible(false);
}
}
public int getSelectedButton()
{
return selectedButton;
}
}