Advertisement
cd62131

PressButtonToOrder

Aug 13th, 2014
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.32 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Container;
  4. import java.awt.GridLayout;
  5. import java.awt.event.MouseAdapter;
  6. import java.awt.event.MouseEvent;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.List;
  10.  
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JPanel;
  14. import javax.swing.SwingUtilities;
  15.  
  16. class MainFrame extends JFrame {
  17.   private static final long serialVersionUID = 1L;
  18.   private List<NumberdButton> numbers = new ArrayList<NumberdButton>();
  19.   private GridLayout grid = new GridLayout(5, 5);
  20.   private boolean[] pressed = new boolean[5 * 5 + 1];
  21.  
  22.   public MainFrame(String name) {
  23.     super(name);
  24.     setResizable(false);
  25.   }
  26.  
  27.   public void setButtons(final Container pane) {
  28.     initPressed();
  29.     initNumbers();
  30.     final JPanel p = new JPanel(grid);
  31.     for (final NumberdButton b : numbers) {
  32.       p.add(b);
  33.       b.addMouseListener(new MouseAdapter() {
  34.         @Override
  35.         public void mouseClicked(MouseEvent e) {
  36.           if (pressed[b.getNumber() - 1]) {
  37.             b.setBackground(Color.BLACK);
  38.             b.setEnabled(false);
  39.             pressed[b.getNumber()] = true;
  40.           }
  41.         }
  42.       });
  43.     }
  44.     pane.add(p, BorderLayout.CENTER);
  45.   }
  46.  
  47.   private void initNumbers() {
  48.     for (int i = 1; i < 26; i++) {
  49.       numbers.add(new NumberdButton(i));
  50.     }
  51.     Collections.shuffle(numbers);
  52.   }
  53.  
  54.   private void initPressed() {
  55.     for (int i = 0; i < pressed.length; i++)
  56.       pressed[i] = false;
  57.     pressed[0] = true;
  58.   }
  59.  
  60.   public static void create(String name) {
  61.     MainFrame frame = new MainFrame(name);
  62.     frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
  63.     frame.setButtons(frame.getContentPane());
  64.     frame.pack();
  65.     frame.setVisible(true);
  66.   }
  67. }
  68.  
  69. class NumberdButton extends JButton {
  70.   private static final long serialVersionUID = 1L;
  71.   private final int number;
  72.  
  73.   public NumberdButton(int number) {
  74.     super("" + number);
  75.     this.number = number;
  76.   }
  77.  
  78.   public int getNumber() {
  79.     return number;
  80.   }
  81. }
  82.  
  83. public class PressButtonToOrder {
  84.   public static void main(String[] args) {
  85.     SwingUtilities.invokeLater(new Runnable() {
  86.       @Override
  87.       public void run() {
  88.         MainFrame.create("PressButtonToOrder");
  89.       }
  90.     });
  91.   }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement