swing - Simplest Gui Test Java repaint -


it's simple question, maybe don't understand tutorial i'm reading from. i've been stuck on while. program simple gets aside "hello world". i'm trying this: when user clicks button, "o" moves right. simple enough, put repaint()? need add something.repaint(); repaint screen or itself? nested class problem? t_t making me miserable how no 1 seems have problem can't comprehend. in advance.

import java.awt.*; import java.awt.event.*; import javax.swing.*;  public class guitest {      static int x = 20;      private static class movetest extends jpanel {          public void paintcomponent(graphics g) {             super.paintcomponent(g);             g.drawstring("o", x, 30);         }     }      private static class buttonhandler implements actionlistener {          public void actionperformed(actionevent e) {             x += 1;         }     }      public static void main(string[] args) {         movetest displaypanel = new movetest();         jbutton okbutton = new jbutton("move");         buttonhandler listener = new buttonhandler();         okbutton.addactionlistener(listener);          jpanel content = new jpanel();         content.setlayout(new borderlayout());         content.add(displaypanel, borderlayout.center);         content.add(okbutton, borderlayout.south);          jframe window = new jframe("gui test");         window.setcontentpane(content);         window.setsize(250, 100);         window.setlocation(100, 100);         window.setvisible(true);     } } 

you need component call repaint() on. simplest solution call repaint() this:

((jcomponent)e.getsource()).gettoplevelancestor().repaint(); 

the issue actionlistener declared static member class, not have access non-static members of enclosing class because not associated instance of enclosing class. put of gui initialization code inside constructor own jpanel subclasses. use anonymous inner classes listeners. can use named classes long not static. can call jpanel methods inside the listeners methods.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -