Results 1 to 6 of 6
  1. #1
    yasmin k is offline Member
    Join Date
    Mar 2009
    Posts
    23
    Rep Power
    0

    Exclamation how to give JButton a name when creating GridLayout

    hello forums :)

    i manage to cread a grid layout for TIC TAC TOE. i have created 9 buttons, but i want to assign each button?

    so for instance i want to call my first button 'box 1' so that box 1 can be assigned to the if statement, if anyone could help me out i would really appreciate it.

    thank you for your time. :)


    import java.awt.GridLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;


    public class TTT {



    public static void main(String[] args) {

    JFrame frame = new JFrame("Guessing Game");
    frame.setVisible(true);
    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    JPanel panel = new JPanel();
    frame.add(panel);
    panel.setLayout(new GridLayout(3, 3, 5, 10));

    panel.add(new JButton("Button 1"));
    panel.add(new JButton("Button 2"));
    panel.add(new JButton("Button 3"));
    panel.add(new JButton("Button 4"));
    panel.add(new JButton("Button 5"));
    panel.add(new JButton("Button 6"));
    panel.add(new JButton("Button 7"));
    panel.add(new JButton("Button 8"));
    panel.add(new JButton("Button 9"));

    //ActionPerformed
    public void actionPerformed(ActionEvent e)
    {
    int count=1;
    do
    {

    if (e.getSource()==box1)
    {
    box1.setText(mark);
    box1.setEnabled(false);
    }
    else if (e.getSource()==box2)
    {
    box2.setText(mark);
    box2.setEnabled(false);
    }
    else if (e.getSource()==box3)
    {
    box3.setText(mark);
    box3.setEnabled(false);
    }
    else if (e.getSource()==box4)
    {
    box4.setText(mark);
    box4.setEnabled(false);
    }
    else if (e.getSource()==box5)
    {
    box5.setText(mark);
    box5.setEnabled(false);
    }
    else if (e.getSource()==box6)
    {
    box6.setText(mark);
    box6.setEnabled(false);
    }
    else if (e.getSource()==box7)
    {
    box7.setText(mark);
    box7.setEnabled(false);
    }
    else if (e.getSource()==box8)
    {
    box8.setText(mark);
    box8.setEnabled(false);
    }
    else if (e.getSource()==box9)
    {
    box9.setText(mark);
    box9.setEnabled(false);
    }




    }
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    12

    Default

    Use the "Code" tags when posting code so the code retains its original formatting.

    There is no need to know the "name" of a button you should be writing generic code:

    Java Code:
    JButton button = (JButton)e.getSource();
    button.setText(mark);
    button.setEnabled(false);
    Thats it, all you need is 3 lines of code. No need for the "do" block, or the nested if/else.

  3. #3
    yasmin k is offline Member
    Join Date
    Mar 2009
    Posts
    23
    Rep Power
    0

    Default

    ok i've tried what you have suggested nothing seem to be happening, i am trying to test out the first three button, nothing happends. once i click on it.


    it should set a mark x once i click on it.

    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;


    public class TTT {



    public static void main(String[] args) {

    JFrame frame = new JFrame("Guessing Game");
    frame.setVisible(true);
    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    JPanel panel = new JPanel();
    frame.add(panel);
    panel.setLayout(new GridLayout(3, 3, 5, 10));

    panel.add(new JButton ());
    panel.add(new JButton());
    panel.add(new JButton());
    panel.add(new JButton());
    panel.add(new JButton());
    panel.add(new JButton());
    panel.add(new JButton());
    panel.add(new JButton());
    panel.add(new JButton());
    }



    public void actionPerformed(ActionEvent e){



    String mark = "X";
    boolean win = false;
    Color black = new Color(0,0,0);
    Color notblack = new Color(255,255,255);


    JButton button = (JButton)e.getSource();
    button.setText(mark);
    button.setEnabled(false);

    JButton button2 = (JButton)e.getSource();
    button2.setText(mark);
    button2.setEnabled(false);

    JButton button3 = (JButton)e.getSource();
    button3.setText(mark);
    button3.setEnabled(false);





    if (button.getText().equals(button2.getText()) && button2.getText().equals(button3.getText()) && button.getText().equals("")==false)
    {
    button.setBackground(notblack);
    button2.setBackground(notblack);
    button3.setBackground(notblack);
    win=true;

    JOptionPane.showMessageDialog(null, mark + " WINS!");
    System.exit(1);

    }
    }
    }

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    12

    Default

    ok i've tried what you have suggested
    Then where are the code tags? Why is the code not formatted?

    i am trying to test out the first three button,
    Why did you add code for 3 buttons? The entire method only needs 3 lines of code. You can only click one button at a time. Thats why the code is generic, it doesn't matter which button you click on.

    nothing happends. once i click on it.
    Did you add an ActionListener to the button? Start by reading the JButton API where you will find a link to the Swing tutorial on "How to Use Buttons" which contains a working example.

  5. #5
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    285
    Rep Power
    11

    Default

    camickr is completely correct. The actionPerformed method operates on e.getSource().

    If you do want an answer to your original question,
    i want to assign each button
    Here's how:
    Java Code:
      [COLOR="DarkGreen"] //in the declarations:[/COLOR]
       JButton box11 = new JButton("Button 1");
       JButton box12 = new JButton("Button 2");
          ...  box13  box21  box22  box23  box31  box32
       JButton box33 = new JButton("Button 9");
    
       [COLOR="DarkGreen"] //in the constructor:[/COLOR]
       panel.add(box11);
       panel.add(box12);
          ...
       panel.add(box33);

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

Similar Threads

  1. Creating a JButton that does not show focus
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 07:38 PM
  2. SWT GridLayout
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:04 AM
  3. GridLayout with animation?
    By tojas in forum AWT / Swing
    Replies: 3
    Last Post: 11-12-2007, 10:16 PM
  4. Help with gaps in GridLayout
    By trill in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 07:20 PM
  5. Gridlayout
    By Marty in forum AWT / Swing
    Replies: 2
    Last Post: 05-31-2007, 11:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •