I have been working on a memory game i asked a question about yesterday, I got some brilliant suggestions but none of them seem to be helping my problem i can get the buttons to shuffle around the grid layout fine, but i cannot figure out how to keep the set values i tried using "setActionCommand" but i don't think this is the way i can do it for matching the values of two buttons within my memory game.
gameBtn[0].setActionCommand("1");
gameBtn[1].setActionCommand("2");
gameBtn[2].setActionCommand("3");
gameBtn[3].setActionCommand("4");
gameBtn[4].setActionCommand("5");
gameBtn[5].setActionCommand("6");
gameBtn[6].setActionCommand("7");
gameBtn[7].setActionCommand("8");
gameBtn[8].setActionCommand("1");
gameBtn[9].setActionCommand("2");
gameBtn[10].setActionCommand("3");
gameBtn[11].setActionCommand("4");
gameBtn[12].setActionCommand("5");
gameBtn[13].setActionCommand("6");
gameBtn[14].setActionCommand("7");
gameBtn[15].setActionCommand("8");
above is how i used the setActionCommand but i cant find out how you would compare them to each other to find a match for example matching club1 to diamond1 and club2 to diamond2. would this not mean i would have to do something like this for every single match i wanted?.
if (gameBtn[0].getActionCommand().equals(gameBtn[8].getActionCommand())){
sameValues();
}
if (gameBtn[1].getActionCommand().equals(gameBtn[9].getActionCommand())){
sameValues();
}
although the above might seem to work as it does not throw any errors im left thinking there must be a more efficient better way to do it i accomplished this at first before i added the shuffle to move the buttons on the grid i was using another list of values, but know its randomized the card on the button no longer actually matches the buttons set value.
have a look at the link at the bottom of the question if you need more information from my last question, Any help would be much appreciated as this has been bugging me for a while know thank you.
link: Java concentrate is there a way to shuffle jbuttons
Related
I need to make a list of runners that arrive each at a different time. So basically what i want is to show in a Jlist the name of the runner, his team and the time he took to finish the race all in a single row. Is there a method for this?
So far i've only found [list.setLayoutOrientation(JList.HORIZONTAL_WRAP);] but i'm not sure if i'm using it correctly or if its even a solution to my problem.
Any help is appreciated :)
I was wondering if there was a way to make one component (in this case a JPanel) visible in more than one of the tabs.
I have a simple JFrame which is completely filled with a JTabbedPane. Each tab has a slightly different function, which is why they're in different tabs. However, they each output the same type of information. I was wondering if there was a way to make it so the same output panel could be seen in all the tabs, without having to create one panel for each tab.
I realize that it is impossible to add one component multiple times (and have it display independently each time), which is why I'm not optimistic about this being possible, however if it is, it would make my code much cleaner. In the case that this is not possible, I am completely open to alternate suggestions that achieve a similar result. I am in the very beginning of my project, so it won't be too difficult to change things.
This image gives a rough idea of what I'm trying to make it look like:
I have searched around for anyone dealing with this issue, however I have had little luck finding anything relevant. If I missed something, I apologize for wasting your time.
I've poked around the internet a bit and my lack of findings makes me guess yes, but would using an array to fill the items of a JMenuBar be a less than good idea? I also think yes since whenever you wonder "is this a bad idea?" it usually is. However, my newness to programming makes me question if I know a bad from good idea at this point.
Originally I thought it'd be good since most of the time arrays are beneficial and save space. However, when I began thinking through the code I realized it wasn't exactly practical. To start if you do all the items in one array you are trying to count to figure out which number goes with which menu.
I suppose you can do an array for the items in each menu, but if you decide to change an item to a menu so you can have sub-menus you're back to which-number-is-it issues.
Edit: It also makes creating and adding the items and adding action listeners much quicker
Opinions?
I'm going to go with no. although it looks like it would save alot of time, and is a very good idea, i'm afraid that it will not work.
I am saying this because the JMenu, or JMenuBar 'add' method is only good for use with single JMenus or JMenuItems. I have tried and I got an error saying that it is used with JMenu not JMenu[].
If you would like, it might be possible to make a class that adds arrays to JComponents, by going through each array item until null, and adding it seperatly. That will be alot of extra work, but it might just pay of for speed and efficiency.
I have a project due in about an hour and I need to figure out the problem in my code before submitting.
In essence, there are two classes: one is a game and the other is a gui that should contain that game, some useless buttons, and a picture.
I'm having trouble making it all fit. I think I have a problem with the JPanels. (There might be some useless looking methods, but I had plans for them and I'll see if I have enough time for them to come into fruition)
This is life and death for my grade. I'd be eternally grateful if you could fix this code for me.
I'm positive the problem is not in the nitty gritty of the second link. It's been tested on its own and works.
Here's the code
http://pastebay.net/1236108
http://pastebay.net/1236109
Here's what I get when I run the code:
Get rid of the window in the Hangman constructor. Hangman is already frame.
Use pack instead of setSize
Git rid of game1 and game2 panels, they are adding nothing and causing you all the problems.
You will also find that if you override the getPreferredSize methods of DrawingDemoPanel and SubKillerPanel and return an appropriate size, that they will layout better.
Alternativly, you could create a single game panel and use a GridLayout to add the DrawingDemoPanel and SubKillerPanel to it, then add this to the CENTER position of the main window it will allow out better...
I'm currently using net beans with Java to work on a senior project. Essentially the program is going to create word search puzzles.
While I'm still a few weeks from needing a solution, I'm having difficulty thinking of a possible control that could display a word search puzzle (not to be mistaken for a crossword puzzle).
My initial thought would be to have a grid of subclassed JPanels stored in another subclass of JPanel [as a 2D array, or something].
You could then attach MouseListeners to the grid cell JPanels if you need to detect if they've been clicked on, or whatever
Might give you an idea
brainjar - Wordsearch (source at bottom of page)
What do you mean by control? Maybe something like a JTable + Custom ListSelectionModel