Menjelang liburan akhir tahun, kantor sepi.....banget, banyak yang ambil cuti, benar-benar tidak kondusif untuk bekerja, hadeh .
Setelah semua kerjaanku selesai, iseng-iseng, aku buat game Tic tac tu, karena pernah ingat, dulu ada latihan pemrograman bikin game ini. Ini masih sangat sederhanaaaaaaaaaaaaaaa sekali. Belum ada handling untuk scorenya, hihi...
Begini nih sintaksnya:
import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * * @author Dyah Fajar */ public class SiimpleGame extends JFrame implements ActionListener { private JButton btn1; private JButton btn2; private JButton btn3; private JButton btn4; private JButton btn5; private JButton btn6; private JButton btn7; private JButton btn8; private JButton btn9; private JButton resume; private JLabel status; JPanel panel1; JPanel panel2; JPanel panel3; Container c; int clicked = 0; public SiimpleGame() { btn1 = new JButton(); btn1.addActionListener(this); btn2 = new JButton(); btn2.addActionListener(this); btn3 = new JButton(); btn3.addActionListener(this); btn4 = new JButton(); btn4.addActionListener(this); btn5 = new JButton(); btn5.addActionListener(this); btn6 = new JButton(); btn6.addActionListener(this); btn7 = new JButton(); btn7.addActionListener(this); btn8 = new JButton(); btn8.addActionListener(this); btn9 = new JButton(); btn9.addActionListener(this); resume = new JButton("Resume"); resume.addActionListener(this); status = new JLabel("Status"); // status.setSize(10, 10); panel1 = new JPanel(); panel1.setLayout(new GridLayout(3, 3)); panel1.add(btn1); panel1.add(btn2); panel1.add(btn3); panel1.add(btn4); panel1.add(btn5); panel1.add(btn6); panel1.add(btn7); panel1.add(btn8); panel1.add(btn9); panel2 = new JPanel(); panel2.setLayout(new FlowLayout(FlowLayout.CENTER)); panel2.add(status); panel3 = new JPanel(); panel3.setLayout(new FlowLayout(FlowLayout.LEFT)); panel3.add(resume); c = getContentPane(); c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS)); c.add(panel1); c.add(panel2); c.add(panel3); setSize(350, 400); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frame = getSize(); setLocation((screen.width-frame.width)/2, (screen.height-frame.height)/2); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { SiimpleGame sg = new SiimpleGame(); sg.setVisible(true); } public void refresh(){ SiimpleGame sg = new SiimpleGame(); sg.setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == btn1) { if (clicked % 2 == 0 || clicked == 0) { btn1.setText("X"); } else { btn1.setText("O"); } clicked++; btn1.setEnabled(false); } else if (e.getSource() == btn2) { if (clicked % 2 == 0 || clicked == 0) { btn2.setText("X"); } else { btn2.setText("O"); } clicked++; btn2.setEnabled(false); } else if (e.getSource() == btn3) { if (clicked % 2 == 0 || clicked == 0) { btn3.setText("X"); } else { btn3.setText("O"); } clicked++; btn3.setEnabled(false); } else if (e.getSource() == btn4) { if (clicked % 2 == 0 || clicked == 0) { btn4.setText("X"); } else { btn4.setText("O"); } clicked++; btn4.setEnabled(false); } else if (e.getSource() == btn5) { if (clicked % 2 == 0 || clicked == 0) { btn5.setText("X"); } else { btn5.setText("O"); } clicked++; btn5.setEnabled(false); } else if (e.getSource() == btn6) { if (clicked % 2 == 0 || clicked == 0) { btn6.setText("X"); } else { btn6.setText("O"); } clicked++; btn6.setEnabled(false); } else if (e.getSource() == btn7) { if (clicked % 2 == 0 || clicked == 0) { btn7.setText("X"); } else { btn7.setText("O"); } clicked++; btn7.setEnabled(false); } else if (e.getSource() == btn8) { if (clicked % 2 == 0 || clicked == 0) { btn8.setText("X"); } else { btn8.setText("O"); } clicked++; btn8.setEnabled(false); } else if (e.getSource() == btn9) { if (clicked % 2 == 0 || clicked == 0) { btn9.setText("X"); } else { btn9.setText("O"); } clicked++; btn9.setEnabled(false); } else if (e.getSource() == resume){ btn1.setEnabled(true); btn2.setEnabled(true); btn3.setEnabled(true); btn4.setEnabled(true); btn5.setEnabled(true); btn6.setEnabled(true); btn7.setEnabled(true); btn8.setEnabled(true); btn9.setEnabled(true); btn1.setText(null); btn2.setText(null); btn3.setText(null); btn4.setText(null); btn5.setText(null); btn6.setText(null); btn7.setText(null); btn8.setText(null); btn9.setText(null); status.setText("Status"); // refresh(); } } } |
boleh dicoba tu mba',,,okelahhttp://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/4.gif
BalasHapusiya, coba aja dek, di modifikasi sekalian, :D
BalasHapus