swing - Java cant make TempListener work -
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class circlepanel extends jpanel { private jtextfield xfield, yfield, diameterfield; private jbutton redraw; private jlabel xlabel, ylabel, rlabel; circle mycircle = new circle (150, 150, 30, color.red, color.white); graphics g; //paint objects on panel public void paintcomponent (graphics page) { super.paintcomponent(page); g = page; mycircle.draw(g); } public circlepanel(){ xlabel = new jlabel("x= "); ylabel = new jlabel("y= "); rlabel = new jlabel("r= "); xfield = new jtextfield(5); xfield.addactionlistener(new templistener()); yfield = new jtextfield(5); yfield.addactionlistener(new templistener()); diameterfield = new jtextfield(5); diameterfield.addactionlistener(new templistener()); redraw = new jbutton("redraw!"); redraw.addactionlistener(new buttonlistener()); add(xlabel); add(xfield); add(ylabel); add(yfield); add(rlabel); add(diameterfield); add(redraw); setpreferredsize(new dimension(500, 500)); setbackground(color.white); } private class buttonlistener implements actionlistener{ public void actionperformed (actionevent event) { //update page mycircle.draw(g); //repaint panel repaint(); } private class templistener implements actionlistener { public void actionperformed(actionevent event) { int x, y, newbase, newhei; string text = xfield.gettext(); string text2 = yfield.gettext(); x = integer.parseint (text); y = integer.parseint (text2); mycircle.draw(g); repaint(); } } } }
hi guys i'm tring make java application draw circle , redraw new values in jtextfield. wrote 3 class it. 1 of them contain accessor, mutators, constructor. 1 of class has main method of course , 1 of class above. templistener not working. can me?
you should graphics field, g, out of program. instead use local graphics variable, 1 call page
inside of paintcomponent method, don't use anywhere else.
suggestions:
- have actionlistener make changes state of mycircle object, code not this.
- then have call
repaint()
. - get this,
mycircle.draw(g);
out of actionlistener doesn't belong there. - read swing graphics tutorials: lesson: performing custom painting
Comments
Post a Comment