eclipse - Java Swing Blank JFrame coming up? -
i'm new swing, , wondering why application comes blank, , displays components. seems sporadic. there no exceptions thrown or that. comes blank jframe. @ times when close application , run again, shows components correctly, comes blank. doing wrong in code? i'm using eclipse ide, if matters. here's code:
import java.applet.applet; import java.awt.borderlayout; import java.awt.flowlayout; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.io.file; import java.io.filenotfoundexception; import java.util.arraylist; import java.util.scanner; import javax.swing.*; public class main extends jframe implements actionlistener { private static final long serialversionuid = 1l; jradiobutton randomradiobutton; jradiobutton uniqueradiobutton; jradiobutton participationradiobutton; arraylist<student> allstudents; jframe mainframe; public main(){ allstudents = new arraylist<student>(); processallstudents(); mainframe = new jframe(); mainframe.setvisible(true); mainframe.setsize(250, 400); mainframe.setdefaultcloseoperation(jframe.exit_on_close); jpanel componentholder = new jpanel(); componentholder.setlayout(new gridlayout(5,1)); jlabel titletext = new jlabel(" randomizer"); componentholder.add(titletext); jbutton picker = new jbutton("pick student"); jfilechooser filepick = new jfilechooser(); filepick.addactionlistener(this); buttongroup allradiobuttons = new buttongroup(); randomradiobutton = new jradiobutton("completely random"); uniqueradiobutton = new jradiobutton("unique"); participationradiobutton = new jradiobutton("complete participation"); allradiobuttons.add(randomradiobutton); allradiobuttons.add(uniqueradiobutton); allradiobuttons.add(participationradiobutton); componentholder.add(randomradiobutton); componentholder.add(uniqueradiobutton); componentholder.add(participationradiobutton); picker.addactionlistener(this); componentholder.add(picker); componentholder.add(filepick); mainframe.add(componentholder); } public void actionperformed(actionevent e){ if(e.getactioncommand().equals("pick student")){ if(randomradiobutton.isselected()){ student result = getstudentrandom(); result.increment(); string resultstring = new string(result.getname() + ", " + result.getfrequency()); system.out.println(resultstring); jlabel resultlabel = new jlabel(resultstring); joptionpane.showmessagedialog(mainframe, resultlabel); } else if(uniqueradiobutton.isselected()){ student firststudent = getstudentrandom(); student secondstudent = getstudentrandom(); student result; if(firststudent.getname().equals(secondstudent.getname())){ result = secondstudent; } else{ result = firststudent; } result.increment(); string resultstring = new string(result.getname() + ", " + result.getfrequency()); system.out.println(resultstring); jlabel resultlabel = new jlabel(resultstring); joptionpane.showmessagedialog(mainframe, resultlabel); } else if(participationradiobutton.isselected()){ student result = selectstudentparticipant(); result.increment(); joptionpane.showmessagedialog(mainframe, result.getname() + ", " + result.getfrequency()); } } else system.out.println("error."); } public void processallstudents(){ file f = new file("test.txt"); scanner scanfile = null; try { scanfile = new scanner(f); } catch (filenotfoundexception e) { system.out.println("file not found"); } while(scanfile.hasnext()){ string name = scanfile.next(); int frequency = scanfile.nextint(); student s = new student(name, frequency); allstudents.add(s); } } public student getstudentrandom(){ int result = (int) (math.random() * allstudents.size()); return allstudents.get(result); } public student selectstudentparticipant(){ student temp = null; //start of bubble sort algorithm for(int = 0; < allstudents.size() - 1; i++){ student firststudent = allstudents.get(i); student secondstudent = allstudents.get(i+1); if(firststudent.getfrequency() > secondstudent.getfrequency()){ temp = firststudent; firststudent = secondstudent; secondstudent = temp; } } //end of bubble sort algorithm. data structure sorted increasing int firstrandom = (int) (math.random() * (allstudents.size()/2) + 0.2 * allstudents.size()); //likely bigger int secondrandom = (int) (math.random() * (allstudents.size()/2)); int randomindex = 0; //used represent random index if(firstrandom > secondrandom){ //more likely. selects first half of list randomindex = (int) (math.random() * allstudents.size()/2); } else if(firstrandom < secondrandom){ //possible, less randomindex = (int) ((math.random() * allstudents.size()/2) + allstudents.size()/2); } else{ //if 2 random numbers same randomindex = (int) (math.random() * allstudents.size()/2); } return allstudents.get(randomindex); } public static void main(string[] args){ new main(); } }
setvisible(true)
has last method called in main method. otherwise nothing may render on jframe
Comments
Post a Comment