Java 在swing主线程运行时侦听键事件
我有一个swing应用程序,它接受用户输入并进入全屏,显示输入图像的变化,我想在特定的按键上停止这个线程的执行(prig不应该终止),程序应该请求另一个用户输入。不知道如何接近?plz帮助代码如下:Java 在swing主线程运行时侦听键事件,java,multithreading,swing,jframe,jfilechooser,Java,Multithreading,Swing,Jframe,Jfilechooser,我有一个swing应用程序,它接受用户输入并进入全屏,显示输入图像的变化,我想在特定的按键上停止这个线程的执行(prig不应该终止),程序应该请求另一个用户输入。不知道如何接近?plz帮助代码如下: public class ImageProcessor extends JFrame implements ActionListener { /** * */ private static final long serialVersionUID = 2916361361443483318L;
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);
}
}
公共类ImageProcessor扩展JFrame实现ActionListener{
/**
*
*/
私有静态最终长serialVersionUID=2916361361361443483318L;
私有JFileChooser fc=null;
第1项、第2项;
私有缓冲区映像=空;
私有JFrame=null;
专用JPanel面板=空;
私有整数宽度=0;
私有整数高度=0;
私人边界布局卡;
专用容器内容窗格;
私有整数wcount=0;
私有整数hcount=0;
公共图像处理器(){
帧=新的JFrame(“图像处理器”);
contentPane=frame.getContentPane();
panel=新的JPanel();
卡片=新边框布局();
面板设置布局(卡片);
面板.立根背景(颜色.白色);
JMenuBar menuBar=新的JMenuBar();
JMenu菜单=新JMenu(“菜单”);
menu.setMnemonic(KeyEvent.VK_M);
菜单栏。添加(菜单);
item1=新JMenuItem(“浏览图像”);
item2=新项目(“退出”);
项目1.addActionListener(本);
项目2.addActionListener(本);
菜单。添加(第1项);
菜单。添加(第2项);
frame.setJMenuBar(菜单栏);
contentPane.add(面板);
setExtendedState(JFrame.MAXIMIZED_二者);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
新的图像处理器();
}
});
}
@凌驾
已执行的公共无效操作(操作事件e){
如果(如getSource()==item1){
如果(fc==null)
fc=新的JFileChooser();
int retVal=fc.showOpenDialog(null);
if(retVal==JFileChooser.APPROVE_选项){
File File=fc.getSelectedFile();
试一试{
image=ImageIO.read(文件);
宽度=image.getWidth();
高度=image.getHeight();
final GraphicsDevice gd=GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
frame.getJMenuBar().setVisible(false);
gd.setFullScreenWindow(框架);
计时器计时器=新计时器(0,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
panel.removeAll();
//System.out.println(wcount+“”+hcount);
如果(你看了多少?我的问题不是关于键侦听器。当你的主线程正在执行一些任务时,如何侦听键evens?从main method开始的线程问题是,事件调度线程,即主UI线程,负责处理所有的事件调度,所以你需要某种补偿onent侦听和处理关键事件,然后将这些事件传递给“其他”线程