为什么程序运行后图片面板无法显示啊?麻烦各位高手了!!!!import java.awt. *;
import java.awt.event. *;
import javax.swing. *;
import javax.swing.border. *;
import java.util. *;
public class PictureBrowse extends JFrame implements ItemListener,ActionListener{
String fname[]={"1.jpg","2.jpg","3.jpg","4.jpg","5.jpg",
"6.jpg","7.jpg","8.jpg","9.jpg","10.jpg"} ;
Browse pp;
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JButton next=new JButton("下页");
JButton prev=new JButton("上页");
JButton first=new JButton("首页");
JButton last=new JButton("尾页");
JComboBox comb=new JComboBox(fname);
int n=0;
public PictureBrowse(){
setSize(400,400);
setTitle("图片查看程序");
pp=new Browse(fname);
p2.add(next);p2.add(prev);p2.add(first);p2.add(last);p2.add(comb);
next.addActionListener(this);
prev.addActionListener(this);
first.addActionListener(this);
last.addActionListener(this);
comb.addItemListener(this);
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(pp);
this.getContentPane().add(p1,"Center");
this.getContentPane().add(p2,"South");
setVisible(true);
}
public void itemStateChanged(ItemEvent e){
pp.dd.show(pp,(String)comb.getSelectedItem());
}
public void actionPerformed(ActionEvent e){
int total=fname.length;
if(e.getSource()==next){pp.dd.next(pp);n=(n+1)%total;}
else if(e.getSource()==prev){pp.dd.previous(pp);n=(n-1+total)%total;}
else if(e.getSource()==first){pp.dd.first(pp);n=0;}
else if(e.getSource()==last){pp.dd.last(pp);n=total;}
}
public static void main(String args[]){
JFrame.setDefaultLookAndFeelDecorated(true);
Font font = new Font("JFrame",Font.PLAIN,14);
Enumeration keys=UIManager.getLookAndFeelDefaults().keys();
while(keys.hasMoreElements()){
Object key=keys.nextElement();
if(UIManager.get(key)instanceof Font)UIManager.put(key,font);
}
PictureBrowse mainFrame=new PictureBrowse();
}
}class Browse extends JPanel{
CardLayout dd=new CardLayout();
boolean f= false;
Picture pic;
public Browse(String fname[]){
setLayout(dd);
for(int i=0;i<fname.length;i++){
pic=new Picture(fname[i]);
add(fname[i],new JScrollPane(pic));
}
}
}
class Picture extends JPanel{
Image im;
MediaTracker tracker=new MediaTracker(this);
public Picture(String fname){
Toolkit tool=Toolkit.getDefaultToolkit();
im=tool.getImage(fname);
tracker.addImage(im,0);
try{
tracker.waitForAll();
}
catch(InterruptedException e){}
this.setPreferredSize(new Dimension(im.getWidth(this),im.getHeight(this)));
}
public void paintComponent(Graphics g){
g.drawImage(im,0,0,this);
}
}
import java.awt.event. *;
import javax.swing. *;
import javax.swing.border. *;
import java.util. *;
public class PictureBrowse extends JFrame implements ItemListener,ActionListener{
String fname[]={"1.jpg","2.jpg","3.jpg","4.jpg","5.jpg",
"6.jpg","7.jpg","8.jpg","9.jpg","10.jpg"} ;
Browse pp;
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JButton next=new JButton("下页");
JButton prev=new JButton("上页");
JButton first=new JButton("首页");
JButton last=new JButton("尾页");
JComboBox comb=new JComboBox(fname);
int n=0;
public PictureBrowse(){
setSize(400,400);
setTitle("图片查看程序");
pp=new Browse(fname);
p2.add(next);p2.add(prev);p2.add(first);p2.add(last);p2.add(comb);
next.addActionListener(this);
prev.addActionListener(this);
first.addActionListener(this);
last.addActionListener(this);
comb.addItemListener(this);
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(pp);
this.getContentPane().add(p1,"Center");
this.getContentPane().add(p2,"South");
setVisible(true);
}
public void itemStateChanged(ItemEvent e){
pp.dd.show(pp,(String)comb.getSelectedItem());
}
public void actionPerformed(ActionEvent e){
int total=fname.length;
if(e.getSource()==next){pp.dd.next(pp);n=(n+1)%total;}
else if(e.getSource()==prev){pp.dd.previous(pp);n=(n-1+total)%total;}
else if(e.getSource()==first){pp.dd.first(pp);n=0;}
else if(e.getSource()==last){pp.dd.last(pp);n=total;}
}
public static void main(String args[]){
JFrame.setDefaultLookAndFeelDecorated(true);
Font font = new Font("JFrame",Font.PLAIN,14);
Enumeration keys=UIManager.getLookAndFeelDefaults().keys();
while(keys.hasMoreElements()){
Object key=keys.nextElement();
if(UIManager.get(key)instanceof Font)UIManager.put(key,font);
}
PictureBrowse mainFrame=new PictureBrowse();
}
}class Browse extends JPanel{
CardLayout dd=new CardLayout();
boolean f= false;
Picture pic;
public Browse(String fname[]){
setLayout(dd);
for(int i=0;i<fname.length;i++){
pic=new Picture(fname[i]);
add(fname[i],new JScrollPane(pic));
}
}
}
class Picture extends JPanel{
Image im;
MediaTracker tracker=new MediaTracker(this);
public Picture(String fname){
Toolkit tool=Toolkit.getDefaultToolkit();
im=tool.getImage(fname);
tracker.addImage(im,0);
try{
tracker.waitForAll();
}
catch(InterruptedException e){}
this.setPreferredSize(new Dimension(im.getWidth(this),im.getHeight(this)));
}
public void paintComponent(Graphics g){
g.drawImage(im,0,0,this);
}
}
建议:构造方法中加上: this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);