GUI components of a memory game - java

I am working on a homework so i am not asking for code, i am trying to make this by myself. by the way, i am stuck again with the GUI part and have little problems with code part. first things is about button size and image size. i didnt use methods for size of buttons just set the image as an icon for the button. but as you see below, buttons dont fit the image.
second thing is how can i first disable the icon and when user presses to button it will reveal the icon ?. and how can i embed 8 pictures in a loop? can i create an array for images... i appreciate if you can help me. and thanks anyway : )

I'd start with How to Use Buttons. JToggleButton works well for this, as you can change the Icon in an ItemListener based on the button's selected state. Examples may be found here and here.

Related

How to make JTextFields clickable?

I posted a similar question a year ago, but it was not really well written and I didn't get an answer I could work with. Now I stand in front of the same problem. I got a JPanel (my content pane), where a MouseListener is implemented.
Everywhere I click, I get the exact coordinates of my mouse click. Except my JTextField components. When I click on those, the MouseEvent isn't even triggered. H
ow do I do this, so my mouse clicks on those will also call the mouse event?
Tried: setEnable(false) and setHighlighter(null)
Sorry thought I fixed the X/Y problem.
The X/Y problem simply means you are telling us what your attempted solution is without telling us what your requirement is. We can't suggest a different approach if we don't know what you are trying to do.
I want to open a menu,
Now we know what the requirement is.
The solution is to add the MouseListener to the text field, not the panel. If you have the same popup of the panel and the text field, then you still need to add the listener to both the panel and the text field.
You can do this in one of two ways:
Read the section from the Swing tutorial on Bringing up a Popup Menu for a working example.
Note the above tutorial is a little old, you can also check out the setComonentPopuMenu(...) method of the JComponent class. This approach will create the listener for you.

Java Swing - What's the proper way of doing a stage based GUI?

I'm currently developing the menu of my application and I would like to know what's the proper way of coding a "stage based menu". What I mean by stage based menu is that, user clicks a button, the entire interface changes to the next "stage". Here are the pictures I designed in Photoshop in order to explain my idea:
First picture would be the first stage and the second picture the second.
Each round looking thing is a JButton
So far I got the main menu (fist picture) made on eclipse using WindowBuilder, made it as a JPanel and then I instantiate it on the window class.
My idea was to have an event listener listen for clicks on each button and then once the even is triggered, have the JPanel variable on the frame change to the next "stage". So I was wondering if this is actually the proper way of doing this or are there any better ways?
You could use a Card Layout to make it easy to swap panels.
Check out the section from the Swing tutorial on How to Use Card Layout for more information and examples.

JFrame 'Console' style text

I have probably a quick question but here it goes. I am making a game (new to programming) and I want a text field in the middle of my game that I can easily add to kind of like a console. You will be able to view the beginning from late into the game.
Example:
Welcome to the game.
Added when you click a button
More text is added.
You click a different button
More text is added.
I am currently using a JLabel for this but it's a .setText which is inconvenient when I want to have tons of text throughout my game. As well, how could I add a scrollbar to this?
I'd guess you are looking for JTextArea .

Scrollable BackGround Image with text input fields using Java

I'm pretty new to Java and have been stuck for hours trying to figure out how to do the following...
I would like to create a Java program that shows a panel (has a background image that is scrollable) that contains input boxes. Think of a panel that shows at 800by800, but the image is more like 2000x2000, with input boxes all over. As you scroll, nothing should move.
I'd be very greatful if anyone can point me the right direction or has a sample piece of code that they would share.
Thank You!
You could try a JScrollPane: JScrollPane

How to make a mouseEvent from a Component to be recognized in a subcomponent?

I have a JTabPane, and i have added a MoustListener to it(for tab's title).
When i press right click, a popup menu is created.
i need to make it invisible when i press the mouse button in any place of window. how can i do this??
(the MouseListener is applied only for tab's title.)
i need to make it invisible when i
press the mouse button in any place of
window. how can i do this??
This is the default behavour of a JPopupMenu so you don't have to do anything special.
Read the JPopupMenu API and you will find a link to the Swing tutorial on "How to Use Menus". The tutorial contains a working example of using a popup menu. Compare your code with the tutorial to see whats different. We can't help you because we don't know what your code is like.
If you need more help post your SSCCE.
in the good old days what I did to solve this problem was to register a mouse listener with ALL the components.
you can write a fairly simple function that recursively traverses a top level container and do it.
this was with Java 1.1, so maybe there is a better option today.
One way off the top of my head would be to grab the coordinates of the clicks, and then have another method to determine if the clicks are on the tab, or inside of the content area of the tabs.

Categories