java - Repaint not working -
i'm having problem repaint function. in jframe pass word jpanel.
package grafisch; import logica.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; public class lingoapp extends javax.swing.jframe implements actionlistener { /** * creates new form lingoapp */ letter letter = new letter(); woordenlijst woordenlijst = new woordenlijst(); lingo lingo = new lingo(); newjpanel newjpanel = new newjpanel(); public lingoapp() { initcomponents(); volgendeknop.setenabled(false); controleerknop.setenabled(false); gokinput.addactionlistener(this); } private void controleerknopactionperformed(java.awt.event.actionevent evt) { string input = gokinput.gettext().replaceall("\\s",""); if(input.contains("0123456789") || input.length() <5 || input.length() >5 || input.isempty()){ gokinput.settext("fout"); } else { letter.checkletters(input); lingo.vermeerdergok(); beurtlabelhoeveelheid.settext(integer.tostring(lingo.geefgokken())); ///here go newjpanel.setword(input); } if(!lingo.checkgokken()){ allerleilabel.settext("helaas, het woord was: "+ letter.geefwoord()+"."); controleerknop.setenabled(false); volgendeknop.setenabled(true); } if(lingo.checkwoord(input)){ allerleilabel.settext("proficiat, het woord inderdaad "+ letter.geefwoord()+"."); beurtlabelhoeveelheid.settext("6");//interface fix scorelabelhoeveelheid.settext(integer.tostring(lingo.updatescore())); lingo.vermeerderbeurt(); controleerknop.setenabled(false); volgendeknop.setenabled(true); } if(lingo.checkbeurten()){ if(lingo.checkgoedgeraden()){ system.out.println("ditmoetnogwordengedaan, 10x goed geraden"); } else { system.out.println("ditmoetnogwordengedaan, !10x goed geraden"); } volgendeknop.setenabled(false); opgevenknop.setvisible(true); startspelknop.setvisible(true); } } private void startspelknopactionperformed(java.awt.event.actionevent evt) { gokinput.settext("");//fix voor nieuwe ronde allerleilabel.settext("");//fix voor nieuwe ronde opgevenknop.setvisible(false); startspelknop.setvisible(false); woordenlijst.maaklijst(); string temp = woordenlijst.geefwillekeurigwoord(); letter.setwoord(temp); lingo.setwoord(temp); controleerknop.setenabled(true); //maakt lijst bij start spel nadat op de knop gedrukt.de knop zal dan ook verdwijnen. //geef woord door aan de andere klasses. } private void volgendeknopactionperformed(java.awt.event.actionevent evt) { ///add reset code pls string temp = woordenlijst.geefwillekeurigwoord(); letter.setwoord(temp); lingo.setwoord(temp); controleerknop.setenabled(true); volgendeknop.setenabled(false); allerleilabel.settext(""); gokinput.settext(""); lingo.softreset(); } private void opgevenknopactionperformed(java.awt.event.actionevent evt) { // todo add handling code here: dispose(); } /** * @param args command line arguments */ public static void main(string args[]) { /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { public void run() { new lingoapp().setvisible(true); } }); } // variables declaration - not modify private javax.swing.jlabel allerleilabel; private javax.swing.jlabel beurtlabel; private javax.swing.jlabel beurtlabelhoeveelheid; private javax.swing.jbutton controleerknop; private javax.swing.jtextfield gokinput; private javax.swing.jlabel goklabel; private javax.swing.jbutton opgevenknop; private javax.swing.jlabel scorelabel; private javax.swing.jlabel scorelabelhoeveelheid; private javax.swing.jbutton startspelknop; private javax.swing.jbutton volgendeknop; private javax.swing.jlabel jlabel1; private javax.swing.jlabel jlabel2; private javax.swing.jlabel jlabel3; private javax.swing.jlabel jlabel4; private javax.swing.jlabel jlabel5; private javax.swing.jpanel jpanel1; // end of variables declaration @override public void actionperformed(actionevent ae) { controleerknop.doclick(); //eventlistener implementatie } }
so i'm trying draw simple string inside jpanel. method setword gets input jframe clicking button. inside jpanel;
package grafisch; import java.awt.dimension; import java.awt.font; import java.awt.graphics; import javax.swing.jpanel; public class newjpanel extends javax.swing.jpanel { private string word = "example"; /** * creates new form newjpanel */ public void setword(string input){ this.word=input; repaint(); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); g.drawstring(word, 75, 100); // changed location make easier see } public newjpanel() { initcomponents(); } }
note don't think code you've posted shows source of bug, , if put in effort create , post minimal example, you'll decent answer, without this, question remain unanswered. quite possibly may have 2 instances of class holds code above, 1 displayed, , 1 not displayed, , calling getword(string input)
on non-displayed one, wild guess. aside, consider changing name of method since not true "getter" method rather "setter" method , should renamed setword(string word)
.
all can @ point problem not in code you've posted. proof of concept, modify posted code working compilable class:
import java.awt.dimension; import java.awt.font; import java.awt.graphics; import javax.swing.jpanel; @suppresswarnings("serial") class showwordpanel extends jpanel { private static final int pref_w = 500; private static final int pref_h = 250; private static final font word_font = new font(font.dialog, font.bold, 20); private string word = "example"; // change name of method getxxx setxxx public void setword(string input) { this.word = input; repaint(); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); g.setfont(word_font); // let's make big enough see g.drawstring(word, 75, 100); // changed location make easier see } @override // jpanel wil big enough show text public dimension getpreferredsize() { if (ispreferredsizeset()) { return super.getpreferredsize(); } return new dimension(pref_w, pref_h); } }
then can use above class in gui creation class, , works fine:
import java.awt.borderlayout; import java.awt.event.actionevent; import javax.swing.*; @suppresswarnings({ "serial"}) public class showwordpaneltest extends jpanel { // create showwordpanel field -- guarantee // instance displayed same 1 change // value of word field private showwordpanel showwordpanel = new showwordpanel(); private jtextfield textfield = new jtextfield(10); private addwordaction addwordaction = new addwordaction("add word"); public showwordpaneltest() { textfield.setaction(addwordaction); jpanel toppanel = new jpanel(); toppanel.add(textfield); toppanel.add(new jbutton(addwordaction)); showwordpanel.setborder(borderfactory.createtitledborder("show word panel")); setlayout(new borderlayout()); add(showwordpanel, borderlayout.center); add(toppanel, borderlayout.page_start); } private class addwordaction extends abstractaction { public addwordaction(string name) { super(name); int mnemonic = (int) name.charat(0); putvalue(mnemonic_key, mnemonic); } @override public void actionperformed(actionevent e) { string text = textfield.gettext(); // text showwordpanel.setword(text); // pass showwordpanel // make easy change text after button press textfield.selectall(); textfield.requestfocusinwindow(); } } private static void createandshowgui() { showwordpaneltest mainpanel = new showwordpaneltest(); jframe frame = new jframe("showwordpaneltest"); frame.setdefaultcloseoperation(jframe.dispose_on_close); frame.getcontentpane().add(mainpanel); frame.pack(); frame.setlocationbyplatform(true); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } }
Comments
Post a Comment