AWT:组件最小化后或在顶部移动另一个窗口后组件消失

时间:2014-07-16 22:26:30

标签: java jframe awt jcomponent webcam-capture

我正在使用网络摄像头捕获库和AWT开发一个简单的界面,用于从网络摄像头拍摄照片。在最小化窗口之后或在其上移动另一个窗口之后,我的JFrame中的按钮和组合框消失。将指针移到框架上会恢复组件的可见性。我不熟悉Java UI,我无法弄清楚我的代码有什么问题。

    @SuppressWarnings("serial")
public class ImageCaptureManager extends JFrame {

    private class SkipCapture extends AbstractAction {

        public SkipCapture() {
            super(“Skip”);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            /*SOME CODE HERE*/

        }
    }

    private class SnapMeAction extends AbstractAction {

        public SnapMeAction() {
            super(“Snap”);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            /*SOME CODE HERE*/

        }
    }

    private class captureCompleted extends AbstractAction {

        public captureCompleted() {
            super(“Completed”);
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            /*SOME CODE HERE*/
        }
    }

    private class saveImage extends AbstractAction {

        public saveImage() {
            super(“Save”);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            /*SOME CODE HERE*/
        }
    }

    private class deleteImage extends AbstractAction {

        public deleteImage() {
            super(“Delete”);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            /*SOME CODE HERE*/
        }
    }


    private class StartAction extends AbstractAction implements Runnable {

        public StartAction() {
            super(“Start”);
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            btStart.setEnabled(false);
            btSnapMe.setEnabled(true);

            executor.execute(this);
        }

        @Override
        public void run() {
            panel.start();
        }
    }

    private Executor executor = Executors.newSingleThreadExecutor();

    private Dimension captureSize = new Dimension(640, 480);

    private Dimension displaySize = new Dimension(640, 480);


    private Webcam webcam = Webcam.getDefault();

    private WebcamPanel panel;

    private JButton btSnapMe = new JButton(new SnapMeAction());
    private JButton btStart = new JButton(new StartAction());

    private JButton btComplete = new JButton(new captureCompleted());
    private JButton btSave = new JButton(new saveImage());
    private JButton btDelete = new JButton(new deleteImage());
    private JButton btSkip = new JButton(new SkipCapture());

    private JComboBox comboBox = new JComboBox();


    public ImageCaptureManager() {
        super(“Frame”);

        this.addWindowListener( new WindowAdapter()
        {

            @Override
            public void windowDeiconified(WindowEvent arg0) {

            }
                public void windowClosing(WindowEvent e)
                {

                }
        });


        List<Webcam> webcams =  Webcam.getWebcams();
        for (Webcam webcam : webcams) {
            System.out.println(webcam.getName());
            if (webcam.getName().startsWith("USB2.0 Camera 1")) {
                this.webcam = webcam;
                break;
            }
        }


        panel = new WebcamPanel(webcam, displaySize, false);
        webcam.setViewSize(captureSize);

        panel.setFPSDisplayed(true);
        panel.setFillArea(true);

        btSnapMe.setEnabled(false);
        btSave.setEnabled(false);
        btDelete.setEnabled(false);


        setLayout(new FlowLayout());

        Panel buttonPanel = new Panel();
        buttonPanel.setLayout(new GridLayout(10, 1));
        buttonPanel.add(Box.createHorizontalStrut(20));
        buttonPanel.add(btSnapMe);
        buttonPanel.add(Box.createHorizontalStrut(20));
        buttonPanel.add(btSave);
        buttonPanel.add(Box.createHorizontalStrut(20));
        buttonPanel.add(btDelete);
        buttonPanel.add(Box.createHorizontalStrut(20));
        buttonPanel.add(btComplete);
        buttonPanel.add(Box.createHorizontalStrut(20));
        buttonPanel.add(btSkip);

        JLabel label1 = new JLabel("Test");
        label1.setText(“Bla bla bla”);
        JLabel label2 = new JLabel("Test");
        label2.setText(" ");

        Panel captionAndWebcamPanel = new Panel();
        captionAndWebcamPanel.add(label1);
        captionAndWebcamPanel.add(label2);
        captionAndWebcamPanel.add(panel);
        captionAndWebcamPanel.add(label2);
        captionAndWebcamPanel.add(comboBox);
        captionAndWebcamPanel.setLayout(new BoxLayout(captionAndWebcamPanel, BoxLayout.Y_AXIS));


        add(captionAndWebcamPanel);

        add(buttonPanel);
        pack();
        setVisible(true);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        btStart.doClick();

        setSize(900,600);

    }

}

1 个答案:

答案 0 :(得分:1)

您正在混合AWT和Swing组件。

“从历史上看,在Java语言中,将重量级和轻量级组件混合在同一容器中一直存在问题。” http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html

我建议您尝试使用JPanels代替Panels for captionAndWebcamPanel和buttonPanel,我还会在添加组件之前将布局设置为captionAndWebcamPanel。