Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.GridLayout;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.SwingUtilities;
- class MainFrame extends JFrame {
- private static final long serialVersionUID = 1L;
- private List<NumberdButton> numbers = new ArrayList<NumberdButton>();
- private GridLayout grid = new GridLayout(5, 5);
- private boolean[] pressed = new boolean[5 * 5 + 1];
- public MainFrame(String name) {
- super(name);
- setResizable(false);
- }
- public void setButtons(final Container pane) {
- initPressed();
- initNumbers();
- final JPanel p = new JPanel(grid);
- for (final NumberdButton b : numbers) {
- p.add(b);
- b.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- if (pressed[b.getNumber() - 1]) {
- b.setBackground(Color.BLACK);
- b.setEnabled(false);
- pressed[b.getNumber()] = true;
- }
- }
- });
- }
- pane.add(p, BorderLayout.CENTER);
- }
- private void initNumbers() {
- for (int i = 1; i < 26; i++) {
- numbers.add(new NumberdButton(i));
- }
- Collections.shuffle(numbers);
- }
- private void initPressed() {
- for (int i = 0; i < pressed.length; i++)
- pressed[i] = false;
- pressed[0] = true;
- }
- public static void create(String name) {
- MainFrame frame = new MainFrame(name);
- frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
- frame.setButtons(frame.getContentPane());
- frame.pack();
- frame.setVisible(true);
- }
- }
- class NumberdButton extends JButton {
- private static final long serialVersionUID = 1L;
- private final int number;
- public NumberdButton(int number) {
- super("" + number);
- this.number = number;
- }
- public int getNumber() {
- return number;
- }
- }
- public class PressButtonToOrder {
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- MainFrame.create("PressButtonToOrder");
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement