public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source.equals(jbColor1))
color = getBlue();
else if (source.equals(jbColor2))
color = getRed();
else if (source.equals(jbColor3))
color = getGreen();
else if (source.equals(jbColor4))
color = getBlack();
else if (source.equals(jbColor5))
color = getYellow();
else if (source.equals(jbColor6))
color = getPink();
else if (source.equals(btnRectangle)) {
isSelectedReactangle = true;
isSelectedCircle = false;
isSelectedLine = false;
btnRectangle.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
btnLine.setBorder(UIManager.getBorder(defaultbtnLable));
btnCircle.setBorder(UIManager.getBorder(defaultbtnLable));
count = 0;
} else if (source.equals(btnCircle)) {
isSelectedReactangle = false;
isSelectedCircle = true;
isSelectedLine = false;
btnCircle.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
btnLine.setBorder(UIManager.getBorder(defaultbtnLable));
btnRectangle.setBorder(UIManager.getBorder(defaultbtnLable));
count = 0;
} else if (source.equals(btnLine)) {
isSelectedReactangle = false;
isSelectedCircle = false;
isSelectedLine = true;
btnLine.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
btnRectangle.setBorder(UIManager.getBorder(defaultbtnLable));
btnCircle.setBorder(UIManager.getBorder(defaultbtnLable));
count = 0;
} else if (source.equals(btnClear)) {
btnLine.setBorder(UIManager.getBorder(defaultbtnLable));
btnRectangle.setBorder(UIManager.getBorder(defaultbtnLable));
btnCircle.setBorder(UIManager.getBorder(defaultbtnLable));
panelContent.repaint();
}
}
I have some code but i would avoid if else statement. for example this snippet code is repeating few times.
btnLine.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
btnRectangle.setBorder(UIManager.getBorder(defaultbtnLable));
btnCircle.setBorder(UIManager.getBorder(defaultbtnLable));
Some tips and tricks how to avoid if condition so many times
2 Answers 2
Why would you have one actionPerformed()
method for multiple UI elements instead of creating each button with its individual Action
instance? Like:
Action clearAction = new AbstractAction("Clear") {
public void actionPerformed(ActionEvent e) {
btnLine.setBorder(UIManager.getBorder(defaultbtnLable));
btnRectangle.setBorder(UIManager.getBorder(defaultbtnLable));
btnCircle.setBorder(UIManager.getBorder(defaultbtnLable));
panelContent.repaint();
}
}
btnClear = new JButton(clearAction);
[Disclaimer: may contain typos, didn't check it in an IDE]
Then the Action is a first-class object, can be bound to multiple UI elements (e.g. a button and a main menu item) and contains both the name (plus an icon if you like) and the implementation.
And if the Actions
have common elements, maybe it's worth having them inherit from a common base Action
class.
-
\$\begingroup\$ Yes, common base
Action
classes are very useful. In fact, most often they don't need to inherit from those, but can instead be an instance of those by passing the right arguments to the constructor. \$\endgroup\$Simon Forsberg– Simon Forsberg2017年10月16日 16:15:17 +00:00Commented Oct 16, 2017 at 16:15
You might be able to put the code into a function:
void setSettings(YourClass object, boolean isSelectedReactangle, boolean isSelectedCircle, boolean isSelectedLine) {
object.isSelectedReactangle = false;
object.isSelectedCircle = true;
object.isSelectedLine = false;
btnCircle.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
btnLine.setBorder(UIManager.getBorder(defaultbtnLable));
btnRectangle.setBorder(UIManager.getBorder(defaultbtnLable));
count = 0;
}
You would still probably have to use the if/else, as switch statements can only switch on limited types of objects.
Also, Reactangle should probably be Rectangle.
getPink
,getBlack
etc methods? \$\endgroup\$