Results 1 to 4 of 4
Thread: button
- 05-04-2011, 01:50 AM #1
Member
- Join Date
- May 2011
- Posts
- 1
- Rep Power
- 0
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; } }
- 05-04-2011, 01:54 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
You can also make the button un clickable withJava Code:JButton b = new JButton("Set in constructor"); JButton c = new JButton(""); c.setText("Set via setText");
Another good way would be to use a JLabel, which sets the text in the constructor or via setText method.Java Code:c.setEnabled(true);
- 05-04-2011, 03:41 AM #3
- 05-04-2011, 03:49 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Oops, I suppose the name implies what true would do. Thanks for the correction.
Similar Threads
-
Make a button class that uses your button image.
By eLancaster in forum New To JavaReplies: 1Last Post: 04-26-2011, 11:32 AM -
Adding a sqrt button and percentage button to a calculator
By Josie_Taylor in forum New To JavaReplies: 4Last Post: 03-14-2011, 01:16 AM -
SWT - X Button
By dorgin in forum SWT / JFaceReplies: 0Last Post: 06-16-2010, 04:05 PM -
Button
By Tb0h in forum New To JavaReplies: 6Last Post: 07-22-2009, 01:28 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks