Par*_*ker 2 java applet swing image jframe
嘿,所以我正在研究一个程序并进行调试,我正试图让程序截取显示器的一部分.我希望更新显示,但我似乎无法让它工作.我确定这是一个简单的问题,但我对Java Applets的经验非常小.
这是我遇到问题的部分:
...
Thread.sleep(5000);
try {gb = new GameBoard(frame.getBounds());}
catch(Exception e){System.out.println("Error.");} // Make "gameboard" Object
while (true)
{
Thread.sleep(1000);
gb.grabImage(); // use java.awt.Robot's createScreenCapture()
ImageIcon icon = new ImageIcon(gb.image()); // wrap the image
JLabel label = new JLabel(icon, JLabel.CENTER);
frame.getContentPane().add(label,BorderLayout.EAST); //display the image (works)
//JOptionPane.showMessageDialog(null, label, "icon", -1);
label.repaint(); //update the display??
frame.repaint();
frame.getContentPane().repaint();
}
Run Code Online (Sandbox Code Playgroud)
正如我所说,图像出现,如果我改变小程序大小,将创建新的图像,但我需要一个不断变化的图像.
提前致谢!
您JLabel
每次通过循环创建并添加新内容.因为您要更改组件树的结构,所以需要revalidate
在框架的内容窗格上调用.
更好的解决方案是在单个JLabel上更改图像.创建一个标签,添加它,然后在循环使用JLabel.setIcon
和repaint
.
归档时间: |
|
查看次数: |
7034 次 |
最近记录: |