Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to program a game that has a title screen, and when you press Enter it advances to the game. The issue that I'm running into is that you can't put add a JPanel from a KeyPressed method.
I have one panel for the title and another for the game board, what is likely the best way to go about this?
I have one panel for the title and another for the game board, what is likely the best way to go about this?
Use a Card Layout so you can easily switch from the title panel to the game panel.
Your title panel should also probably have a button like "Start Game" so the user knows what to do. Then they can either click the button or press Enter.
and when you press Enter it advances to the game
To make the button the default button (so it responds to Enter) for the panel you can use:
frame.getRootPane().setDefaultButton(gameButton);
If you don't like the idea of adding a button then you will need to use Key Bindings.
You might want to take a look at CardLayout.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am currently trying to make a calculator in java. I created all the number buttons, operations,number display, etc. I also added the function to every button. Everything were working well. Then I added a button through which I could insert a dot(decimal point). But this is not properly getting inserted in JTextField. Here is the actionPerfomed() for button 1, and dot button....
one.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
numdisp.setText(numdisp.getText()+one.getText()); //numdisp is the number displayer(JTextField)
}
//Other buttons like button2,button3,button4,etc.... are not mentioned as they have the same set of code....
dot.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
numdisp.setText(dot.getText()+numdisp.getText());
});
Download the jar file here
When you press the one button the JTextField shows the number 1. Then press the dot button. The JTextField shows "1.". Everything is good till now. Then press 1 again. The expected result is "1.1" but instead It displays "11.". Why is this weird problem occurring? How to fix it?
The logic in your ActionListeners is different. In one case you add the text of the button at the beginning. In the other case you add the text of the button at the end.
So a better solution is to not use different ActionListeners, but to share the same ActionListener, so the logic will be same for all buttons and you don't have to create multiple listeners. This way you are less likely to make mistakes.
Check out setText method with panel and button. This example will show you how to:
create a single ActionListener to be shared by each button
"append" the text to the text field instead of replacing the text
use Key Bindings so the user can also just type the number
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
When I run my program, I have a button called "Create new account" that opens a new jframe (bad design I know), and when this new window opens, it suddenly distorts and changes the color of some of the text on tabbed panels from the previous jframe.
Here are some images of the problem I'm experiencing. Look at the tabs labeled administration, create new account, and modify existing account
Before:
After:
Any help or insight as to why the text gets distorted would really be appreciated.
You should not have JFrames launching JFrames. If a JFrame needs to launch another window that behaves as its child window, it should be a dialog such as a JDialog or JOptionPane. Note that dialogs can be shown in both modal and non-modal fashion, the former (modal) freezes the code flow from the calling program until the dialog has been dealt with, preventing users from interacting with the parent window while the latter (non-modal) doesn't.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
sorry for my blunders..
i am creating a simple java game.in my game is a rat image(jlabel) which appears randomly on the screen we need to click on the rat to win.if i click on the screen other than that includes rat image, the number of chances should decrease.i included levels in it.each level differs by rat speed.i included timer to change speed for levels.so wen i complete 1st level it will move 2 2nd level.here i am geting difficulty.when i click on level2 button it is called 2 times and when i click on rat image the rat image's listener is called 3 times and so on.. i am not getting how to do it.....
thank u :)
A JLabel doesn't support an ActionListener.
So I would guess you are using a MouseListener. If it keeps getting called one more time whenever you click on it then it sounds like you keep adding the MouseListener to the label in the event code.
You should only have one addMouseListener() statement in your code for each component.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I wish to create a button exactly same as this (from iOS5).
You will have two images, for ON and for OFF.
Whenever you click the button, you will switch images with button.setImage(..).
Swing constraints:
Button will still be rectangular, even though the image is oval
Animation possible, but quite hard AFAIK. From the way you asked your question, I think it will take you a while to study swing more
In swing, there is JToggleButton which can have two status. However, if you want the button of the same shape and the same visual effect, you should implement it.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am.working. on a Netbeans Java project in JFrame form (a GUI application) where I want to move a JLabel into circular path. Can any tell or help to how to do that?
It sounds like you haven't done anything yet, and when that happens and you need to get started, then you should try to break the main problem into little steps, and then try to solve each little step one at a time, including looking at references for each of these steps such as using a Timer, doing animation, positioning components, etc...
So general recommendations:
Look up using a Swing Timer (or just click on link)
Use the Timer to drive your animation.
You can move a JLabel if the layout is null, but this is generally to be avoided.
Consider instead using a custom layout if you absolutely need to move a JComponent (the JLabel) along a prescribed path.
Or if you just want to move an image, then draw the image inside of a JPanel's paintComponent(...) method, setting its position with two int fields that are changed by the Timer. This JPanel of course will need to be displayed in your GUI. There are lots of examples on how to do this on this site, some written by me (for example), that simple searching can help you find.
But most important, take the first steps, do something, anything, that moves you forward with this project.
Then when you try this if it doesn't work, show your code and we'll be much better able to help.