java - Adding JPanels to JFrame? -
the while loop in code below not displaying jpanels in gui frame. correct way this? appreciate help. don't worry code does.
import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; import javax.swing.*; public class green { public static int[] values = new int[100]; public final int color_range = 150; public static int count; jframe window = new jframe("green values"); static jpanel main_panel = new jpanel(); static jpanel green_panel; public green() { window.setsize(600, 600); window.setvisible(true); main_panel.setvisible(true); window.setdefaultcloseoperation(jframe.exit_on_close); window.pack(); } public static void main (string[] args) throws filenotfoundexception { green g = new green(); scanner sc = new scanner(new file("green.txt")); int = 0; while(sc.hasnextint()) { values[i++] = sc.nextint(); count++; jpanel green_panel = new jpanel(); // main_panel.add(green_panel); // - correct? sc.close(); int max = values[0]; int min = values[0]; (i = 0; < count; i++) { if (max < values[i]) { max = values[i]; } if (min > values[i]) { min = values[i]; } } int temp; int val = 0; (i = 0; < count; i++) { values[i] = val; system.out.println("array value: " + val + " | " + "green value: " + g.greenvalue(val, max, min)); temp = values[i+1]; val = temp; } } } public int greenvalue(int val, int max, int min) { int colorvalue; int arrayrange = (max - min); colorvalue = (((val-min) * color_range) / arrayrange) + 100; return colorvalue; } }
......................................................................................................................................................
what correct way this?
- avoid
static
, won't in long run - add
main_panel
frame
maybe more like...
import java.awt.eventqueue; import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; import javax.swing.jcomponent; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class green { public static int[] values = new int[100]; public final int color_range = 150; public static int count; jframe window = new jframe("green values"); jpanel main_panel = new jpanel(); jpanel green_panel; public green() { window.setsize(600, 600); window.add(main_panel); window.setdefaultcloseoperation(jframe.exit_on_close); window.pack(); window.setvisible(true); } public void add(jcomponent comp) { main_panel.add(comp); main_panel.revalidate(); main_panel.repaint(); } public static void main(string[] args) { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } green g = new green(); try { scanner sc = new scanner(new file("green.txt")); int = 0; while (sc.hasnextint()) { values[i++] = sc.nextint(); count++; jpanel green_panel = new jpanel(); // g.add(green_panel); // - correct? sc.close(); int max = values[0]; int min = values[0]; (i = 0; < count; i++) { if (max < values[i]) { max = values[i]; } if (min > values[i]) { min = values[i]; } } int temp; int val = 0; (i = 0; < count; i++) { values[i] = val; system.out.println("array value: " + val + " | " + "green value: " + g.greenvalue(val, max, min)); temp = values[i + 1]; val = temp; } } } catch (filenotfoundexception exp) { exp.printstacktrace(); } } }); } public int greenvalue(int val, int max, int min) { int colorvalue; int arrayrange = (max - min); colorvalue = (((val - min) * color_range) / arrayrange) + 100; return colorvalue; } }