Hi everyone.
At the moment I am trying to learn myself Java, and I am doing better and better lucky.
There is however one thing I don't understand about JButtons.
I tried searching the forums, but I couldn't find it, so I hope somebody here can help me.
I am making a game called Tic Tac Toe, in Dutch it is: Boter, Kaas & Eieren.
Now I am trying to make a game for a Human against a Computer.
nd to try some things out, I use 2 pictures and not X or O.
This is my code so far:
// Import import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; public class BoterKaasEieren extends JFrame implements ActionListener { // Load pictures ImageIcon playerPicture = new ImageIcon("assassinsCreed.jpg"); ImageIcon computerPicture = new ImageIcon("princeOfPersia.jpg"); // Create Integers. static int turn = 0; // Create JButtons JButton button1 = new JButton("Knop1"); JButton button2 = new JButton("Knop2"); JButton button3 = new JButton("Knop3"); JButton button4 = new JButton("Knop4"); JButton button5 = new JButton("Knop5"); JButton button6 = new JButton("Knop6"); JButton button7 = new JButton("Knop7"); JButton button8 = new JButton("Knop8"); JButton button9 = new JButton("Knop9"); /** * BoterKaasEieren() * Make a frame with buttons. */ public BoterKaasEieren(){ // 3 rows, 3 columns setLayout(new GridLayout(3,3)); // Add JButtons and JLabels to GUI add(button1); add(button2); add(button3); add(button4); add(button5); add(button6); add(button7); add(button8); add(button9); } /** * actionPerformed() * When a button is pushed, perform an action. */ public void actionPerformed(ActionEvent e) { // Button 1 if (e.getSource() == button1) { button1.setIcon(playerPicture); } // Button 2 else if (e.getSource() == button2) { // } else if (e.getSource() == button3) { // } else if (e.getSource() == button4) { // } else if (e.getSource() == button5) { // } else if (e.getSource() == button6) { // } else if (e.getSource() == button7) { // } else if (e.getSource() == button8) { // } else if (e.getSource() == button9) { // } else { System.out.println("Button not regocnized!"); } } /** * randomNumber() * Generate a Random number between certain numbers. * In this case, between 0 and 8. */ public static void randomNumber() { Random rand = new Random(); int n = rand.nextInt(9); System.out.println(n); } /** * order() * Looks who's turn it is. */ public static void order() { // if (turn % 2 == 0) { System.out.println("Player"); } else { System.out.println("Computer"); } // turn = turn +1; } /** * main() * */ public static void main(String[] args) { // Define the frame itself. BoterKaasEieren frame = new BoterKaasEieren(); frame.setTitle("Boter, Kaas & Eieren - versie 1.0"); frame.setSize(500, 500); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); // run subclasses // /** int g = 0; int c = 10; while (g < c){ order(); g = g + 1; } */ } }
Only problem I have is that if a button is pushed (button1), then the image doesn't get shown on the game.
I know the image works, and I think the word "Knop1" is more dominant all the time.
But how do I change this?
--- Update ---
I figured it out already. :p
I changed something:
// Add JButtons and JLabels to GUI add(button1); button1.addActionListener(this);
And now the picture does go on the button.
I just need to clear the text from the button as well, but I will find that one out. :p