Advertisement
Madain

Untitled

Aug 18th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Graphics;
  3. import java.awt.Color;
  4. import java.awt.EventQueue;
  5. import java.util.Random;
  6.  
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9. import javax.swing.border.EmptyBorder;
  10. import javax.swing.JLabel;
  11. import javax.swing.JSpinner;
  12. import javax.swing.SpinnerNumberModel;
  13. import javax.swing.JComboBox;
  14. import javax.swing.DefaultComboBoxModel;
  15. import javax.swing.JButton;
  16. import java.awt.event.ItemListener;
  17. import java.awt.event.ItemEvent;
  18. import javax.swing.event.ChangeListener;
  19. import javax.swing.event.ChangeEvent;
  20. import java.awt.event.ActionListener;
  21. import java.awt.event.ActionEvent;
  22.  
  23. public class Aduana extends JFrame {
  24.  
  25. private JPanel contentPane;
  26. private JSpinner spinner;
  27. private JComboBox comboBox;
  28.  
  29. /**
  30. * Launch the application.
  31. */
  32. private int x;
  33. public static void main(String[] args) {
  34. EventQueue.invokeLater(new Runnable() {
  35. public void run() {
  36. try {
  37. Aduana frame = new Aduana();
  38. frame.setVisible(true);
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. });
  44. }
  45.  
  46. /**
  47. * Create the frame.
  48. */
  49. public Aduana() {
  50. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51. setBounds(100, 100, 535, 376);
  52. contentPane = new JPanel();
  53. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  54. setContentPane(contentPane);
  55. contentPane.setLayout(null);
  56.  
  57. JLabel lblCantidadDeBultos = new JLabel("Cantidad de bultos");
  58. lblCantidadDeBultos.setBounds(34, 53, 90, 14);
  59. contentPane.add(lblCantidadDeBultos);
  60.  
  61. JLabel lblProcedencia = new JLabel("Procedencia");
  62. lblProcedencia.setBounds(264, 53, 58, 14);
  63. contentPane.add(lblProcedencia);
  64.  
  65. spinner = new JSpinner();
  66. spinner.addChangeListener(new ChangeListener() {
  67. public void stateChanged(ChangeEvent arg0) {
  68. repaint();
  69. }
  70. });
  71. spinner.setModel(new SpinnerNumberModel(0, 0, 10, 1));
  72. spinner.setBounds(34, 78, 90, 20);
  73. contentPane.add(spinner);
  74.  
  75. comboBox = new JComboBox();
  76. comboBox.addItemListener(new ItemListener() {
  77. public void itemStateChanged(ItemEvent arg0) {
  78. repaint();
  79. }
  80. });
  81. comboBox.setModel(new DefaultComboBoxModel(new String[] {"Interior", "Exterior"}));
  82. comboBox.setBounds(264, 78, 113, 20);
  83. contentPane.add(comboBox);
  84.  
  85. JButton btnSortear = new JButton("Sortear");
  86. btnSortear.addActionListener(new ActionListener() {
  87. public void actionPerformed(ActionEvent arg0) {
  88. int bultos=Integer.parseInt(spinner.getValue().toString());
  89.  
  90. /* Si cada vez que le das al botón vuelves a inicializar a 0 revisados y no revisados, es como si siempre lo hicieras por primera vez. Deberías crear una variable de control para saber si es la primera vez que se hace clic en el botón (algo así como boolean primerClick = true; y ponerlo a false en cuanto hagas la primera ejecución. Luego, pones la inicialización de los dos valores dentro del if que lo controla y ya.*/
  91.  
  92. int revisados=0;
  93. int norevisados=0;
  94. if(bultos!=0)
  95. if(bultos>5) {
  96. revisados=revisados+bultos;
  97. repaint();
  98. } else if(bultos<=5){
  99. Random rnd=new Random();
  100. int x=(int)(rnd.nextDouble()*3+1);
  101. if(x==1) {
  102. revisados=revisados+bultos;
  103. // Si haces el repaint antes del setTitle, no debería hacerlo bien (creo, no domino el framework)
  104. setTitle("Revisados:"+revisados+" No revisados:"+norevisados);
  105. repaint();
  106. } else if(x==2 || x==3) {
  107. norevisados=norevisados+bultos;
  108. // Si haces el repaint antes del setTitle, no debería hacerlo bien (creo, no domino el framework)
  109.  
  110. setTitle("Revisados:"+revisados+" No revisados:"+norevisados);
  111. repaint();
  112. }
  113. }
  114. }
  115. });
  116. btnSortear.setBounds(35, 207, 89, 23);
  117. contentPane.add(btnSortear);
  118. }
  119.  
  120. public void paint(Graphics g) {
  121. super.paint(g);
  122. int bultos=Integer.parseInt(spinner.getValue().toString());
  123. if(bultos!=0)
  124. if(bultos>5) {
  125. g.setColor(Color.red);
  126. g.fillOval(60, 275, 50, 50);
  127. } else if(bultos<=5){
  128. if(x==1) {
  129. g.setColor(Color.red);
  130. g.fillOval(60,275,50,50);
  131. } else if(x==2 || x==3) {
  132. g.setColor(Color.green);
  133. g.fillOval(60,275,50,50);
  134. }
  135. }
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement