java - JScrollPane scrollbar doesnt work after GridBagLayout -
i making auto chat client cleverbot school. have 2 problems... 1) scroll bar doesn't seem work reason. here screenshot:
import javax.swing.*; import java.awt.*; import java.awt.event.keylistener; import java.awt.event.keyevent; import java.lang.math; public class chatbot extends jframe implements keylistener{ //main method public static void main(string[] args){ new chatbot(); } //swing settings jpanel window=new jpanel(){ protected void paintcomponent(graphics g){ super.paintcomponent(g); image background = new imageicon("texteffect.png").getimage(); int x = (window.getwidth() - background.getwidth(null)) / 2; int y = (window.getheight() - background.getheight(null)) / 2; g.drawimage(background,x,y,null,this); } }; jlabel label=new jlabel("say: "); jtextarea dialog=new jtextarea(); jtextfield input=new jtextfield(46); jscrollpane scroll=new jscrollpane( dialog, jscrollpane.vertical_scrollbar_as_needed, jscrollpane.horizontal_scrollbar_never ); //makes window , starts bot public chatbot(){ super("pollockoraptor"); setsize(600,400); setresizable(true); setdefaultcloseoperation(exit_on_close); dialog.seteditable(false); dialog.setlinewrap(true); dialog.setopaque(false); scroll.getviewport().setopaque(false); input.addkeylistener(this); window.add(scroll); window.add(label); window.add(input); //background color; new color(97, 118, 131) nice color window.setbackground(new color(255, 255, 255)); add(window); setvisible(true); //gui layout window.setlayout(new gridbaglayout()); gridbagconstraints c = new gridbagconstraints(); c.fill = gridbagconstraints.horizontal; //dialog c.weightx = 1.0; c.weighty = 1.0; c.anchor = gridbagconstraints.page_start; c.fill = gridbagconstraints.both; c.insets = new insets(10, 10, 0, 10); c.gridx = 0; c.gridy = 0; c.gridwidth = 3; c.gridheight = 2; window.add(dialog, c); //input box c.weightx = 0; c.weighty = 0; c.anchor = gridbagconstraints.page_end; c.fill = gridbagconstraints.horizontal; c.insets = new insets(0, 0, 10, 10); c.gridx = 1; c.gridy = 2; c.gridwidth = gridbagconstraints.remainder; window.add(input, c); //label c.weightx = 0; c.weighty = 0; c.anchor = gridbagconstraints.page_end; c.fill = gridbagconstraints.horizontal; c.insets = new insets(0, 10, 10, 0); c.gridx = 0; c.gridy = 2; c.gridwidth = 1; window.add(label, c); input.requestfocus(); } //knowledgebase string[][] knowledgebase={ {"hi","hello","howdy","hey"}, {"hi","hello","hey"}, {"how you", "how r u", "how r you", "how u"}, {"good","doing well"}, {"shut up","noob","stop talking"} }; //what when come in pressed public void keypressed(keyevent e){ if(e.getkeycode()==keyevent.vk_enter){ input.seteditable(false); //get user input string quote=input.gettext(); input.settext(""); if(!quote.equals("")){ addtext("you:\t"+quote); quote.trim(); while(quote.charat(quote.length()-1)=='!' || quote.charat(quote.length()-1)=='.' || quote.charat(quote.length()-1)=='?'){ quote=quote.substring(0,quote.length()-1); } quote.trim(); byte response=0; int j=0; //check knowledgebase match or alter topic while(response==0){ //if match found, reply reply if(inarray(quote.tolowercase(),knowledgebase[j*2])){ response=2; int r=(int)math.floor(math.random()*knowledgebase[(j*2)+1].length); addtext("\npollockoraptor:\t"+knowledgebase[(j*2)+1][r]); } j++; //if match not found, go alter topic if(j*2==knowledgebase.length-1 && response==0){ response=1; } } //change topic if bot lost if(response==1){ int r=(int)math.floor(math.random()*knowledgebase[knowledgebase.length-1].length); addtext("\npollockoraptor:\t"+knowledgebase[knowledgebase.length-1][r]); } addtext("\n"); } } } //other events public void keytyped(keyevent e){} public void keyreleased(keyevent e){ if(e.getkeycode()==keyevent.vk_enter){ input.seteditable(true); } } //format input public void addtext(string str){ dialog.settext(dialog.gettext()+str); } //check knowledgebase match public boolean inarray(string in,string[] str){ boolean match=false; for(int i=0;i<str.length;i++){ if(str[i].equals(in)){ match=true; } } homecoming match; } }
i have else working, need way create database of responses can edit easily. how this? have utilize mysql or that? there easier way can can create matrix similar 1 have reading text file?
***************edit****************
import javax.swing.*; import java.awt.*; import java.awt.event.keylistener; import java.awt.event.keyevent; import java.lang.math; public class chatbot extends jframe implements keylistener{ //main method public static void main(string[] args){ new chatbot(); } //swing settings jpanel window=new jpanel(){ protected void paintcomponent(graphics g){ super.paintcomponent(g); image background = new imageicon("texteffect.png").getimage(); int x = (window.getwidth() - background.getwidth(null)) / 2; int y = (window.getheight() - background.getheight(null)) / 2; g.drawimage(background,x,y,null,this); } }; jlabel label=new jlabel("say: "); jtextarea dialog=new jtextarea(5,30); jtextfield input=new jtextfield(46); jscrollpane scroll=new jscrollpane( dialog, jscrollpane.vertical_scrollbar_as_needed, jscrollpane.horizontal_scrollbar_never ); //makes window , starts bot public chatbot(){ super("pollockoraptor"); setsize(600,400); setresizable(true); setdefaultcloseoperation(exit_on_close); dialog.seteditable(false); dialog.setlinewrap(true); dialog.setopaque(false); scroll.getviewport().setopaque(false); input.addkeylistener(this); window.add(scroll); //background color; new color(97, 118, 131) nice color window.setbackground(new color(255, 255, 255)); add(window); setvisible(true); //gui layout window.setlayout(new gridbaglayout()); gridbagconstraints c = new gridbagconstraints(); c.fill = gridbagconstraints.horizontal; //dialog c.weightx = 1.0; c.weighty = 1.0; c.anchor = gridbagconstraints.page_start; c.fill = gridbagconstraints.both; c.insets = new insets(10, 10, 0, 10); c.gridx = 0; c.gridy = 0; c.gridwidth = 3; c.gridheight = 2; window.add(scroll, c); //input box c.weightx = 0; c.weighty = 0; c.anchor = gridbagconstraints.page_end; c.fill = gridbagconstraints.horizontal; c.insets = new insets(0, 0, 10, 10); c.gridx = 1; c.gridy = 2; c.gridwidth = gridbagconstraints.remainder; window.add(input, c); //label c.weightx = 0; c.weighty = 0; c.anchor = gridbagconstraints.page_end; c.fill = gridbagconstraints.horizontal; c.insets = new insets(0, 10, 10, 0); c.gridx = 0; c.gridy = 2; c.gridwidth = 1; window.add(label, c); input.requestfocus(); } //knowledgebase string[][] knowledgebase={ {"hi","hello","howdy","hey"}, {"hi","hello","hey"}, {"how you", "how r u", "how r you", "how u"}, {"good","doing well"}, {"shut up","noob","stop talking"} }; //what when come in pressed public void keypressed(keyevent e){ if(e.getkeycode()==keyevent.vk_enter){ input.seteditable(false); //get user input string quote=input.gettext(); input.settext(""); if(!quote.equals("")){ addtext("you:\t"+quote); quote.trim(); while(quote.charat(quote.length()-1)=='!' || quote.charat(quote.length()-1)=='.' || quote.charat(quote.length()-1)=='?'){ quote=quote.substring(0,quote.length()-1); } quote.trim(); byte response=0; int j=0; //check knowledgebase match or alter topic while(response==0){ //if match found, reply reply if(inarray(quote.tolowercase(),knowledgebase[j*2])){ response=2; int r=(int)math.floor(math.random()*knowledgebase[(j*2)+1].length); addtext("\npollockoraptor:\t"+knowledgebase[(j*2)+1][r]); } j++; //if match not found, go alter topic if(j*2==knowledgebase.length-1 && response==0){ response=1; } } //change topic if bot lost if(response==1){ int r=(int)math.floor(math.random()*knowledgebase[knowledgebase.length-1].length); addtext("\npollockoraptor:\t"+knowledgebase[knowledgebase.length-1][r]); } addtext("\n"); } } } //other events public void keytyped(keyevent e){} public void keyreleased(keyevent e){ if(e.getkeycode()==keyevent.vk_enter){ input.seteditable(true); } } //format input public void addtext(string str){ dialog.append(str); } //check knowledgebase match public boolean inarray(string in,string[] str){ boolean match=false; for(int i=0;i<str.length;i++){ if(str[i].equals(in)){ match=true; } } homecoming match; } }
***************edit2****************
import javax.swing.*; import java.awt.*; import java.awt.event.actionlistener; import java.awt.event.actionevent; import java.lang.math; public class chatbot extends jframe implements actionlistener{ //main method public static void main(string[] args){ new chatbot(); } //swing settings jpanel window=new jpanel(){ protected void paintcomponent(graphics g){ super.paintcomponent(g); image background = new imageicon("texteffect.png").getimage(); int x = (window.getwidth() - background.getwidth(null)) / 2; int y = (window.getheight() - background.getheight(null)) / 2; g.drawimage(background,x,y,null,this); } }; jlabel label=new jlabel("say: "); jtextarea dialog=new jtextarea(5,30); jtextfield input=new jtextfield(46); jscrollpane scroll=new jscrollpane( dialog, jscrollpane.vertical_scrollbar_as_needed, jscrollpane.horizontal_scrollbar_never ); //makes window , starts bot public chatbot(){ super("pollockoraptor"); setsize(600,400); setresizable(true); setdefaultcloseoperation(exit_on_close); dialog.seteditable(false); dialog.setlinewrap(true); dialog.setopaque(false); scroll.getviewport().setopaque(false); input.addactionlistener(this); window.add(scroll); //background color; new color(97, 118, 131) nice color window.setbackground(new color(255, 255, 255)); add(window); setvisible(true); //gui layout window.setlayout(new gridbaglayout()); gridbagconstraints c = new gridbagconstraints(); c.fill = gridbagconstraints.horizontal; //dialog c.weightx = 1.0; c.weighty = 1.0; c.anchor = gridbagconstraints.page_start; c.fill = gridbagconstraints.both; c.insets = new insets(10, 10, 0, 10); c.gridx = 0; c.gridy = 0; c.gridwidth = 3; c.gridheight = 2; window.add(scroll, c); //input box c.weightx = 0; c.weighty = 0; c.anchor = gridbagconstraints.page_end; c.fill = gridbagconstraints.horizontal; c.insets = new insets(0, 0, 10, 10); c.gridx = 1; c.gridy = 2; c.gridwidth = gridbagconstraints.remainder; window.add(input, c); //label c.weightx = 0; c.weighty = 0; c.anchor = gridbagconstraints.page_end; c.fill = gridbagconstraints.horizontal; c.insets = new insets(0, 10, 10, 0); c.gridx = 0; c.gridy = 2; c.gridwidth = 1; window.add(label, c); input.requestfocus(); } //knowledgebase string[][] knowledgebase={ {"hi","hello","howdy","hey"}, {"hi","hello","hey"}, {"how you", "how r u", "how r you", "how u"}, {"good","doing well"}, {"shut up","noob","stop talking"} }; //what when come in pressed public void actionperformed(actionevent e){ //get user input string quote=input.gettext(); input.settext(""); if(!quote.equals("")){ addtext("you:\t"+quote); quote.trim(); while(quote.charat(quote.length()-1)=='!' || quote.charat(quote.length()-1)=='.' || quote.charat(quote.length()-1)=='?'){ quote=quote.substring(0,quote.length()-1); } quote.trim(); byte response=0; int j=0; //check knowledgebase match or alter topic while(response==0){ //if match found, reply reply if(inarray(quote.tolowercase(),knowledgebase[j*2])){ response=2; int r=(int)math.floor(math.random()*knowledgebase[(j*2)+1].length); addtext("\npollockoraptor:\t"+knowledgebase[(j*2)+1][r]); } j++; //if match not found, go alter topic if(j*2==knowledgebase.length-1 && response==0){ response=1; } } //change topic if bot lost if(response==1){ int r=(int)math.floor(math.random()*knowledgebase[knowledgebase.length-1].length); addtext("\npollockoraptor:\t"+knowledgebase[knowledgebase.length-1][r]); } addtext("\n"); } } //format input public void addtext(string str){ dialog.append(str); } //check knowledgebase match public boolean inarray(string in,string[] str){ boolean match=false; for(int i=0;i<str.length;i++){ if(str[i].equals(in)){ match=true; } } homecoming match; } }
sorry blunt, problem 1 of sloppy code.
you're adding dialog twice window container, 1 time in jscrollpane , 1 time itself.
public chatbot() { super("pollockoraptor"); setsize(600, 400); setresizable(true); setdefaultcloseoperation(exit_on_close); dialog.seteditable(false); dialog.setlinewrap(true); dialog.setopaque(false); scroll.getviewport().setopaque(false); input.addkeylistener(this); // **** adding bunch of junk **without** constraings?? window.add(scroll); // *** add together scrollpane *with* dialog here window.add(label); window.add(input); // ..... gridbagconstraints c = new gridbagconstraints(); c.fill = gridbagconstraints.horizontal; // dialog c.weightx = 1.0; c.weighty = 1.0; c.anchor = gridbagconstraints.page_start; c.fill = gridbagconstraints.both; c.insets = new insets(10, 10, 0, 10); c.gridx = 0; c.gridy = 0; c.gridwidth = 3; c.gridheight = 2; window.add(dialog, c); // *** add together dialog itself*** ???? wtf???
don't that. instead add together jscrollpane, add together jscrollpane container with gridbagconstraints , leave @ that.
you've got several other components you're adding window without gridbagconstraints, without thought, if you're randomly , sloppily coding without planning(?). don't either. stop, plan want code first, , create code. don't type out free-thought that's sloppy , never work. honest errors fine, sloppy coding, nope.
java mysql database jscrollpane gridbaglayout
No comments:
Post a Comment