java - How can I generalize these repetitive blocks of code? -
code near-identical blocks makes me cringe. plus adds have thousand lines of code half suffice. surely there way make loop make happen , not have code looks unsophisticated , brainless.
offhand seems adding code seek reduce: loop make 5 buttons, array of labels buttons, array of backgrounds... maybe more. if turned out acceptable, how make loop handle listeners? i can't have array of methods, can i? guess such loop have include switch
. yes? i'd if didn't want seek better solution. i'm asking...
what code listen entire group of buttons , take action based on 1 pressed? component assign single listener? , how?
(there's chance answer question make me cringe more repetitive nature of code, if realize know how , needn't have asked in first place, i'm asking anyway. i'm @ 1 of i've-had-it-for-today points brain wants out.)
private void makescoremasterbonuses(){ pnlbonuses = new jpanel(new gridlayout(1, 6)); pnlbonuses.setsize(6,1); jbutton t1 = (new jbutton("3w")); t1.settooltiptext("this triple-word cell."); t1.setbackground(triple_word); t1.sethorizontalalignment(jbutton.center); t1.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { highlighter.shadesymmetric(currentcell,triple_word); }}); jbutton t2 = (new jbutton("3l")); t2.settooltiptext("this triple-letter cell"); t2.setbackground(triple_letter); t2.sethorizontalalignment(jbutton.center); t2.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { highlighter.shadesymmetric(currentcell,triple_letter); }}); jbutton t3 = (new jbutton("2w")); t3.settooltiptext("this double-word cell"); t3.setbackground(double_word); t3.sethorizontalalignment(jbutton.center); t3.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { highlighter.shadesymmetric(currentcell,double_word); }}); jbutton t4 = (new jbutton("2l")); t4.settooltiptext("this double-letter cell"); t4.setbackground(double_letter); t4.sethorizontalalignment(jbutton.center); t4.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { highlighter.shadesymmetric(currentcell,double_letter); }}); jbutton t5 = (new jbutton("")); t5.settooltiptext("no bonus"); t5.setbackground(white); t5.sethorizontalalignment(jbutton.center); t5.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { highlighter.shadesymmetric(currentcell,b_normal); }}); pnlbonuses.add(new jlabel("legend: ")); pnlbonuses.add(t1); pnlbonuses.add(t2); pnlbonuses.add(t3); pnlbonuses.add(t4); pnlbonuses.add(t5); }
i'm not asking write code; wouldn't want (but couldn't ignore it!).
here's code above does:
generally time have repeated functionality that, want extract code out helper method this:
private jbutton makejbutton(string label, string tooltip, color bgcolor, final color highlight) { jbutton button = new jbutton(label); button.settooltiptext(tooltip); button.setbackground(bgcolor); button.sethorizontalalignment(jbutton.center); button.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { highlighter.shadesymmetric(currentcell, highlight); } }); return button; }
then makescoremasterbonuses()
method becomes simpler:
private void makescoremasterbonuses() { pnlbonuses = new jpanel(new gridlayout(1, 6)); pnlbonuses.setsize(6, 1); pnlbonuses.add(new jlabel("legend: ")); pnlbonuses.add(makejbutton("3w", "this triple-word cell.", triple_word, triple_word)); pnlbonuses.add(makejbutton("3l", "this triple-letter cell.", triple_letter, triple_letter)); pnlbonuses.add(makejbutton("2w", "this double-word cell.", double_word, double_word)); pnlbonuses.add(makejbutton("3l", "this double-letter cell.", double_letter, double_letter)); pnlbonuses.add(makejbutton("", "no bonus.", white, b_normal)); }
Comments
Post a Comment