Im making tic tac toe app, after first click on the button it get either "O" or "X" text and I want to avoid second click, like I want to make it immune to second click
if (!pola[i].getText().isEmpty()) {
pola[i].setEnabled(true);
}
so far I got this,
make a boolean clicked set it to true when the button is clicked, then in the action listener method check if !clicked before you execute the code, or maybe if u want u can do JFrame.remove(pola[i]); where JFrame is the frame you are using and if you want it to be interactable again you add it JFrame.add(pola[i]);
Related
I plan to make a text Adventure for a school project, for this I need to add individual buttons which all have actions
Example:
You stand before a troll, a button shows "Attack" and it actually lets
you attack
you stand before a door, a button on the exact same spot shows "Open"
and it opens the door
Every tutorial I've found only shows how to make buttons and add them but I need to individually generate buttons with full functions.
You can create all of your buttons and place them where you want to (same position as you mentioned). After that you only have to change the visibility of the buttons with the setVisible() method.
Like this:
if(/*stand before a troll condition*/)
{
attackButton.setVisible(true);
openButton.setVisible(false);
}
else if(/*stand before a door condition*/)
{
attackButton.setVisible(false);
openButton.setVisible(true);
}
else // standing before nothing
{
attackButton.setVisible(false);
openButton.setVisible(false);
}
So, I was just wondering. Say I had a simple game of pong consisting of two JButtons as pongs that move with key input, a smaller JButton for the ball moving in a timer and a text box for a score. When I made this, I had to click on the button before I could move it. Is there a way to make it so that the button is selected by default when the program runs so that I can just press keys to move it straight away without clicking it first? Thanks.
Use the setDefaultButton method of the JFrame's root pane:
myFrame.getRootPane().setDefaultButton(button);
In frames constructor use below code after making button:
this.getRootPane().setDefaultButton(button);
I think you can select one by default with requestFocus().
Something like:
defaultJButton.requestFocus();
You should do that at the initialization or every time you want to restart, reset the state or similar.
I want to create a hidden button in netbeans. Suppose if I click a Next button then a new "Exit" button will be appear in the same jFrame. Until I click the Next button the Exit button will not be shown. So how to make it? I mean how to make a button which is by-default in .setVisible(false) in netbeans?
Please help me out about this problem.
Since you already seem to know the proper method I assume you doubt is about Netbeans operation.
Right click in the method and select "Customize Code".
Then you'll have the chance to add the proper code after the button creation.
Use JButton.setVisible(false); right after you create the button.
I had just a few class of java on college, but I want to do a thing that I don't know how.
Is basically a window with a SplitPane, on Left side I have a menu made with toggle buttons, and on the Right side I need to change the content based on each button.
Theres any way to design the ViewA and ViewB on separated JFrame Form and load then inside my Right Side when I click on menu items?
Another idea is, put the ViewA and ViewB put a JTabbedPane on the Right Side, and hide the Tabs. So there's any way to hide the tabs?
I have none experience developing in java, any problem about this concept (difficult, loading time, memory, maintenance), If you guy know a better way to to this, I just don't want a lot of windows popping up.
A really simply way would be to simply have a set of jPanels in the right side, with only one ever set to Visible.
Basically, for each toggle on the left side, you will have an Event Listener that does this:
private void toggle1ActionPerformed(java.awt.event.ActionEvent evt) {
jPanel1.setVisible(false);
jPanel2.setVisible(false);
jPanel3.setVisible(true);
}
Simply changing the true value depending on the individual toggle.
In Netbeans, if using the GUI editor, you can simply double click the toggle button to generate the listener and appropriate method, then fill in the code for it.
I have a JButton and when a player clicks it it tell my Action Listener that a button is clicked. What i want to know is that is there a command or something that acts as if a player clicked the button.
Like Tic Tac Toe, i have it so 2 players can play against each other, but i want to add the option for a computer player vs a human player. But since the computer cant actually click the button, i am lost.
Edit:
would it be as easy as gridButton2.click() (Name of button).click();
Pretty much. All you need to do is use the doClick() function. See the API for more information.
for the tic-tac-toe thing, you don't need the computer to click a button. you just have to wait until the human makes a move, then have the computer choose its move and execute the code that happens if the button gets clicked.
Just call directly the actionPerformed() method from the ActionListenerimplementing class. You can do it the following way:
actionPerformed(new ActionEvent(gridButton2, ActionEvent.ACTION_FIRST, "youractioncommand"));