在swing主线程运行时监听键事件

时间:2014-01-24 05:36:13

标签: java multithreading swing jframe jfilechooser

我有一个摆动应用程序,它接受用户输入并进入全屏并显示输入图像的变化,我想在特定按键上停止此线程执行(prig不应该终止)并且程序应该要求另一个用户输入。不知道怎么办? plz帮助这里的代码:::

public class ImageProcessor extends JFrame implements ActionListener {

/**
 * 
 */
private static final long serialVersionUID = 2916361361443483318L;
private JFileChooser fc = null;
private JMenuItem item1, item2;
private BufferedImage image = null;
private JFrame frame = null;
private JPanel panel = null;
private int width = 0;
private int height = 0;
private BorderLayout card;
private Container contentPane;
private int wcount = 0;
private int hcount = 0;

public ImageProcessor() {


    frame = new JFrame("Image Processor");
    contentPane = frame.getContentPane();

    panel = new JPanel();
    card = new BorderLayout();
    panel.setLayout(card);
    panel.setBackground(Color.white);

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    menu.setMnemonic(KeyEvent.VK_M);
    menuBar.add(menu);

    item1 = new JMenuItem("Browse an image");
    item2 = new JMenuItem("Exit");

    item1.addActionListener(this);
    item2.addActionListener(this);

    menu.add(item1);
    menu.add(item2);

    frame.setJMenuBar(menuBar);
    contentPane.add(panel);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new ImageProcessor();
        }
    });

}

@Override
public void actionPerformed(ActionEvent e) {

    if (e.getSource() == item1) {
        if (fc == null)
            fc = new JFileChooser();
        int retVal = fc.showOpenDialog(null);
        if (retVal == JFileChooser.APPROVE_OPTION){
            File file = fc.getSelectedFile();
            try {
                image = ImageIO.read(file);
                width = image.getWidth();
                height = image.getHeight();

                final GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
                frame.getJMenuBar().setVisible(false);
                gd.setFullScreenWindow(frame);


                Timer timer = new Timer(0, new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        panel.removeAll();
                        //System.out.println(wcount + " " + hcount);
                        if(wcount <= width-1 && hcount <= height-1){
                            Color c = new Color(image.getRGB(wcount, hcount));
                            panel.setBackground(c);
                            panel.revalidate();
                            panel.repaint();
                        }
                        ((Timer)e.getSource()).setDelay(333);
                        if(wcount <= width-1){
                            if(hcount < height-1 )
                                hcount++;
                            else{
                                hcount = 0;
                                wcount++;
                            }
                        }else{
                            //((Timer)e.getSource()).stop();
                            //gd.setFullScreenWindow(null);
                            //frame.getJMenuBar().setVisible(true);
                            //JOptionPane.showMessageDialog(null, "Image Read Complete!");
                            wcount = 0;
                            hcount = 0;
                        }
                    }
                });

                timer.start();

            } catch (IOException e1) {
                System.out.println("IO::" + e1.getMessage());
            } catch (Exception e1) {
                System.out.println("Exception::" + e1.getMessage());
            }
        }
    }
    if (e.getSource() == item2) {
        System.exit(0);
    }
}

}

plz建议..

0 个答案:

没有答案