这个代码为什么不可以实现按键盘3然后退出啊
public class PosFrame extends JFrame
{
private JButton jButton1;
private JButton jButton2;
private JButton jButton3=new JButton("退出 (ESC)");
Container con=this.getContentPane(); public PosFrame()
{
this.jButton3.setBackground(new Color(203,231,255));
this.jButton3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}); this.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e)
{
if(e.getKeyChar()=='3') ////按键3
System.exit(0);
}
} );
con.add(jButton3);
this.setVisible(true);
this.pack();
}
public static void main(String args[])
{
new PosFrame();
}
}
public class PosFrame extends JFrame
{
private JButton jButton1;
private JButton jButton2;
private JButton jButton3=new JButton("退出 (ESC)");
Container con=this.getContentPane(); public PosFrame()
{
this.jButton3.setBackground(new Color(203,231,255));
this.jButton3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}); this.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e)
{
if(e.getKeyChar()=='3') ////按键3
System.exit(0);
}
} );
con.add(jButton3);
this.setVisible(true);
this.pack();
}
public static void main(String args[])
{
new PosFrame();
}
}
{
public void keyTyped(KeyEvent e)
{
if(e.getKeyChar()=='3') ////按键3
System.exit(0);
}
} );=========================>this.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_3)
{
System.exit(0);
}
}
} );
public void keyTyped(KeyEvent e) {
char ch=e.getKeyChar( ); //注意这一行
if (ch == '3') {
System.exit(0);
}
}
});
{
public void keyTyped(KeyEvent e)
{
if(e.getKeyChar()==27) ////按键ESC ,这里非要写ASCII码才可以的
System.exit(0);
}
} );
改为KeyEvent里的常数不就得了。this.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char ch=e.getKeyChar(); //注意这一行
if (ch == e.VK_ESCAPE) {
System.exit(0);
}
}
});
import java.awt.event.*;import javax.swing.*;public class PosFrame extends JFrame
{
private JButton jButton1; private JButton jButton2; private JButton jButton3 = new JButton("退出 (ESC)"); Container con = this.getContentPane(); public PosFrame()
{
this.jButton3.setBackground(new Color(203, 231, 255)); AbstractAction exitAction = new AbstractAction() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
};
this.jButton3.addActionListener(exitAction);
this.jButton3.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "ExitApp");
this.jButton3.getActionMap().put("ExitApp", exitAction); con.add(jButton3);
this.setVisible(true);
this.pack();
} public static void main(String args[])
{
new PosFrame();
}
}
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
};
this.jButton3.addActionListener(exitAction);
this.jButton3.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "ExitApp");
this.jButton3.getActionMap().put("ExitApp", exitAction);
看不懂啊
可以给我解释一下吗?
强~!
不过也有点咕哝玄虚了
this.addKeyListener
而不是this.jButton3.addKeyListener
按3呐