Java Images in the GUI class -


hello developing custom clock application.

my gui works find functionality fine have 1 problem 3 days now. can't gui display image in background without hiding components.

here code of gui class

public void makeframe()  {     contentpane.setlayout(new borderlayout());     contentpane.add(panel1, borderlayout.north);     contentpane.add(panel2, borderlayout.center);     contentpane.add(panel3, borderlayout.south);     contentpane.add(panel4, borderlayout.west);     contentpane.add(panel5, borderlayout.east);     panel1.add(label1);     panel2.setlayout(new gridlayout(3,4));     panel2.add(time);     panel2.add(label2);     panel2.add(stopwatch);     panel3.setlayout(new flowlayout());     panel4.setlayout(new flowlayout());     panel5.add(alarm);     panel5.add(change);     panel5.setlayout(new flowlayout());     label1.setfont(new font("arial", font.plain, 90));     label1.setforeground(color.blue);     label2.setfont(new font("arial", font.plain, 70));     label2.setforeground(color.red);     time.seteditable(true);     time.settext("sample time: n/ 13:45:23 ");     time.setfont(new font("arial", font.plain, 60));     stopwatch.setfont(new font("arial", font.plain, 45));     stopwatch.setsize(20,20);     stopwatch.settext("00 : 00 : 00");     stopwatch.seteditable(false);     stopwatch.add(rounds);      frame = new jframe("clock");     frame.setlayout(null);     frame.setsize(600,900);     paint();     frame.setcontentpane(contentpane);     makemenu();     combobox();     stopwatch();      frame.setdefaultcloseoperation(jframe.exit_on_close);     frame.validate();     frame.pack();     frame.setvisible(true);     frame.setlocationrelativeto(null);   } 

and paint() method

 public void paint() {      bufferedimage img = null;         try {             //load image             img = imageio.read(new file("c:/users/user/workspace/alarm clock/src/clock/clock.jpg"));              imageicon image = new imageicon(img);             jlabel label = new jlabel(image);             frame.setcontentpane(label);          } catch (ioexception e) {          }  } 

contentpane.add(panel1, borderlayout.north); contentpane.add(panel2, borderlayout.center); 

your code adding panels content pane, fine.

frame.setcontentpane(label); 

but in paint() method replace content pane label, lose original panels.

first of all, should not override paint() on jframe. if ever custom painting should override paintcomponent() method of jpanel , add panel frame. also, painting method painting only, should nvever create , add component gui in painting method. should not read image in painting method. painting should efficient.

so solve problem suggest can use backgroundpanel. custom panel supports painting of images , make component add non-opaque. can paint background image 1) @ original size, 2) scaled fill panel, 3) tiled.

the basic code be:

//contentpane.setlayout(new borderlayout()); backgroundpanel contentpane = new backgroundpanel(...); // choose constructor frame.setcontentpane( contentpane ); contentpane.add(panel1, borderlayout.north); ... 

Comments