2011-04-13 115 views
0

这是从主框架窗口我的代码:问题加载图像

public class DynamicalSystem { 


    public static void createAndShowGraphic() { 


    //Create and set up the window. 
    JFrame frame = new JFrame("Dynamical System: The beauty of Chaos"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    JLabel emptyLabel = new JLabel(""); 
    emptyLabel.setPreferredSize(new Dimension(500, 500)); 
    frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); 




    //Display the window. 

    MenuLook menubar = new MenuLook(); //display menubar 
    frame.setJMenuBar(menubar.createMenuBar()); 

    frame.pack(); 
    frame.setVisible(true); 




} 
} 

,这是从我的bufferdimage:

public class LabelDemo extends JPanel 
{ 
//path of image 
private String path; 

//image object 
private Image img; 

public LabelDemo(String path) throws IOException 
{ 
//save path 
this.path = path; 

//load image 
img = ImageIO.read(new File(path)); 

} 

//override paint method of panel 
public void paint(Graphics g) 
{ 
//draw the image 
if(img != null) 
g.drawImage(img,0,0, this); 
} 

} 
//class image frame periexei tin methodo createloadimage i opoia pernei 
//to path apo ton filechooser kai kanei load tin eikona 

class ImageFrame{ 

    public static void createLoadImage(){ 

     try 
    { 

     //create frame 
     JFrame f = new JFrame(); 

     //ask for image file 
     JFileChooser chooser = new JFileChooser(); 
     chooser.showOpenDialog(f); 

     //create panel with selected file 
     LabelDemo panel = new LabelDemo(chooser.getSelectedFile().getPath()); 

     //add panel to pane 
     f.getContentPane().add(panel); 


     //show frame 
     f.setBounds(0,0,800,800); 
     f.setVisible(true); 
    } 
     catch(Exception e) 
     { 
     System.out.println ("Den dialeksate eikona!"); 
     } 
    } 
} 

我想要的形象在我的主开窗户不在一个新的。我如何做到这一点?

+1

你的Shift键似乎坏了。至少在你输入标题时。 – 2011-04-13 16:39:55

+0

你是什么意思,我怎么修复它 – MoglisSs 2011-04-13 16:52:19

+0

@user:我只是在开玩笑说你写了所有大写字母的标题。这被广泛接受为互联网版本的呐喊,这不是很礼貌。 – 2011-04-13 16:55:40

回答

1

也许我错过了一些东西,但它看起来像没有任何意义,你是不是真的把你的图像放在主框架中,而你的问题似乎是DynamicalSystem。相反,它看起来像是在ImageFrame中创建一个新窗口并将图像放在那里。尝试调用

LabelDemo panel = new LabelDemo(chooser.getSelectedFile().getPath());

从DynamicalSystem并把LabelDemo在该框架而不是IMAGEFRAME的。

+0

我试过了,但是我对选择器有问题 – MoglisSs 2011-04-13 18:31:44

+0

什么样的问题? – 2011-04-14 19:25:01