【发布时间】:2012-12-21 01:44:09
【问题描述】:
我是一名刚学习 Java 的 .net 程序员。过去两周我一直在研究这个问题...
这是我的代码,用于显示我的安全系统网络摄像头并更新在小程序中运行的 JFrame 中的图像:(我需要将此代码转换为在 JPanel 中工作!)
public class Streamer extends JApplet {
String PATH = "C:/Security/CamCap/";
Integer UPDATE_INTERVAL = 100;
String CAM1FILE = "current1.png";
String CAM2FILE = "current2.png";
String CAM3FILE = "current3.png";
String CAM4FILE = "current4.png";
String CAM5FILE = "current5.png";
String CAM6FILE = "current6.png";
String CAM7FILE = "current7.png";
String CAM8FILE = "current8.png";
String LOGOFILE = "logo.png";
private String path1 = PATH + CAM1FILE;
private String path2 = PATH + CAM2FILE;
private String path3 = PATH + CAM3FILE;
private String path4 = PATH + CAM4FILE;
private String path5 = PATH + CAM5FILE;
private String path6 = PATH + CAM6FILE;
private String path7 = PATH + CAM7FILE;
private String path8 = PATH + CAM8FILE;
private String pathLogo = PATH + LOGOFILE;
JFrame frame = new JFrame("@Home - Live Video");
boolean loaded = false;
JLabel label1, label2, label3, label4, label5, label6, label7, label8, labelLogo;
public void init()
{
frame.getContentPane().setBackground(Color.BLACK);
frame.setMinimumSize(new Dimension(1087, 777));
frame.setLayout(new GridLayout(3, 3, 2, 2));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1090,780);
frame.getContentPane().setSize(1087, 777);
frame.setLocationRelativeTo ( null );
frame.setVisible(true);
//INITIALIZE CAMERA 1
ImageIcon icon1 = new ImageIcon();
loaded = false;
while(!loaded)
{
try {
icon1 = new ImageIcon(ImageIO.read(new File(path1)));
loaded = true;
} catch (Exception ex) { loaded = false; }
}
label1 = new JLabel(icon1);
frame.add(label1);
//INITIALIZE CAMERA 2
ImageIcon icon2 = new ImageIcon();
loaded = false;
while(!loaded)
{
try {
icon2 = new ImageIcon(ImageIO.read(new File(path2)));
loaded = true;
} catch (Exception ex) { loaded = false; }
}
label2 = new JLabel(icon2);
frame.add(label2);
//INITIALIZE CAMERA 3
ImageIcon icon3 = new ImageIcon();
loaded = false;
while(!loaded)
{
try {
icon3 = new ImageIcon(ImageIO.read(new File(path3)));
loaded = true;
} catch (Exception ex) { loaded = false; }
}
label3 = new JLabel(icon3);
frame.add(label3);
//INITIALIZE CAMERA 4
ImageIcon icon4 = new ImageIcon();
loaded = false;
while(!loaded)
{
try {
icon4 = new ImageIcon(ImageIO.read(new File(path4)));
loaded = true;
} catch (Exception ex) { loaded = false; }
}
label4 = new JLabel(icon4);
frame.add(label4);
//INITIALIZE CAMERA 5
ImageIcon icon5 = new ImageIcon();
loaded = false;
while(!loaded)
{
try {
icon5 = new ImageIcon(ImageIO.read(new File(path5)));
loaded = true;
} catch (Exception ex) { loaded = false; }
}
label5 = new JLabel(icon5);
frame.add(label5);
//INITIALIZE CAMERA 6
ImageIcon icon6 = new ImageIcon();
loaded = false;
while(!loaded)
{
try {
icon6 = new ImageIcon(ImageIO.read(new File(path6)));
loaded = true;
} catch (Exception ex) { loaded = false; }
}
label6 = new JLabel(icon6);
frame.add(label6);
//INITIALIZE CAMERA 7
ImageIcon icon7 = new ImageIcon();
loaded = false;
while(!loaded)
{
try {
icon7 = new ImageIcon(ImageIO.read(new File(path7)));
loaded = true;
} catch (Exception ex) { loaded = false; }
}
label7 = new JLabel(icon7);
frame.add(label7);
//INITIALIZE CAMERA 8
ImageIcon icon8 = new ImageIcon();
loaded = false;
while(!loaded)
{
try {
icon8 = new ImageIcon(ImageIO.read(new File(path8)));
loaded = true;
} catch (Exception ex) { loaded = false; }
}
label8 = new JLabel(icon8);
frame.add(label8);
//INITIALIZE LOGO
ImageIcon iconLogo = new ImageIcon();
try {
iconLogo = new ImageIcon(ImageIO.read(new File(pathLogo)));
loaded = true;
} catch (Exception ex) { loaded = false; }
labelLogo = new JLabel(iconLogo);
frame.add(labelLogo);
frame.setVisible(true);
run();
}
public void run()
{
while(true)
{
try {
ImageIcon icon1 = new ImageIcon(ImageIO.read(new File(path1)));
label1.setIcon(icon1);
label1.repaint();
} catch (Exception ex) {}
try {
ImageIcon icon2 = new ImageIcon(ImageIO.read(new File(path2)));
label2.setIcon(icon2);
label2.repaint();
} catch (Exception ex) {}
try {
ImageIcon icon3 = new ImageIcon(ImageIO.read(new File(path3)));
label3.setIcon(icon3);
label3.repaint();
} catch (Exception ex) {}
try {
ImageIcon icon4 = new ImageIcon(ImageIO.read(new File(path4)));
label4.setIcon(icon4);
label4.repaint();
} catch (Exception ex) {}
try {
ImageIcon icon5 = new ImageIcon(ImageIO.read(new File(path5)));
label5.setIcon(icon5);
label5.repaint();
} catch (Exception ex) {}
try {
ImageIcon icon6 = new ImageIcon(ImageIO.read(new File(path6)));
label6.setIcon(icon6);
label6.repaint();
} catch (Exception ex) {}
try {
ImageIcon icon7 = new ImageIcon(ImageIO.read(new File(path7)));
label7.setIcon(icon7);
label7.repaint();
} catch (Exception ex) {}
try {
ImageIcon icon8 = new ImageIcon(ImageIO.read(new File(path8)));
label8.setIcon(icon8);
label8.repaint();
} catch (Exception ex) {}
try {
Thread.sleep(UPDATE_INTERVAL);
} catch (Exception ex) {}
}
}
}
我已经尝试了所有方法,但找不到将这段代码转换为在 JPanel 中工作的方法。我需要在我的小程序中使用 JPanel,这样我的相机就不会出现在一个新的、单独的窗口 (JFrame) 中。
有谁知道如何转换(只是一小部分)这段代码以在 JPanel 中绘制和刷新我的图像?
谢谢(提前)!
【问题讨论】:
-
它怎么不工作——你从来没有告诉我们?你有例外吗?不是在编译吗?从小处着手然后成长不是更好吗?首先在 JPanel 上创建一个小型 GUI,然后按照您的需要逐步构建您的 GUI?
-
对不起,应该提一下:这段代码完美运行!但是,由于我使用的是 JFrame,所以当我在 Web 浏览器中启动这个小程序时,它会在一个弹出窗口中启动(这不是我想要的)。我希望它在加载时显示在网页上,而不是在弹出(JFrame)窗口中。我到处看,它建议使用 JPanel 而不是 JFrame。但是:当我使用 JPanel 时,我的图像无法正确显示...
-
所以不要使用 JFrame,而是将所有 GUI 放到一个 JPanel 上,然后再添加到 JApplet 的 contentPane 中。我仍然不清楚你到底在哪里卡住了,或者你在哪里试图解决这个问题,以及它是如何不起作用的。
-
我尝试重新编写代码以使用添加到 contentPane 的 JPanel,但是我的图像不会在 run() 方法中更新。我知道这个问题是由于我缺乏 Java 经验...我会继续尝试并发布返工并尝试使用 JPanel in contentPane 方法清楚地显示问题。
-
编辑:一个问题是您的程序似乎没有遵守 Swing 的线程规则。你读过这方面的教程吗?您需要考虑在此处使用 Swing Timer 或 SwingWorker。
标签: java swing security applet jframe