Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class gra extends JFrame implements ActionListener{
- private String gracz = "O";
- private JButton nowaGra;
- private JButton zakoncz;
- private JLabel wynik;
- private JButton sprawdz;
- private JButton[] b = new JButton[9];
- private boolean wygrana(String s)
- {
- if( (b[0].getLabel() == s && b[1].getLabel() == s && b[2].getLabel() == s) ||
- (b[3].getLabel() == s && b[4].getLabel() == s && b[5].getLabel() == s) ||
- (b[6].getLabel() == s && b[7].getLabel() == s && b[8].getLabel() == s) ||
- (b[0].getLabel() == s && b[4].getLabel() == s && b[8].getLabel() == s) ||
- (b[2].getLabel() == s && b[4].getLabel() == s && b[6].getLabel() == s) ||
- (b[0].getLabel() == s && b[3].getLabel() == s && b[6].getLabel() == s) ||
- (b[1].getLabel() == s && b[4].getLabel() == s && b[7].getLabel() == s) ||
- (b[2].getLabel() == s && b[5].getLabel() == s && b[8].getLabel() == s) )
- return true;
- return false;
- }
- private JPanel plansza()
- {
- JPanel tlo = new JPanel();
- tlo.setBackground(Color.LIGHT_GRAY);
- tlo.setPreferredSize(new Dimension(500, 100));
- tlo.setLayout(new BoxLayout(tlo, BoxLayout.Y_AXIS));
- JPanel menu = new JPanel();
- nowaGra = new JButton("Nowa gra");
- nowaGra.setBackground(Color.GRAY);
- nowaGra.setFont(new Font("Comic Sans", Font.BOLD, 20));
- nowaGra.setPreferredSize(new Dimension(150, 50));
- nowaGra.addActionListener(this);
- menu.add(nowaGra);
- sprawdz = new JButton("Wynik");
- sprawdz.setPreferredSize(new Dimension(150, 50));
- sprawdz.setBackground(Color.GRAY);
- sprawdz.setFont(new Font("Comic Sans", Font.BOLD, 20));
- sprawdz.addActionListener(this);
- menu.add(sprawdz);
- zakoncz = new JButton("Zakończ");
- zakoncz.setPreferredSize(new Dimension(150, 50));
- zakoncz.setBackground(Color.GRAY);
- zakoncz.setFont(new Font("Comic Sans", Font.BOLD, 20));
- zakoncz.addActionListener(this);
- menu.add(zakoncz);
- tlo.add(menu);
- JPanel mp = new JPanel();
- for(int i=0;i<9;i++)
- {
- b[i] = new JButton();
- b[i].addActionListener(this);
- b[i].setPreferredSize(new Dimension(20,20));
- b[i].setFont(new Font("Comic Sans", Font.BOLD, 40));
- b[i].setForeground(Color.BLACK);
- b[i].setBackground(Color.LIGHT_GRAY);
- }
- mp.setLayout(new GridLayout(3,3));
- mp.add(b[0]); mp.add(b[1]); mp.add(b[2]);
- mp.add(b[3]); mp.add(b[4]); mp.add(b[5]);
- mp.add(b[6]); mp.add(b[7]); mp.add(b[8]);
- tlo.add(mp);
- wynik = new JLabel();
- wynik.setPreferredSize(new Dimension(100,50));
- wynik.setFont(new Font("Comic Sans", Font.BOLD, 40));
- menu.add(wynik);
- return tlo;
- }
- public gra()
- {
- super();
- this.getContentPane().add(this.plansza());
- }
- public static void okno()
- {
- gra g = new gra();
- g.setDefaultCloseOperation(EXIT_ON_CLOSE);
- g.pack();
- g.setLocation(400, 0);
- g.setTitle("GRA: KÓŁKO I KRZYŻYK");
- g.setSize(700, 700);
- g.setResizable(true);
- g.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent action) {
- Object source = action.getSource();
- if(((JButton)source).getLabel()=="" && gracz == "X")
- {
- ((JButton)source).setLabel("X");
- gracz = "O";
- repaint();
- }
- else if(((JButton)source).getLabel()=="" && gracz == "O")
- {
- ((JButton)source).setLabel("O");
- gracz = "X";
- }
- else if(source == zakoncz)
- {
- zakoncz.setBackground(Color.RED);
- zakoncz.setForeground(Color.BLACK);
- int w = JOptionPane.showConfirmDialog(this, "Czy na pewno chcesz zamknąć program?", "Pytanie", JOptionPane.YES_NO_OPTION);
- if(w == JOptionPane.YES_OPTION)
- dispose();
- else
- {
- zakoncz.setBackground(Color.LIGHT_GRAY);
- zakoncz.setForeground(Color.RED);
- }
- }
- else if(source == nowaGra)
- {
- int w = JOptionPane.showConfirmDialog(this, "Czy na pewno chcesz rozpocząć nową grę?", "Pytanie", JOptionPane.YES_NO_OPTION);
- if(w == JOptionPane.YES_OPTION)
- for(int i=0; i<9; i++)
- {
- b[i].setLabel("");
- gracz = "O";
- repaint();
- }
- }
- else if(source == sprawdz)
- {
- if(wygrana("X"))
- {
- wynik.setText("Wygrał zawodnik: X");
- }
- else if(wygrana("O"))
- {
- wynik.setText("Wygrał zawodnik: O");
- }
- else
- {
- for(int i=0;i<9;i++)
- {
- if(b[i].getLabel()=="")
- {
- wynik.setText("Pola są puste!");
- break;
- }
- if(i==8)
- wynik.setText("Remis!!!");
- }
- }
- }
- }
- public static void main(String[] args){
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- okno();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement