Java 在正确的帧中加载图像时出现问题

Java 在正确的帧中加载图像时出现问题,java,image,filechooser,Java,Image,Filechooser,这是我在主框架窗口中的代码: 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.E

这是我在主框架窗口中的代码:

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




 }
}
这是我的照片:

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!");
         }
   }
}

我希望图像在我的主窗口中打开,而不是在新窗口中打开。我该怎么做呢?

也许我遗漏了什么,但看起来你根本没有把你的图像放在主框架中,从你的问题来看,主框架似乎是动态系统。相反,看起来您正在ImageFrame中创建一个新窗口,并将图像放在那里。试着打电话

LabelDemo面板=新建LabelDemo(chooser.getSelectedFile().getPath())


从DynamicSystem将LabelDemo放入该帧而不是ImageFrame。

您的Shift键似乎已断开。至少在你输入标题的时候。你是什么意思?我怎样才能修复it@user当前位置我只是开玩笑说你用大写字母写了标题。这被广泛认为是网络版的叫喊,这是不礼貌的。哦,soz,我会修复它的