I am cleaning my code. I read that I putting the ActionListener
is another class is better. So that's what I did.
But in my ActionListener
, everything works except at some point in the code, I got a setSize(xx,xx)
. I worked before because it was in the same class. But not anymore. I've tried multiple solutions but I couldn't figure it out.
ActionListener
:
public class ActionFrame implements ActionListener{
public void actionPerformed(ActionEvent e){
Object src = e.getSource();
if(src == Frame.Console_Bouton){
System.out.println("Bouton console");
if(getSize().getWidth() >= 750){
/** If True (Retirer) */
for(int i = 1090; i > 689; i--){
setSize(i, 490);
System.out.println("Rétractation du Frame");
}
}else{
/** If False (Etirer) */
for(int i = 689; i < 1090; i++){
setSize(i, 490);
System.out.println("Etirage du Frame");
}
}
}
...
As for errors, there are none, it will just freeze the program.
Guessing: possibly this is a case where extracting the ActionListener
is not a great idea, since it uses a private
method of your object.
Classes with generic/reusable functionality deserve to be on their own. As long as they are intended for specific usage, it's not bad practice (at all!) to only put them as close as possible to the spot where they're used. I can imagine that your setSize
method is not part of your class' public interface, so the ActionListener
is merely 'glue' to couple an event to your specific class.
In this case, you would create a 'tiny' line of glue:
abstract class ActionAdapter implements ActionListener {
}
...
frame.Console_Bouton.addActionListener(
new ActionAdapter(){ // anonymous inner class
void actionPerformed(ActionEvent e){
... // (no need to check source!)
}
});
Create a new class as:
ButtonAction implements actionListner
{
//put the code above here
}
A Good way of doing this is use Callback mechanism .
I have posted an answer in the same context here
-- EDIT--
Get the source from ActionEvent
then find its parent (get parent of parent if needed until you get the desired component that needs to be re sized) and call setSize()
on it.
Create an instance of the View in the controller
Change the access modifier of setSize(xx,yy)
method from private
to public
.
replace setSize
in actionPerformed()
with to view.setSize(xx,yy)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.