Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.01 KB | None | 0 0
  1. import javax.swing.Box;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import javax.swing.JOptionPane;
  7. import javax.swing.JFrame;
  8. import java.sql.*;
  9. import javax.swing.JPanel;
  10. import javax.swing.SwingConstants;
  11. import javax.swing.Icon;
  12. import javax.swing.ImageIcon;
  13.  
  14.  
  15.  
  16. public class FrontScreen implements ActionListener
  17. {
  18. private static JRadioButton rb1, rb2, rb3;
  19. private static final JFrame f = new JFrame("Welcome to Ballon d'or , created by Darren Estcourt");
  20. private static JButton b;
  21. private static JPanel myFirstPanel;
  22. private static JLabel ballondor , trophylabel;
  23. private static String firstName , surname;
  24.  
  25. public FrontScreen()
  26. {
  27.  
  28. }
  29. public void CreateAndShowFrontScreen()
  30. {
  31.  
  32. f.setExtendedState(JFrame.MAXIMIZED_BOTH);
  33. f.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  34.  
  35. myFirstPanel = new JPanel();
  36. myFirstPanel.setBackground(Color.GREEN);
  37. f.add(myFirstPanel, BorderLayout.CENTER);
  38.  
  39. ballondor = new JLabel("Ballon d'or");
  40. ballondor.setToolTipText("Ballon d'or tooltip");
  41. myFirstPanel.add(ballondor);
  42.  
  43. Icon trophy = new ImageIcon(getClass().getResource("Ballondor.jpg"));
  44. trophylabel = new JLabel("Please choose an option, then click OK" , trophy, SwingConstants.RIGHT);
  45. trophylabel.setToolTipText("Trophy Label tooltip");
  46. myFirstPanel.add(trophylabel);
  47.  
  48.  
  49. rb1 = new JRadioButton("Start New Game");
  50. rb2 = new JRadioButton("Load Game");
  51. rb3 = new JRadioButton("Quit");
  52.  
  53. Box box1 = Box.createVerticalBox();
  54. box1.add(rb1); // TEST VERTICAL RADIO BUTTONS !
  55. box1.add(rb2);
  56. box1.add(rb3);
  57.  
  58.  
  59. myFirstPanel.add(box1);
  60.  
  61.  
  62. ButtonGroup bg=new ButtonGroup();
  63. bg.add(rb1);
  64. bg.add(rb2);
  65. bg.add(rb3);
  66.  
  67. b = new JButton("OK");
  68. myFirstPanel.add(b);
  69.  
  70. b.addActionListener(this);
  71.  
  72. f.addWindowListener(new WindowAdapter()
  73. {
  74.  
  75. @Override public void windowClosing(WindowEvent e)
  76. {
  77. if(JOptionPane.showConfirmDialog(f, "Are you sure ?" , "Warning", JOptionPane.YES_NO_OPTION ) == JOptionPane.YES_OPTION)
  78. {
  79. f.setVisible(false);
  80. f.dispose();
  81. }
  82. else{
  83. }
  84. }
  85. });
  86. } // OUT OF SCOPE !!!!!!
  87.  
  88.  
  89. @Override public void actionPerformed(ActionEvent e)
  90. {
  91. if(rb1.isSelected())
  92. {
  93. myFirstPanel.setVisible(false);
  94. ManagerName panel = new ManagerName();
  95. f.add(panel);
  96. f.pack();
  97.  
  98. panel.setVisible(true);
  99.  
  100. }
  101. if(rb2.isSelected())
  102. {
  103. JOptionPane.showMessageDialog(f ,"Which saved game would you like to load?", "Load game", JOptionPane.QUESTION_MESSAGE);
  104. }
  105. if(rb3.isSelected())
  106. {
  107. System.exit(0);
  108. }
  109.  
  110. }
  111.  
  112. public static void main(String[] args) throws SQLException , ClassNotFoundException, InstantiationException, IllegalAccessException
  113. {
  114.  
  115. SwingUtilities.invokeLater(new Runnable()
  116. {
  117. public void run()
  118. {
  119. FrontScreen test = new FrontScreen();
  120. test.CreateAndShowFrontScreen();
  121. f.pack();
  122. f.setVisible(true);
  123.  
  124. }
  125. });
  126.  
  127. Database passFirstNameObject = new Database(firstName , surname);
  128. passFirstNameObject.setFirstName(firstName);
  129.  
  130. } // end main
  131. }
  132.  
  133. ------------------------------------------------------------------------------
  134.  
  135. import java.awt.Color;
  136. import java.awt.event.*;
  137. import javax.swing.*;
  138. import javax.swing.JOptionPane;
  139. import javax.swing.JPanel;
  140. import javax.swing.JLabel;
  141. import java.sql.*;
  142.  
  143.  
  144. /**
  145. *
  146. * @author Darren Estcourt
  147. */
  148.  
  149. public class ManagerName extends JPanel implements ActionListener
  150. {
  151.  
  152. private final JLabel firstNameLabel , surnameLabel;
  153. private final JButton confirmNames , quit;
  154. private String firstName , surname;
  155.  
  156.  
  157. private final JTextField myTextField , myTextField2;
  158.  
  159.  
  160.  
  161. public ManagerName()
  162. {
  163.  
  164. setBackground(Color.GREEN);
  165.  
  166.  
  167. firstNameLabel = new JLabel("Please enter your first name");
  168.  
  169. add(firstNameLabel);
  170.  
  171.  
  172. myTextField = new JTextField(20); // or use 20 columns
  173. add(myTextField);
  174. myTextField.addActionListener(this);
  175.  
  176. surnameLabel = new JLabel("Please enter your surname");
  177. add(surnameLabel);
  178.  
  179. myTextField2 = new JTextField(20);
  180. add(myTextField2);
  181. myTextField2.addActionListener(this);
  182.  
  183. confirmNames = new JButton("Submit Names");
  184. confirmNames.addActionListener(this);
  185. add(confirmNames);
  186.  
  187. quit = new JButton("Quit");
  188. quit.addActionListener(this);
  189. add(quit);
  190.  
  191. }
  192.  
  193.  
  194.  
  195. @Override public void actionPerformed(ActionEvent e)
  196. {
  197. if(e.getSource()==confirmNames)
  198. {
  199.  
  200. firstName = myTextField.getText();
  201. surname = myTextField2.getText();
  202. try{
  203. Database passFirstNameObject = new Database(firstName , surname);
  204. passFirstNameObject.setFirstName(firstName);
  205. passFirstNameObject.setSurname(surname);
  206. Database.databaseMethod(passFirstNameObject);
  207. }
  208. catch(SQLException i)
  209. {
  210. System.err.println("Got an exception! ");
  211. }
  212. catch(ClassNotFoundException a)
  213. {
  214. System.err.println("Got an exception! ");
  215. }
  216. catch(InstantiationException b)
  217. {
  218. System.err.println("Got an exception! ");
  219. }
  220. catch(IllegalAccessException c)
  221. {
  222. System.err.println("Got an exception! ");
  223. }
  224. JOptionPane.showMessageDialog(null, "Your name is : " + firstName + " " + surname);
  225. this.setVisible(false);
  226. NewGame callNewGamePanel = new NewGame();
  227. this.add(callNewGamePanel);
  228.  
  229. callNewGamePanel.setVisible(true);
  230. }
  231.  
  232. if(e.getSource()==quit)
  233. {
  234. System.exit(0);
  235. }
  236.  
  237. }
  238.  
  239.  
  240. }
  241. ------------------------------------------------------------------------
  242.  
  243. import java.awt.event.*;
  244. import javax.swing.*;
  245. import javax.swing.JOptionPane;
  246. import javax.swing.JLabel;
  247. import java.awt.Color;
  248.  
  249.  
  250. public class NewGame extends JPanel implements ActionListener
  251. {
  252. private final String[] premierLeagueClubs = {"Arsenal" , "Bournemouth" , "Burnley" , "Chelsea" , "Crystal Palace" ,
  253. "Everton" , "Hull City" , "Leicester City" , "Liverpool" , "Manchester United" , "Manchester City" , "Middlesborough" ,
  254. "Southampton" , "Stoke City", "Sunderland", "Swansea City", "Tottenham Hotspur" , "Watford" , "West Brom" , "West Ham"};
  255. private final JRadioButton[] rb = new JRadioButton[20];
  256.  
  257. JButton confirmTeam , quit;
  258. String teamName;
  259. JLabel label1;
  260.  
  261. public NewGame()
  262. {
  263. setBackground(Color.GREEN);
  264.  
  265. label1 = new JLabel("Please choose a team");
  266. add(label1);
  267.  
  268. for (int i = 0; i < premierLeagueClubs.length; i++)
  269. {
  270. rb[i] = new JRadioButton(premierLeagueClubs[i]);
  271. rb[i].setActionCommand(premierLeagueClubs[i]);
  272. }
  273.  
  274. confirmTeam = new JButton("OK");
  275. confirmTeam.addActionListener(this);
  276. add(confirmTeam);
  277.  
  278. for(int j=0; j < 20; j++)
  279. {
  280. add(rb[j]);
  281. }
  282.  
  283. ButtonGroup bg=new ButtonGroup();
  284.  
  285. int startvalueBG;
  286. int endvalueBG=19;
  287. for(startvalueBG=0; startvalueBG <= endvalueBG; startvalueBG++)
  288. {
  289. bg.add(rb[startvalueBG]);
  290. }
  291.  
  292.  
  293.  
  294. quit = new JButton("Quit");
  295. quit.addActionListener(this);
  296. add(quit);
  297.  
  298.  
  299. }
  300.  
  301.  
  302. @Override public void actionPerformed(ActionEvent e)
  303. {
  304.  
  305. if(e.getSource()==confirmTeam)
  306. {
  307. for(int i=0; i < 20; i++){
  308. if(rb[i].isSelected())
  309. {
  310. JOptionPane.showMessageDialog(null , "You chose:" + premierLeagueClubs[i]);
  311.  
  312. }
  313. }
  314. if(e.getSource()==quit)
  315. {
  316. System.exit(0);
  317. }
  318. }
  319.  
  320.  
  321. }
  322. }
  323. ------------------------------------------------------------------------------
  324.  
  325. import java.sql.*;
  326.  
  327. public class Database
  328. {
  329.  
  330. private String firstName , surname;
  331.  
  332. public Database(String firstName , String surname)
  333. {
  334. this.firstName = firstName;
  335. this.surname = surname;
  336. }
  337. public void setFirstName(String firstName)
  338. {
  339. this.firstName = firstName;
  340. }
  341. public String getFirstName()
  342. {
  343. return firstName;
  344. }
  345. public void setSurname(String surname)
  346. {
  347. this.surname = surname;
  348. }
  349. public String getSurname()
  350. {
  351. return surname;
  352. }
  353.  
  354. public static void databaseMethod(Database passFirstNameObject) throws SQLException , ClassNotFoundException, InstantiationException,IllegalAccessException
  355. {
  356.  
  357.  
  358.  
  359. String myDriver = "com.mysql.jdbc.Driver";
  360. String myUrl = "jdbc:mysql://localhost:3306/ballondor?autoReconnect=true&useSSL=false";
  361.  
  362. Connection connOne = DriverManager.getConnection(myUrl, "root", "Lucia290907");
  363. Connection connTwo = DriverManager.getConnection(myUrl, "root", "Lucia290907");
  364.  
  365. Statement st1 = connOne.createStatement();
  366. Statement st2 = connTwo.createStatement();
  367. Class.forName(myDriver).newInstance();
  368.  
  369.  
  370.  
  371. JOptionPane.showMessageDialog(null,"List of team names" ,TeamName, JOptionPane.QUESTION_MESSAGE);
  372.  
  373. System.out.println("The first attempt :" + passFirstNameObject.getFirstName() + passFirstNameObject.getSurname());
  374. st2.executeUpdate("insert into managername (firstName,surname) values('"+passFirstNameObject.getFirstName()+"' , '"+passFirstNameObject.getSurname()+"')"); // UPDATING MYSQL DATABASE
  375. // } // end while loop
  376.  
  377. st1.close();
  378.  
  379.  
  380.  
  381.  
  382. }
  383.  
  384.  
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement