swing - How to add actions to a button in java -
i have been working java buttons, , have created button ,but when click button, want shape of object change. code i've worked on
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class shapes { public static void main(string[] a) { jframe f = new jframe("change shapes"); f.setdefaultcloseoperation(jframe.exit_on_close); jbutton b = new jbutton("shapes change"); f.getcontentpane().add(b); f.pack(); f.setvisible(true); } public void paint (graphics g) { //no clue here } private static abstract class mybutton extends jbutton implements actionlistener { mybutton() { addactionlistener(this); } public void actionperformed(actionevent e) { if (e.getsource() == b) { //no clue here } } } }
at first, there shape created, once button clicked want change shape.
there should need subclass jbutton
. if want customise button, use action
api instead, see how use actions.
to perform custom painting should extend swing component jcomponent
or jpanel
, override paintcomponent
method...
see performing custom painting more details.
you need provide method call tell component shape should change how shape should changed.
you provide means buttons actionlistener
reference instance of paint panel , call these methods...
Comments
Post a Comment