我编写了一个小程序,其中有一个 JLabel(包含 ImageIcon)和一个 JPanel 内的自定义 ImagePanel。由于某种原因,JLabel 在第一次运行/加载时永远不会出现在 mac 操作系统上的 safari 和 firefox 中,但在其他操作系统(windows、linux)上它看起来很好。现在,在同一个小程序中,有一个按钮可以将图像翻转到另一图像。在 mac os 上的 safari/firefox 上,单击按钮时,会显示第二张图像,再次单击时,会显示第一张图像!知道什么可能导致这个问题吗?即使在 Windows 的 safari 上,小程序也能正常工作......即第一个图像加载并出现。
用户界面代码
public void createUI(){
mainpanel = new JPanel();
mainpanel.setMaximumSize(new Dimension(154, 212));
mainpanel.setMinimumSize(new Dimension(154, 212));
mainpanel.setName("mainPanel");
mainpanel.setLayout(new BorderLayout());
lcdpanel = new ImagePanel(bgLcdImage);
lcdpanel.setBounds(22, 22, 110, 28);
bgImage = Toolkit.getDefaultToolkit().createImage(bytes);//BufferedImage
label = new JLabel(new ImageIcon(bgImage));
mainpanel.add(lcdpanel);
mainpanel.add(label);
mainpanel.invalidate();
getContentPane().add(mainpanel);
repaint();
}
按钮点击代码
private void flipImage()
{
label.setIcon(new ImageIcon(backImg));
label.repaint();
lcdpanel.setVisible(false);
lcdpanel.repaint();
mainpanel.repaint();
this.repaint();
}
如有任何帮助,我们将不胜感激。
谢谢
最佳答案
I even made the jlabel as a imagepanel, set the layout of jpanel to null
将布局设置为 null 是最糟糕的事情。这通常会导致比解决问题更多的问题。
The issue is not where i want the jlabel to be shown but WHY its not showing on Mac OS X firefox/safari browsers when it shows on windows/linux firefox/safari browsers.
我们如何知道何时只发布了几行代码?当您遇到问题时,发布正确的 SSCCE
。
On safari/firefox on mac os, when the button is clicked, the second image shows, the when clicked again, the first image now appears!!
在可见 GUI 上添加/删除组件时的一般格式是:
panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes needed
当您更改组件的属性时,您永远不需要调用重绘。 Swing 足够智能,可以为您重新绘制。
label.setIcon(new ImageIcon(backImg));
//label.repaint();
关于java - Jpanel 未出现在 Safari 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17106913/