Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. package com.bruno.javaserialgui.ventanas;
  2.  
  3. @SuppressWarnings("serial")
  4. public class ventanaPrincipal extends JFrame implements ActionListener{
  5.  
  6. JPanel jpanel;
  7. JFrame ventanaPrincipal;
  8. Dimension d;
  9. ImageIcon im;
  10. public static JTextField numSerial;
  11. JButton bt;
  12.  
  13. @SuppressWarnings("unused")
  14. public static void main(String[] args){
  15. ventanaPrincipal ventanaPrincipal = new ventanaPrincipal("Java Serial Gui");
  16. }
  17.  
  18. public ventanaPrincipal(String titulo){
  19. jpanel = new JPanel();
  20. jpanel.setLayout(null);
  21. jpanel.setBackground(Color.lightGray);
  22.  
  23. ventanaPrincipal = new JFrame(titulo);
  24. d = new Dimension(); //Objeto para obtener el tamaño de la ventana
  25. //im = new ImageIcon("ruta");
  26. numSerial = new JTextField();
  27. numSerial.setBounds(new Rectangle(25, 15, 230, 21));
  28. numSerial.setText("Ingrese el serial");
  29. numSerial.setEditable(true);
  30. numSerial.setHorizontalAlignment(JTextField.LEFT);
  31.  
  32. bt = new JButton("Comprobar serial");
  33. bt.setLayout(null);
  34. bt.setBounds(50, 50, 160, 30);
  35. bt.addActionListener(this);
  36. bt.setEnabled(true);
  37.  
  38. ventanaPrincipal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39. //ventanaPrincipal.setIconImage(im.getImage()); //Añadimos un icono a la ventan
  40. ventanaPrincipal.setResizable(true); //Configuramos si se puede redimensionar la ventana
  41. ventanaPrincipal.setLocation((int) ((d.getWidth()/2)+290), 50); //para ubicar inicialmente donde se muestra la ventana (x,y)
  42. ventanaPrincipal.setSize(290, 150); //Configurando el tamaño de la ventana
  43. ventanaPrincipal.setVisible(true);
  44. ventanaPrincipal.add(jpanel);
  45. ventanaPrincipal.getContentPane();
  46. jpanel.add(numSerial, null);
  47. jpanel.add(bt, null);
  48. }
  49.  
  50. @Override
  51. public void actionPerformed(ActionEvent e) {
  52. if(e.getSource() == bt){
  53. comprobarSerial.main();
  54. }else{
  55. JOptionPane.showMessageDialog(null, "Error");
  56. }
  57.  
  58. }
  59. }
  60.  
  61. package com.bruno.javaserialgui;
  62.  
  63. public static void main(){
  64. String numSerial = ventanaPrincipal.numSerial.getText();
  65.  
  66. //A este if no entra el programa
  67.  
  68. if(numSerial == "test"){
  69. System.out.println("hola");
  70. VentanaAceptado.init();
  71. }
  72.  
  73. try{
  74. Class.forName("com.mysql.jdbc.Driver");
  75. Connection con = DriverManager.getConnection("jdbc:mysql://localhost/seriales","root","");
  76. PreparedStatement ps = con.prepareStatement(
  77. "SELECT usado FROM serial WHERE serial = ?",
  78. ResultSet.TYPE_SCROLL_SENSITIVE, // Esto asegura que el ResultSet pueda ser recorrido en cualquier dirección (adelante o atrás)
  79. ResultSet.CONCUR_READ_ONLY // Esto asegura que el ResultSet sea de sólo lectura
  80. );
  81. ps.setString(1, numSerial);
  82. ResultSet rs = ps.executeQuery();
  83. rs.beforeFirst(); // Coloca el cursor ANTES de la primera fila del ResultSet
  84. // (para esto es que se requiere la opción TYPE_SCROLL_SENSITIVE)
  85.  
  86.  
  87. if(rs.first()){
  88. if(rs.getInt("usado") == 0){ //0 no usado 1 usado
  89. VentanaAceptado.init();
  90. }else if(rs.getInt("usado") == 1) {
  91. VentanaDenegado.init();
  92. }else{
  93. Exception ex = new Exception("Error");
  94. throw ex;
  95.  
  96. }
  97. }
  98. }catch(Exception ex){
  99. JOptionPane.showMessageDialog(null, ex);
  100. ex.printStackTrace(System.out);
  101. }
  102. }
  103.  
  104. package com.bruno.javaserialgui.ventanas;
  105.  
  106. @SuppressWarnings("serial")
  107. public class VentanaAceptado extends JFrame{
  108. JPanel jpanel;
  109. JFrame ventanaAceptado;
  110. Dimension d;
  111. JComboBox<String> cb;
  112. JTextField tf;
  113.  
  114. public static void init(){
  115. @SuppressWarnings("unused")
  116. VentanaAceptado ventanaAceptado = new VentanaAceptado("Serial aceptado");
  117. }
  118.  
  119. public VentanaAceptado(String titulo){
  120. jpanel = new JPanel();
  121. jpanel.setLayout(null);
  122. jpanel.setBackground(Color.lightGray);
  123.  
  124. cb.setBounds(HEIGHT, WIDTH, SOMEBITS, HEIGHT);
  125. cb.addItem("Opcion 1");
  126. cb.addItem("Opcion 2");
  127. cb.addActionListener(new ActionListener(){
  128. @Override
  129. public void actionPerformed(ActionEvent e){
  130. tf.setText(cb.getSelectedItem().toString());
  131. }
  132. });
  133.  
  134. ventanaAceptado = new JFrame(titulo);
  135. d = new Dimension(); //Objeto para obtener el tamaño de la ventana
  136.  
  137. ventanaAceptado.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  138. //ventanaPrincipal.setIconImage(im.getImage()); //Añadimos un icono a la ventan
  139. ventanaAceptado.setResizable(true); //Configuramos si se puede redimensionar la ventana
  140. ventanaAceptado.setLocation((int) ((d.getWidth()/2)+290), 50); //para ubicar inicialmente donde se muestra la ventana (x,y)
  141. ventanaAceptado.setSize(290, 150); //Configurando el tamaño de la ventana
  142. ventanaAceptado.setVisible(true);
  143. ventanaAceptado.add(jpanel);
  144. ventanaAceptado.getContentPane();
  145. jpanel.add(cb, null);
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement