1.键盘事件及监听处理
在Swing的事件处理中也可以对键盘的操作进行监听,直接使用KeyListener接口即可。
package com.beyole.util;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
class MyKeyHandle extends JFrame implements KeyListener {
private JTextArea text = new JTextArea();
public MyKeyHandle() {
super.setTitle("Crystal");
JScrollPane pane = new JScrollPane(text);// 加入滚动条
pane.setBounds(5, 5, 300, 200);
super.add(pane);// 像窗体加入组件
text.addKeyListener(this);// 加入key监听
super.setSize(310, 210);// 设置窗体
super.setVisible(true);// 显示窗体
super.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
System.exit(1);// 系统退出
}
});
}
public void keyPressed(KeyEvent e) {
text.append("键盘" + KeyEvent.getKeyText(e.getKeyCode()) + "键向下\n");
}
public void keyReleased(KeyEvent e) {
text.append("键盘" + KeyEvent.getKeyText(e.getKeyCode()) + "键松开\n");
}
public void keyTyped(KeyEvent e) {
text.append("输入的内容是" + e.getKeyChar() + "\n");
}
}
public class MyKeyEventDemo {
public static void main(String[] args) {
new MyKeyHandle();
}
}
package com.beyole.util;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
class MyKeyHandle extends JFrame implements KeyListener {
private JTextArea text = new JTextArea();
public MyKeyHandle() {
super.setTitle("Crystal");
JScrollPane pane = new JScrollPane(text);// 加入滚动条
pane.setBounds(5, 5, 300, 200);
super.add(pane);// 像窗体加入组件
text.addKeyListener(this);// 加入key监听
super.setSize(310, 210);// 设置窗体
super.setVisible(true);// 显示窗体
super.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
System.exit(1);// 系统退出
}
});
}
public void keyPressed(KeyEvent e) {
text.append("键盘" + KeyEvent.getKeyText(e.getKeyCode()) + "键向下\n");
}
public void keyReleased(KeyEvent e) {
text.append("键盘" + KeyEvent.getKeyText(e.getKeyCode()) + "键松开\n");
}
public void keyTyped(KeyEvent e) {
text.append("输入的内容是" + e.getKeyChar() + "\n");
}
}
public class MyKeyEventDemo {
public static void main(String[] args) {
new MyKeyHandle();
}
}
package com.beyole.util;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
class MyKeyHandle extends JFrame implements KeyListener {
private JTextArea text = new JTextArea();
public MyKeyHandle() {
super.setTitle("Crystal");
JScrollPane pane = new JScrollPane(text);// 加入滚动条
pane.setBounds(5, 5, 300, 200);
super.add(pane);// 像窗体加入组件
text.addKeyListener(this);// 加入key监听
super.setSize(310, 210);// 设置窗体
super.setVisible(true);// 显示窗体
super.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
System.exit(1);// 系统退出
}
});
}
public void keyPressed(KeyEvent e) {
text.append("键盘" + KeyEvent.getKeyText(e.getKeyCode()) + "键向下\n");
}
public void keyReleased(KeyEvent e) {
text.append("键盘" + KeyEvent.getKeyText(e.getKeyCode()) + "键松开\n");
}
public void keyTyped(KeyEvent e) {
text.append("输入的内容是" + e.getKeyChar() + "\n");
}
}
public class MyKeyEventDemo {
public static void main(String[] args) {
new MyKeyHandle();
}
}
package com.beyole.util;
package com.beyole.util;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
class MyKeyHandle extends JFrame implements KeyListener {
private JTextArea text = new JTextArea();
public MyKeyHandle() {
super.setTitle("Crystal");
JScrollPane pane = new JScrollPane(text);// 加入滚动条
pane.setBounds(5, 5, 300, 200);
super.add(pane);// 像窗体加入组件
text.addKeyListener(this);// 加入key监听
super.setSize(310, 210);// 设置窗体
super.setVisible(true);// 显示窗体
super.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
System.exit(1);// 系统退出
}
});
}
public void keyPressed(KeyEvent e) {
text.append("键盘" + KeyEvent.getKeyText(e.getKeyCode()) + "键向下\n");
}
public void keyReleased(KeyEvent e) {
text.append("键盘" + KeyEvent.getKeyText(e.getKeyCode()) + "键松开\n");
}
public void keyTyped(KeyEvent e) {
text.append("输入的内容是" + e.getKeyChar() + "\n");
}
}
public class MyKeyEventDemo {
public static void main(String[] args) {
new MyKeyHandle();
}
}
package com.beyole.util;
package com.beyole.util;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
class MyKeyHandle extends JFrame implements KeyListener {
private JTextArea text = new JTextArea();
public MyKeyHandle() {
super.setTitle("Crystal");
JScrollPane pane = new JScrollPane(text);// 加入滚动条
pane.setBounds(5, 5, 300, 200);
super.add(pane);// 像窗体加入组件
text.addKeyListener(this);// 加入key监听
super.setSize(310, 210);// 设置窗体
super.setVisible(true);// 显示窗体
super.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
System.exit(1);// 系统退出
}
});
}
public void keyPressed(KeyEvent e) {
text.append("键盘" + KeyEvent.getKeyText(e.getKeyCode()) + "键向下\n");
}
public void keyReleased(KeyEvent e) {
text.append("键盘" + KeyEvent.getKeyText(e.getKeyCode()) + "键松开\n");
}
public void keyTyped(KeyEvent e) {
text.append("输入的内容是" + e.getKeyChar() + "\n");
}
}
public class MyKeyEventDemo {
public static void main(String[] args) {
new MyKeyHandle();
}
}
2.鼠标事件及其监听
鼠标事件
//鼠标必须在Collider上,点击鼠标触发时间
private void OnMouseDown()
{
//鼠标按下
Debug.Log("鼠标按下");
}
private void OnMouseUp()
{
Debug.Log("鼠标抬起");
}
private void OnMouseDrag()
{
Debug.Log("鼠标拖拽");//就算鼠标离开物体也会一只调用 直到鼠标抬起
}
private void OnMouseEnter()
{
Debug.Log("鼠标移进");//只调用一次,进去的时候
}
private void OnMouseExit()
{
Debug.Log("鼠标移出");//也是只调一次,移出的时候
}
private void OnMouseOver()
{
Debug.Log("鼠标悬浮");//每帧都在调用
}
private void OnMouseUpAsButton()
{
Debug.Log("按下又抬起的时候调用");
}
//总结:当鼠标按下的时候,又抬起最先鼠标移进OnMouseEnter 先执OnMouseDown,再执行OnMouseUpAsButton,再执行OnMouseUp
3.单选钮JRadioButton
单选按钮就是在给定的多个选择项中选择一个,并且只能选择一个。在
Swing
中可以使用JRadioButton
完成一组单选按钮的操作,JRadioButton
的常用方法如下表。JRadioButton事件处理
在单选按钮操作中,可以使用ItemListener
接口进行事件的监听。此接口定义了如下表所示的方法。
此方法中存在ItemEvent
事件,此事件的常用方法及常量如下表所示。