-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartMenu.java
More file actions
214 lines (186 loc) · 6.77 KB
/
StartMenu.java
File metadata and controls
214 lines (186 loc) · 6.77 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
/**
* This class is the Start Menu, which asks for the location of the directory
* which contains the program files and the image and problem files to set the
* space in which the code functions and accesses problems.
*
* @author Krishnkumar Bhattaram
* @version May 23, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
public class StartMenu
{
/**
* Holds the GUI elements
*/
private JFrame frame;
/**
* The text field for the problem name
*/
private JTextField namepathfield;
/**
* The container of the frame
*/
private Container c;
/**
* The text label to warn that the fields are not completed
*/
private JLabel warntxt;
/**
* The text label to warn that the name is not in problem
*/
private JLabel notfoundtxt;
/**
* The statistics from this set
*/
private Statistics stats;
/**
* Label to allow user to choose path
*/
private String pathFileChooser;
/**
* The constructor, which makes visible the frame and adds the enter button
* and the single text field for the pathname of the folder in which the
* program is located.
*
* @param statistics
* The statistics for this set (from QuickQuiz)
*/
public StartMenu( Statistics statistics )
{
stats = statistics;
JLabel nametxt = new JLabel(
"Enter the directory of the program file to begin (ex. /Users/JohnDoe/Desktop/QuickQuiz)" );
nametxt.setFont( new Font( "font", Font.PLAIN, 10 ) );
nametxt.setBounds( 200, 180, 500, 20 );
nametxt.setHorizontalAlignment( JLabel.LEFT );
nametxt.setVerticalAlignment( JLabel.TOP );
namepathfield = new JTextField();
namepathfield.setBounds( 200, 200, 400, 20 );
JButton pathname = new JButton( "Enter" );
pathname.setBounds( 600, 200, 100, 20 );
pathname.addActionListener( new PathnameListener() );
JButton filechooser = new JButton( "Choose Directory" );
filechooser.setBounds( 75, 200, 125, 20 );
filechooser.addActionListener( new FileListener() );
JLabel text = new JLabel( "Welcome to Quick Quiz!" );
text.setFont( new Font( "font", Font.PLAIN, 30 ) );
text.setHorizontalAlignment( JLabel.CENTER );
text.setBounds( 100, 30, 600, 100 );
frame = new JFrame( "Fîzîk" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
c = frame.getContentPane();
frame.setLayout( null );
frame.setBounds( 0, 0, 800, 600 );
warntxt = new JLabel( "Please complete the required field!" );
warntxt.setFont( new Font( "font", Font.PLAIN, 15 ) );
warntxt.setBounds( 200, 500, 500, 20 );
warntxt.setHorizontalAlignment( JLabel.LEFT );
warntxt.setVerticalAlignment( JLabel.TOP );
warntxt.setVisible( false );
notfoundtxt = new JLabel( "File not found! Please check the fields" );
notfoundtxt.setFont( new Font( "font", Font.PLAIN, 15 ) );
notfoundtxt.setBounds( 200, 500, 500, 20 );
notfoundtxt.setHorizontalAlignment( JLabel.LEFT );
notfoundtxt.setVerticalAlignment( JLabel.TOP );
notfoundtxt.setVisible( false );
ImageIcon icon = new ImageIcon( getClass().getResource("/images/Icon.png"));
Image image = icon.getImage();
Image newImage = image.getScaledInstance( 150, 75, Image.SCALE_DEFAULT );
icon.setImage( newImage );
JLabel imageLabel = new JLabel();
imageLabel.setBounds( 300, 100, 150, 75 );
imageLabel.setIcon(icon);
imageLabel.setVerticalTextPosition( JLabel.BOTTOM );
imageLabel.setHorizontalTextPosition( JLabel.CENTER );
namepathfield.setText( getClass().getResource("").toString().substring(5) );
c.add( text );
c.add( pathname );
c.add( warntxt );
c.add( notfoundtxt );
c.add( filechooser );
c.add( namepathfield );
c.add( nametxt );
c.add(imageLabel);
frame.setResizable( false );
frame.setVisible( true );
}
/**
* Handles when the enter button is clicked; when clicked, checks if all
* conditions are satisfied (if all fields are entered and all pathnames
* exist) and displays the appropriate warnings if not. Otherwise, the frame
* closes itself and opens MainMenu.
*
* @author Krishnakumar Bhattaram
* @version May 19, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class PathnameListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
if ( namepathfield.getText().trim().isEmpty() )
{
warntxt.setVisible( true );
notfoundtxt.setVisible( false );
return;
}
else
{
String pathproblems = namepathfield.getText() + "/ProblemFile/problems.txt";
try
{
Scanner readIn = new Scanner( new File( pathproblems ) );
}
catch ( FileNotFoundException exc )
{
warntxt.setVisible( false );
notfoundtxt.setVisible( true );
return;
}
MainMenu main = new MainMenu( new ProblemDatabase( pathproblems ), stats );
frame.dispose();
}
}
}
/**
* Opens the system's file explorer to allow selection of the directory
* which contains the image files and problem set.
*
* @author Krishnakumar Bhattaram
* @version May 23, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: Mykong.com
*/
private class FileListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
JFileChooser filechooser = new JFileChooser(
FileSystemView.getFileSystemView().getHomeDirectory() );
filechooser.setDialogTitle( "Choose Directory of Program Files" );
filechooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
int ret = filechooser.showOpenDialog( null );
if ( ret == JFileChooser.APPROVE_OPTION )
{
File file = filechooser.getSelectedFile();
pathFileChooser = file.getAbsolutePath();
namepathfield.setText( pathFileChooser );
}
}
}
}