开发者

Problem loading image in the correct frame

开发者 https://www.devze.com 2023-02-24 04:38 出处:网络
T开发者_如何学编程his is my code from the main frame window: public class DynamicalSystem { public staticvoid createAndShowGraphic() {

T开发者_如何学编程his is my code from the main frame window:

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);




 }
}

and this is from my 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!");
         }
   }
}

I want the image to open in my main window not in a new one. How I can do it?


Maybe I'm missing something, but it looks like at no point are you actually putting your image in the main frame, which from your question appears to be DynamicalSystem. Instead, it looks like you're creating a new window in ImageFrame and putting the image there instead. Try calling

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

from DynamicalSystem and putting the LabelDemo in that frame instead of ImageFrame.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号