Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 刷新我的JFrame_Java_Image_Swing_Jframe - Fatal编程技术网

Java 刷新我的JFrame

Java 刷新我的JFrame,java,image,swing,jframe,Java,Image,Swing,Jframe,我想在应用程序上显示一个图像,当我想打开另一个图像时,我希望新图像覆盖旧图像 我到处寻找解决方案,比如使用invalidate()、repaint()等。。但是仍然不能工作,我也不明白为什么windows不能刷新,有人能帮我吗 代码如下: public void actionPerformed(ActionEvent e) { System.out.println(e.getActionCommand()); if (e.getActionCommand().contains(

我想在应用程序上显示一个图像,当我想打开另一个图像时,我希望新图像覆盖旧图像

我到处寻找解决方案,比如使用invalidate()、repaint()等。。但是仍然不能工作,我也不明白为什么windows不能刷新,有人能帮我吗

代码如下:

public void actionPerformed(ActionEvent e) 
{
    System.out.println(e.getActionCommand());
    if (e.getActionCommand().contains("Open"))
    {
        filename_ = new String();
        filename_ = JOptionPane.showInputDialog("File to open ?");
        ImagePanel test = new ImagePanel(new File(filename_));

        test.setPreferredSize(new Dimension(test.getWidth(), test.getHeight()));
        test.setMinimumSize(new Dimension(test.getWidth(), test.getHeight()));
        test.repaint();
        JScrollPane tmp = new JScrollPane();
        tmp.getViewport().add(test);

        tmp.getViewport().repaint();
        mainPanel_.add(tmp, BorderLayout.NORTH);
        mainPanel_.repaint();
        curim_ = test;
        test.memento_ = new Memento(test);
        test.caretaker_.add(test.memento_);
        curim_ = test;


        curmodindex_ = curim_.caretaker_.getIndex();
        this.setContentPane(mainPanel_);
        System.out.println(curmodindex_);
        if (curmodindex_ != 0)
        {
            button1.setEnabled(true);
            button2.setEnabled(true);
        }
}

不要创建新组件。只需更新现有组件的数据。可能是这样的:

scrollPane.setViewportView( imagePanel );
甚至更简单,只需使用JLabel显示图像即可。然后,当图像更改时,您可以使用:

label.setIcon( new ImageIcon(...) );
没有合适的SSCCE,很难猜测你做错了什么。例如,我看到:

tmp.getViewport().add(test);
...
test.memento_ = new Memento(test);

在不知道代码的情况下,您似乎试图将相同的组件添加到两个不同的组件中,这是不允许的。

请显示您的图形实现。您是指我用于图像的Jpanel?从这里开始。尝试并调整它以适应您的需要。如果你做不到,请发布你的最佳尝试。是一个完整的例子。事实上,我有一个ImagePanel类,它扩展了JPanel,我的图像在这个JPanel中,然后我把我的JPanel放在一个滚动面板中以获得滚动条,然后我把我的ScollPanel放在JFrame中的一个主面板中。我明白你在做什么。无需为图像创建自定义面板。您可以使用JLabel。