Implemented custom board config dialog

This commit is contained in:
Kai S. K. Engelbart 2019-04-07 16:05:15 +02:00
parent a83fe6819b
commit 2ac192c230
2 changed files with 31 additions and 27 deletions

View File

@ -13,7 +13,6 @@ import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.EmptyBorder;
/**
* Project: <strong>Minesweeper</strong><br>
* File: <strong>CustomDialog.java</strong><br>
@ -23,8 +22,9 @@ import javax.swing.border.EmptyBorder;
public class CustomDialog extends JDialog {
private static final long serialVersionUID = -4019516811065781434L;
private final JPanel mcontentPanel = new JPanel();
private final JSlider sliderBoardWidth, sliderBoardHeight, sliderNumMines;
private final JPanel mcontentPanel = new JPanel();
private BoardConfig result;
/**
* Create the dialog.
@ -43,13 +43,11 @@ public class CustomDialog extends JDialog {
lblBoardWidth.setFont(new Font("Tahoma", Font.PLAIN, 14));
mcontentPanel.add(lblBoardWidth);
}
{
sliderBoardWidth = new JSlider();
sliderBoardWidth.setValue(16);
sliderBoardWidth.setMinimum(2);
sliderBoardWidth.setMaximum(30);
mcontentPanel.add(sliderBoardWidth);
}
JSlider sliderBoardWidth = new JSlider();
sliderBoardWidth.setValue(16);
sliderBoardWidth.setMinimum(2);
sliderBoardWidth.setMaximum(30);
mcontentPanel.add(sliderBoardWidth);
{
JLabel label = new JLabel("");
mcontentPanel.add(label);
@ -59,13 +57,11 @@ public class CustomDialog extends JDialog {
lblBoardHeight.setFont(new Font("Tahoma", Font.PLAIN, 14));
mcontentPanel.add(lblBoardHeight);
}
{
sliderBoardHeight = new JSlider();
sliderBoardHeight.setValue(16);
sliderBoardHeight.setMaximum(30);
sliderBoardHeight.setMinimum(2);
mcontentPanel.add(sliderBoardHeight);
}
JSlider sliderBoardHeight = new JSlider();
sliderBoardHeight.setValue(16);
sliderBoardHeight.setMaximum(30);
sliderBoardHeight.setMinimum(2);
mcontentPanel.add(sliderBoardHeight);
{
JLabel label = new JLabel("");
mcontentPanel.add(label);
@ -75,13 +71,11 @@ public class CustomDialog extends JDialog {
lblNumberOfMines.setFont(new Font("Tahoma", Font.PLAIN, 14));
mcontentPanel.add(lblNumberOfMines);
}
{
sliderNumMines = new JSlider();
sliderNumMines.setValue(16);
sliderNumMines.setMinimum(2);
sliderNumMines.setMaximum(200);
mcontentPanel.add(sliderNumMines);
}
JSlider sliderNumMines = new JSlider();
sliderNumMines.setValue(16);
sliderNumMines.setMinimum(2);
sliderNumMines.setMaximum(200);
mcontentPanel.add(sliderNumMines);
{
JLabel label = new JLabel("");
mcontentPanel.add(label);
@ -93,6 +87,11 @@ public class CustomDialog extends JDialog {
{
JButton okButton = new JButton("Start Game");
okButton.setActionCommand("OK");
okButton.addActionListener((evt) -> {
result = new BoardConfig(sliderBoardWidth.getValue(), sliderBoardHeight.getValue(),
sliderNumMines.getValue());
dispose();
});
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
@ -103,9 +102,10 @@ public class CustomDialog extends JDialog {
buttonPane.add(cancelButton);
}
}
setVisible(true);
}
public BoardConfig getBoardConfig() {
return new BoardConfig(sliderBoardWidth.getValue(), sliderBoardHeight.getValue(), sliderNumMines.getValue());
public BoardConfig showDialog() {
setVisible(true);
return result;
}
}

View File

@ -133,6 +133,10 @@ public class Minesweeper {
easyMenuItem.addActionListener((evt) -> initGame(easyConfig));
mediumMenuItem.addActionListener((evt) -> initGame(mediumConfig));
hardMenuItem.addActionListener((evt) -> initGame(hardConfig));
customMenuItem.addActionListener((evt) -> {
BoardConfig cfg = new CustomDialog(mframe).showDialog();
if (cfg != null) initGame(cfg);
});
aboutMenuItem.addActionListener((evt) -> {
JOptionPane.showMessageDialog(board, "Minesweeper version " + VERSION + "\nby Kai S. K. Engelbart");
});