I'm making a game where the players are taking turns and they would have a set of buttons each that they can click when it is their turn. Below is a sample code that follows the logic of what I am saying. But what happens is when I clicked the "btn1", it prints three 1s and I can still click the second button.
//this loop is in the main
for(int i=0; i<3;i++){
if(player==1){
player1();
}
else if (player==2){
player2();
}
}
public void player1(){
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.out.println("\n1");
player=2;
}});
}
public void player2(){
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.out.println("\n2");
player=1;
}});
}
I can see what could be the problem but I don't know what to do.
Replace the loop
for(int i=0; i<3;i++){
if(player==1){
player1();
}
else if (player==2){
player2();
}
}
with just
player1();
player2();
Instead of adding 3 times the same listener to the button add it just once
If all you want to do is enable and disable buttons, why not do:
public void player1(){
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.out.println("\n1");
player=2;
btn1.setEnabled(false);
btn2.setEnabled(true);
}});
}
public void player2(){
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.out.println("\n2");
player=1;
btn1.setEnabled(true);
btn2.setEnabled(false);
}});
}
Related
b.button1 = new JButton("Deal");
b.button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//code
b.button2 = new JButton("Hit");
panel.add(b.button2);
panel.validate();
b.button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//code
}
});
b.button3 = new JButton("Stay");
panel.add(b.button3);
panel.validate();
b.button3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//code
}
}
});
So I want The Buttons Hit and Stay to be added as soon as the Deal button has been pressed. I searched for a solution and found the panel.validate() method. I used it but now if I press the Deal button it only adds the Hit button.
You could add the buttons before and make them "hidden". If you press the button, you can "show" them to include them.
I created an action listener that listens if there is any change with a departingStop (a combo box object)
departingStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//Lots of code here
}
});
I would like to also add this action listener to another combo box (finalStop) without having to create a separate listener like so:
finalStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//Lots of code here
}
});
How can this be achieved? Thank you
You can assign the listener to a variable...
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//Lots of code here
}
};
And then add it multiple times...
departingStop.addActionListener(listener);
finalStop.addActionListener(listener);
as commented above, you are implementing an anonymous listener you need a reference that is set to both:
ActionListener foo = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//Lots of code here
}
};
departingStop.addActionListener(foo);
finalStop.addActionListener(foo);
If your IDE makes it difficult to set arbitrary listener on GUI components, put the common listener functionality into a separate method and call that method from both combo boxes' listeners:
departingStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
commonGutsOfListener(arg0);
}
});
departingStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
commonGutsOfListener(arg0);
}
});
private void commonGutsOfListener(ActionEvent arg0){
//Lots of code here
}
The button listener is stacking: the first time I click the button, it writes in the combo-box once; the second time it writes twice, and so on.
Do I have to clean the buffer or something like that?
novoP.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
painel.removeAll();
painel2.removeAll();
if(autenticator == false) {
add(painel, BorderLayout.CENTER);
painel.add(nega);
} else {
add(painel, BorderLayout.CENTER);
painel.add(iproduto);
painel.add(buton);
buton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
jComboBox1.addItem(iproduto.getText().toString());
}
});
}
painel.repaint();
painel.revalidate();
painel2.repaint();
painel2.revalidate();
}
});
To simply the question here, for the following code, after mouse clicking, instead of executing the code one line by another, the following happens:
1. program paused for 1 second;
2. GLabel "CLICKED" and GLabel "PAUSE FINISHED" are added to canvas at the same time after the pause.
Could someone explain this for me? Thank you very much.
public void mouseClicked(MouseEvent e){
add(new GLabel("CLICKED"),200,200);
pause(1000);
add(new GLabel("PAUSE FINISHED"),200,300);
}
While this code would work as expected:
public void run(){
add(new GLabel("CLICKED"),20,20);
pause(1000);
add(new GLabel("PAUSE FINISHED"),20,50);
}
Try using handler to run the pause on next frame: like this
public void mouseClicked(MouseEvent e){
add(new GLabel("CLICKED"), 200, 200);
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
pause(1000);
add(new GLabel("PAUSE FINISHED"), 200, 300);
}
}, 1);
}
Finally this solved my problem (Thanks #Charles Goodwin for his answer to the question: Java MouseEvent, check if pressed down)
public void mouseClicked(MouseEvent e) {
x=e.getX();
y=e.getY();
if(getElementAt(x,y)==null) return;
currentTower = (GCompound) getElementAt(x,y);
initThread();
}
private void initThread() {
new Thread() {
public void run() {
((SignalTower) currentTower).lightTower();
}
}.start();
}
public void lightTower(){
beacon.setFillColor(Color.red);
beacon.setFilled(true);
pause(500);
beacon.setFilled(false);
if(this.next!=null) this.next.lightTower();
}
addItem = new JButton("Add");
gc.gridwidth = GridBagConstraints.RELATIVE;
gc.weightx = 1.0;
panel.add(addItem, gc);
Am I able to make it into something like that:
addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
Instead of this I would want that button, but I have no idea what does that addItem do, since it does not let me to add a name there.
Is there a way how I can do this without modifying the 4 rows of code given at the beginning of the question?
what you can do is to add ActionListener to the button click or if you want you can add MouseListener , actually it depends on what you want to do
addItem.addActionListener(new ActtionListener() {...});
Code using an inner class instead of EventHandler.
addItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//do something
}
});
you can get more information on this in java official website.
Refer: http://docs.oracle.com/javase/7/docs/api/java/beans/EventHandler.html
if you add just the mouse listener you will not get the 'press' event if using keyboard. So, if your requirement is strictly bound to mouse press then use below snippet :)
addItem.addMouseListener(new MouseListener() {
#Override
public void mouseReleased(MouseEvent e) {
// do something
}
#Override
public void mousePressed(MouseEvent e) {
// do something
}
#Override
public void mouseExited(MouseEvent e) {
// do something
}
#Override
public void mouseEntered(MouseEvent e) {
// do something
}
#Override
public void mouseClicked(MouseEvent e) {
// do something
}
});