JFrame表现得很奇怪。零星地展示JLabel

时间:2013-05-04 10:29:03

标签: java swing jframe jlabel

很难解释所以我把我的手机和视频扯掉了我遇到的问题:

http://www.youtube.com/watch?v=KdOeNdE8W2Q

简单地说,图像(你可以看到的底部边框,实现为JLabel)应该始终保持在那里,而是显示1秒钟,消失然后偶尔闪烁。

import java.awt.Button;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.imgscalr.Scalr;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamResolution;

public class WebcamPanelExample extends JFrame {

    static Webcam webcam;
    static JFrame window;


    public static void main(String[] args) throws IOException {

        window = new JFrame("Test webcam panel");

        Dimension[] nonStandardResolutions = new Dimension[] {
                WebcamResolution.HD720.getSize(),
                };

        webcam = Webcam.getDefault();
        webcam.setCustomViewSizes(nonStandardResolutions);
        webcam.setViewSize(WebcamResolution.HD720.getSize());

        WebcamPanel panel = new WebcamPanel(webcam);
        panel.setFPSLimited(false); 
        panel.setFillArea(true); 
        window.add(panel);

        ////////////

        BufferedImage image2 = ImageIO.read(new File("F:/DATA/images/image_area.png"));
        JLabel guiBorder = new JLabel(new ImageIcon( image2 ));
        guiBorder.setSize(new Dimension(1280,720));
        guiBorder.setLocation(0, 0);
        window.add( guiBorder );

        ///////////

        Button button = new Button("Do Something");
        button.setSize(new Dimension(200,32));
        button.setLocation(540, 670);
        window.add(button);

        ///////////

        window.setComponentZOrder(button, 0);
        window.setComponentZOrder(guiBorder, 1);
        window.setComponentZOrder(panel, 2);

        ///////////
        window.pack();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLayout(null);
        window.validate();
        window.setSize(1280,720);
        window.setResizable(false);
        window.setVisible(true);

    }
}

此外,如果重要:

关于问题的潜在原因的任何想法?

编辑(安德鲁汤普森)

 import javax.swing.JOptionPane;
 import javax.swing.JFileChooser;
 import javax.sound.sampled.*;
 import java.net.URL;
 import java.io.ByteArrayOutputStream;
 import java.io.ByteArrayInputStream;
 import java.util.Date;
 import java.io.File;

 class AcceleratePlayback {

     public static void main(String[] args) throws Exception {
         int playBackSpeed = 3;
     File soundFile;
         if (args.length>0) {
             try {
                 playBackSpeed = Integer.parseInt(args[0]);
             } catch (Exception e) {
                 e.printStackTrace();
                 System.exit(1);
             }
         }
         System.out.println("Playback Rate: " + playBackSpeed);

         JFileChooser chooser = new JFileChooser();
         chooser.showOpenDialog(null);
         soundFile = chooser.getSelectedFile();

         System.out.println("FILE: " + soundFile);
         AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
         AudioFormat af = ais.getFormat();

         int frameSize = af.getFrameSize();

         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         byte[] b = new byte[2^16];
         int read = 1;
         while( read>-1 ) {
             read = ais.read(b);
             if (read>0) {
                 baos.write(b, 0, read);
             }
         }
         System.out.println("End entire: \t" + new Date());

        //This is the important bit

         byte[] b1 = baos.toByteArray();
         byte[] b2 = new byte[b1.length/playBackSpeed];
         for (int ii=0; ii<b2.length/frameSize; ii++) {
             for (int jj=0; jj<frameSize; jj++) {
                     int b3=0;  
             for (int kk = 0; kk < playBackSpeed; kk++){
              b3 = b3+(int)b1[(ii*frameSize*playBackSpeed)+jj+kk];  
             }
             b3 = b3/playBackSpeed;
             b2[(ii*frameSize)+jj] = (byte)b3;
             }
         }
        //ends here

         System.out.println("End sub-sample: \t" + new Date());

         ByteArrayInputStream bais = new ByteArrayInputStream(b2);
         AudioInputStream aisAccelerated = new AudioInputStream(bais, af, b2.length);
         Clip clip = AudioSystem.getClip();
         clip.open(aisAccelerated);
         clip.loop(2*playBackSpeed);
         clip.start();

         JOptionPane.showMessageDialog(null, "Exit?");
     }
}

1 个答案:

答案 0 :(得分:0)

交换使用JLayeredPane。

工作流程基本上是:

  1. 创建JFrame
  2. 创建JLayeredPane
  3. 将JLayeredPane添加到JFrame
  4. 创建JPanels(根据需要,我有三个,一个用于网络摄像头背景,两个前景图层用于按钮等)
  5. 将您的JPanel添加到JLayeredPane。您可以在添加时设置z-index / order。
  6. 完整示例: http://www.roseindia.net/java/example/java/swing/jlayered-overlap-panel.shtml