[英]Refreshing my JFrame
I want to display an image on a application and when I want to open another one, I want that the new one overwrite the old. 我想在应用程序上显示图像,当我想打开另一个图像时,我想要新的一个覆盖旧的。
I've looking everywhere to find a solution like use invalidate(), repaint(), etc.. but still not working and I can't figured out why the windows doesn't refresh, can someone help me? 我到处寻找找到像使用invalidate(),repaint()等的解决方案..但仍然无法工作,我无法弄清楚为什么窗户不刷新,有人可以帮我吗?
Here the code : 这里的代码:
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);
}
}
Don't create new components. 不要创建新组件。 Just update the data of existing components.
只需更新现有组件的数据即可。 Maybe something like:
也许是这样的:
scrollPane.setViewportView( imagePanel );
Or even easier just use a JLabel to display your image. 或者更简单地使用JLabel来显示图像。 Then when the image changes you can use:
然后当图像发生变化时,您可以使用:
label.setIcon( new ImageIcon(...) );
Without a proper SSCCE its hard to guess what you are doing wrong. 没有适当的SSCCE,很难猜出你做错了什么。 For example I see:
例如,我看到:
tmp.getViewport().add(test);
...
test.memento_ = new Memento(test);
Without knowing what your code does it looks like you are trying to add the same component to two different components which is not allowed. 在不知道您的代码是什么的情况下,您似乎试图将相同的组件添加到两个不允许的组件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.