swing - java JPanel setSize and setLocation -


hey second post don't mad @ me i'm having issues jpanel in java. trying set size , location won't work, have tried repaint(); not work. help?

here's code:

package test.test.test;  import java.awt.color; import java.awt.flowlayout; import java.awt.textfield;  import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel;  public class test extends jframe  {    jpanel colorpanel = new jpanel();    public display(){     super("jpanel test");     setlayout(new flowlayout());     add(colorpanel);     colorpanel.setbackground(color.cyan);     colorpanel.setsize(300, 300);     repaint();   }  } 

for benefit of reading question later, here's short, self contained, correct example of defining jpanel background color.

almost time, should let swing component layout manager determine size of swing components. in case, define preferred size of jpanel because jpanel not contain other swing components.

the default layout manager of jframe borderlayout. jpanel positioned in center of borderlayout.

import java.awt.color; import java.awt.dimension;  import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities;  public class simplepanel implements runnable {      @override     public void run() {         jframe frame = new jframe("jpanel test");         frame.setdefaultcloseoperation(jframe.exit_on_close);          jpanel colorpanel = new jpanel();         colorpanel.setbackground(color.cyan);         colorpanel.setpreferredsize(new dimension(300, 300));          frame.add(colorpanel);          frame.pack();         frame.setvisible(true);     }      public static void main(string[] args) {         swingutilities.invokelater(new simplepanel());     }  } 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -