user interface - Problem with Java GUI implementation -
i have problem code trying run - trying make 3 buttons, put them on gui, , have first buttons colour changed orange, , buttons next colour change white , green. every click thereafter result in colours moving 1 button right. code far follows, skipping colours in places , not behaving @ expected. can offer help/guidance please ?
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class buttonjava extends jbutton implements actionlistener { private int currentcolor=-1; private int clicks=0; private static final color[] colors = { color.orange, color.white, color.green }; private static buttonjava[] buttons; public buttonjava( ){ setbackground( color.yellow ); settext( "pick me" ); this.addactionlistener( ); } public static void main(string[] args) { jframe frame = new jframe ("jframe"); jpanel panel = new jpanel( ); frame.setdefaultcloseoperation( jframe.exit_on_close); buttons = new buttonjava[3]; for(int = 0;i<buttons.length ; i++){ buttons[i] = new buttonjava(); panel.add(buttons[i]); } frame.getcontentpane( ).add( panel ); frame.setsize( 500, 500); frame.setvisible( true ); } private void updatebutton() { clicks++; changecolors(); // settext( ); } private void changecolors( ) { (int i=buttons.length-1;i>=0;i--){ buttons[i].currentcolor = nextcolor(currentcolor); buttons[i].setbackground(colors[buttons[i].currentcolor]); buttons[i].settext(("# of clicks = " + buttons[i].getclicks() ) ); } } private integer getclicks() { return clicks; } private int nextcolor( int curcol ) { final int collen = colors.length; curcol--; curcol = (collen + curcol % collen) % collen; return curcol; } private void firstclick( actionevent event ) { int curcol = 0; (int i=buttons.length-1;i>=0;i--){ if ( buttons[i] == event.getsource() ) { buttons[i].currentcolor = curcol; curcol++; currentcolor++; } }} @override public void actionperformed( actionevent event ) { if ( -1 == currentcolor ) { firstclick( event ); } updatebutton( ); } }
thank :)
you have couple issues code posted, boil down being clear member of class(static) , member of instance.
for starters, buttons
array exists inside main method , can't accessed changecolors()
. along same lines, since changecolors()
instance method, setbackground()
needs called directly on button in array. written setting color 1 button 3 times.
additionally, logic in changecolors()
not rotating currentcolor
index. need both increase counter , ensure wraps length of color array. if arrays same size, need make sure there addition make colors cycle.
private static void changecolors( ) { (int i=0;i<buttons.length;i++){ buttons[i].setbackground(colors[currentcolor]); currentcolor = nextcolor(currentcolor); } if (buttons.length == colors.length) { currentcolor = nextcolor(currentcolor); } } private static int nextcolor(int currentcolor) { return (currentcolor+1)% colors.length; }
edit new code:
i'm not sure why re-wrote nextcolor()
, posted worked. in general, feel running issues because code not partitioned tasks trying achieve. have code related specific button instance , code related controlling buttons mixing together.
with following implementation, issue of how many times button clicked self-contained in button class. every button press calls 1 method in owning panel. method knows how many buttons there , color of first button. , each subsequent button contain next color in list, wrapping when necessary.
public class rotatebuttons extends jpanel { private static final color[] colors = { color.orange, color.white, color.green }; private static final int button_count = 3; private jbutton[] _buttons; private int _currentcolor = 0; public static void main(string[] args) { jframe frame = new jframe("jframe"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(new rotatebuttons()); frame.setsize(500, 500); frame.setvisible(true); } public rotatebuttons() { _buttons = new jbutton[button_count]; (int = 0; < _buttons.length; i++) { _buttons[i] = new countbutton(); add(_buttons[i]); } } private void rotatebuttons() { (jbutton button : _buttons) { button.setbackground(colors[_currentcolor]); _currentcolor = nextcolor(_currentcolor); } if (_buttons.length == colors.length) { _currentcolor = nextcolor(_currentcolor); } } private int nextcolor(int currentcolor) { return (currentcolor + 1) % colors.length; } private class countbutton extends jbutton { private int _count = 0; public countbutton() { setbackground(color.yellow); settext("pick me"); addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { _count++; settext("# of clicks = " + _count); rotatebuttons(); } }); } } }
2nd edit:
shows changes shift _currentcolor
necessary amount on first click.
public class rotatebuttons extends jpanel { ... private boolean _firstclick = true; ... private void rotatebuttons(countbutton source) { if (_firstclick) { _firstclick = false; boolean foundsource = false; (int = 0; < _buttons.length; i++) { if (foundsource) { _currentcolor = nextcolor(_currentcolor); } else { foundsource = _buttons[i] == source; } } } ... } private class countbutton extends jbutton { ... public countbutton() { ... addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { ... rotatebuttons(countbutton.this); } }); } }
Comments
Post a Comment