在正确的框架中加载图像时出现问题

发布于 2024-11-01 02:19:39 字数 1908 浏览 3 评论 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!");
         }
   }
}

我希望图像在我的主窗口中打开,而不是在新窗口中打开。我怎样才能做到呢?

This 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖个坑埋了你 2024-11-08 02:19:39

也许我遗漏了一些东西,但看起来你实际上根本没有将图像放入主框架中,从你的问题来看,它似乎是 DynamicalSystem。相反,看起来您正在 ImageFrame 中创建一个新窗口并将图像放在那里。尝试从 DynamicalSystem 调用

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

并将 LabelDemo 放入该帧而不是 ImageFrame 中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文