In my previous assignment, I was marked down because I didn't use actionListeners correctly. The assignment included 3 buttons, each of which did something different when pressed. I placed all my code for each button inside 1 action listener class with several if statements.
For example:
class buttonPressed implements ActionListener {
public void actionPerformed(ActionEvent a){
if (a.getSource() == add){
checkWordAdd(input.getText());
}
if (a.getSource() == first){
checkLetter(toArray(al), input.getText().toLowerCase());
}
if (a.getSource() == many){
center.setText(input.getText() + " was found " + addAmount(toArray(al), input.getText().toLowerCase()) + " times");
}
}
}
}
I should have made separate actionListener classes, assigned to different buttons. Like so:
class buttonAdd implements ActionListener {
public void actionPerformed(ActionEvent a){
if (a.getSource() == add){
checkWordAdd(input.getText());
}
}
}
}
class buttonLetter implements ActionListener {
public void actionPerformed(ActionEvent a){
if (a.getSource() == first){
checkLetter(toArray(al), input.getText().toLowerCase());
}
}
}
}
}
etc... etc...
The problem I am now having is what to do in my current assignment. I have to use mouse buttons and depending what button is pressed, something happens. I have done exactly the same what I did in the previous example, and am concerned I will be marked down again. I just don't know how to split these up, because there is only 1 panel I can add the mouseHandler to. (In the previous assignment, there was 3 buttons each of which could have their own buttonHandler).
Here is what I have got:
public void mouseClicked(MouseEvent e){
if (e.getButton() == 1){
// Code to make something happen
}
if (e.getButton() == 3){
// Code to make something happen
}
if (e.getButton() == 2){
// Code to make something happen
}
}
The code inside these seperate if statements is very long and I didn't want to bore you with the details. The bit I am confused with is how can I split these into seperate mouse classes. They all point to the same panel. It just seems like I'll be repeating code over and over again.
I lost about 10% last time because of this mistake and I don't want to make it again.

New Topic/Question
Reply




MultiQuote








|