Results 1 to 4 of 4

Thread: button

  1. #1
    REX055 is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default button

    Hello,

    I would like to create a button which is showing the answer in this matching game.

    Do anybody have a idea?



    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.Shape;
    import java.awt.Cursor;
    import java.awt.event.ActionEvent;
    import java.awt.event.InputEvent;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Line2D;
    import java.awt.geom.Rectangle2D;
    import java.awt.geom.Area;
    
    import java.awt.*;
    import javax.swing.*;
    
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    
    public class matchgame {
            public static void main(String[] args)
            {
                    GameFrame frame = new GameFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
            }
    }
    
    class GameFrame extends JFrame
    {
            private JButton button, firstButton, secondButton;
            JPanel buttonPanel;
            int cardnum = 0;
            private String first, second;
           
            public GameFrame()
            {
                    setTitle("Game Frame");
                    setSize(600, 500);
    
                    JMenuBar menuBar = new JMenuBar();
                   
                    JRadioButton LGlayButton = new JRadioButton("LightGray", true);
                    menuBar.add(LGlayButton);
                    JRadioButton YellowButton = new JRadioButton("Yellow", false);
                    menuBar.add(YellowButton);
                    JButton ResetButton = new JButton("Reset");
                    menuBar.add(ResetButton);
                   
                    setJMenuBar(menuBar);
                   
            buttonPanel = new JPanel(new GridLayout(6, 8));
            addButton();
            getContentPane().add(buttonPanel);
            }
           
            private void addButton()
            {
                    IntegerPair pair = new IntegerPair();
                    int [] array = pair.GeneratePairedIntegerArray();
                    for (int count = 0; count < array.length; count++)
                    {
                            JButton button = new JButton();
                            button.setActionCommand(Integer.toString(array[count]));
                            button.addActionListener(new ActionListener()
                            {
                                    public void actionPerformed(ActionEvent e)
                                    {
                                        if (cardnum == 2)
                                        {
                                                if(firstButton != secondButton){
                                                if(comparecard()){
                                                        firstButton.setEnabled(false);
                                                        secondButton.setEnabled(false);
                                                } else {
                                                        firstButton.setText("");
                                                        secondButton.setText("");
                                                }
                                                } else {
                                                        secondButton = null;
                                                        cardnum--;
                                                }
                                        }
                                           
                                            String flipcard = e.getActionCommand();
                                            cardnum++;
                                            JButton b = (JButton) e.getSource();
                                            setcard (flipcard, cardnum, b);
                                            b.setText(flipcard);
                                    }
                            });
                            buttonPanel.add(button);
                    }
            }
           
            private void setcard(String card, int cardnum, JButton button)
            {
                    if (cardnum == 1)
                    {
                            first = card;
                            firstButton = button;
                            System.out.println("First card is: " + first + "\n");
                    }
                   
                    else if (cardnum == 2)
                    {
                            second = card;
                            secondButton = button;
                            System.out.println("Second card is: " + second + "\n");
                    }
            }
           
            public String getfirstCard()
            {
                    return first;
            }
           
            public String getsecondCard()
            {
                    return second;
            }
           
            private boolean comparecard()
            {
                    String a = getfirstCard();
                    String b = getsecondCard();
                    int firstcards = Integer.parseInt(a);
                    int secondcards = Integer.parseInt(b);
                    if (firstcards == secondcards)
                    {
                            System.out.println("get pair!!");
                            cardnum = 0;
                            return true;
                    }
                   
                    else
                    {
                            cardnum = 0;
                            System.out.println("sorry!!");
                            return false;
                    }
            }
    }
    
            class IntegerPair
            {
                    public int MAX_VALUE = 20;
                    public int ARRAY_SIZE = 48;
                   
                    private int [] sampleArray;
                   
                    public IntegerPair()
                    {
                            sampleArray = InitializeArray(MAX_VALUE);
                    }
                   
                    public int [] GeneratePairedIntegerArray()
                    {
                            int [] intArray = InitializeArray(ARRAY_SIZE);
                            int [] indexArray = InitializeArray(ARRAY_SIZE);
                           
                            int currentIndexSize = indexArray.length;        //10
                            int iterationNum = intArray.length / 2;                //5
                            for (int i = 0; i < iterationNum; i++)
                            {
                                    int sampleIndex = (int)(Math.random() * sampleArray.length);
                                    //random 0-99 number
                                   
                                    int index = (int)(Math.random() * currentIndexSize);
                                    intArray[indexArray[index]] = sampleArray[sampleIndex];
                                    indexArray[index] = indexArray[--currentIndexSize];
                                   
                                    index = (int)(Math.random() * currentIndexSize);
                                    intArray[indexArray[index]] = sampleArray[sampleIndex];
                                    indexArray[index] = indexArray[--currentIndexSize];
                            }
                           
                            return intArray;
                    }
                   
                    private int [] InitializeArray(int size)
                    {
                            int [] array = new int [size];
                           
                            for (int i = 0; i < size; i++)
                            {
                                    array[i] = i;
                            }
                           
                            return array;
                    }       
            }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I didn't read all your code but you can have a JButton which has some text on it. by either calling the setText method, or setting the text in the constructor

    Java Code:
    JButton b = new JButton("Set in constructor");
    JButton c = new JButton("");
    c.setText("Set via setText");
    You can also make the button un clickable with
    Java Code:
    c.setEnabled(true);
    Another good way would be to use a JLabel, which sets the text in the constructor or via setText method.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Quote Originally Posted by sunde887 View Post
    You can also make the button un clickable with
    Java Code:
    c.setEnabled(true);
    Um, false

    db

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Oops, I suppose the name implies what true would do. Thanks for the correction.

Similar Threads

  1. Make a button class that uses your button image.
    By eLancaster in forum New To Java
    Replies: 1
    Last Post: 04-26-2011, 12:32 PM
  2. Adding a sqrt button and percentage button to a calculator
    By Josie_Taylor in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 02:16 AM
  3. SWT - X Button
    By dorgin in forum SWT / JFace
    Replies: 0
    Last Post: 06-16-2010, 05:05 PM
  4. Button
    By Tb0h in forum New To Java
    Replies: 6
    Last Post: 07-22-2009, 02:28 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
  •