-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
133 lines (127 loc) · 4.59 KB
/
GUI.java
File metadata and controls
133 lines (127 loc) · 4.59 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
*Het scherm waar de applicatie op draait
*
* @author Madelon
* @version 2.0
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class GUI extends JFrame implements ActionListener{
//JFrame f;
JPanel info; //het paneel met alle invoervakjes
JButton start; //het knopje dat het huidige scherm verwijdert en minesweeper opstart
JTextField rijen; //het vakje met invoer van de rijen
JTextField kolommen; //het vakje met invoer van de kolommen
public GUI() {
info = new JPanel();
info.setSize(300,200);
JLabel Rijennum = new JLabel( "Voer het aantal rijen in" );
rijen = new JTextField( 10 );
JLabel Kolommennum = new JLabel( "Voer het aantal kolommen in" );
kolommen = new JTextField( 10 );
start = new JButton("Start Minesweeper");
start.addActionListener(this); //actionlistener om alle waardes te controleren
add(info); //het paneel is toegevoegd aan het JFrame
//alle elementen aan het paneel toevoegen
info.add(Rijennum);
info.add(rijen);
info.add(Kolommennum);
info.add(kolommen);
info.add(start);
//de grootte van het JFrame vaststellen.
setSize(350,175);
}
public static void main( String args[] ) {
//stelt het JFrame in
JFrame frame = new GUI();
frame.setLayout(null);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setTitle( "Minesweeper" );
frame.setVisible( true );
}
public void actionPerformed( ActionEvent e ) {
//eerst controleren we of de knop daadwerkelijk is ingedrukt
if(e.getSource() == start) {
//vervolgens definieeren we de tekst die is ingevuld in de vlakken als String
String Trijen = rijen.getText();
String TKolommen = kolommen.getText();
//we zetten ook de standaardwaardes vast voor de rijen en kolommen
int rij = 16;
int kolom = 33;
//---RIJ----
//eerst wordt de input omgezet in een int
try {
rij = Integer.parseInt(Trijen);
if (rij > 20) {
rij = 16; //zet terug naar standaard
throw new TeGrootException(); //te groot
} else if (rij < 5) {
rij = 16; //zet terug naar standaard
throw new TeKleinException(); //te klein
}
} catch(NumberFormatException nferij) {
rij = 16; //zet terug naar standaard
//een popup
JOptionPane.showMessageDialog(
this,
"Uw invoer voor de rijen:" + Trijen + " is geen getal, het is aangepast naar 16",
"Invoerfout",
JOptionPane.ERROR_MESSAGE
);
} catch(TeGrootException tgerij) {
//een popup
JOptionPane.showMessageDialog(
this,
"Uw invoer voor de rijen:" + Trijen + " is te groot, de maximale waarde is 20. Het is aangepast naar 16",
"Invoerfout",
JOptionPane.ERROR_MESSAGE
);
} catch(TeKleinException tkerij) {
//een popup
JOptionPane.showMessageDialog(
this,
"Uw invoer voor de rijen:" + Trijen + " is te klein, de minimale waarde is 5. Het is aangepast naar 16",
"Invoerfout",
JOptionPane.ERROR_MESSAGE
);
}
//----KOLOM----
//identiek aan rij, maar rij = kolom en standaardwaarde = 33
try {
kolom = Integer.parseInt(TKolommen);
if (kolom > 40) {
kolom = 33;
throw new TeGrootException();
} else if (kolom < 5) {
kolom = 33;
throw new TeKleinException();
}
} catch(NumberFormatException nfekolom) {
kolom = 33;
JOptionPane.showMessageDialog(
this,
"Uw invoer voor de kolommen:" + TKolommen + " is geen getal, het is aangepast naar 33.",
"Invoerfout",
JOptionPane.ERROR_MESSAGE
);
} catch(TeGrootException tgekolom) {
JOptionPane.showMessageDialog(
this,
"Uw invoer voor de kolommen:" + TKolommen + " is te groot, de maximale waarde is 40. Het is aangepast naar 33",
"Invoerfout",
JOptionPane.ERROR_MESSAGE
);
} catch(TeKleinException tkekolom) {
JOptionPane.showMessageDialog(
this,
"Uw invoer voor de kolommen:" + TKolommen + " is te klein, de minimale waarde is 5. Het is aangepast naar 33",
"Invoerfout",
JOptionPane.ERROR_MESSAGE
);
}
dispose(); //verwijdert het huidige scherm
new GUIMinesweeper(rij,kolom); //maakt het nieuwe scherm met Minesweeper
}
}
}