程序功能:
演示各种Swing GUI组件(JTextArea, JCheckBox, JButton, JComboBox)的各种事件(键盘、鼠标)的捕捉及事件信息显示。
运行示例:
代码:
1. 测试类
import javax.swing.JFrame;
public class EventsDisplayFrameTest {
public static void main(String[] args)
{
EventsDisplayFrame testFrame = new EventsDisplayFrame();
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testFrame.setSize(900,568);
testFrame.setVisible(true);
}
}
2. 实体类
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.util.Date;
//Java How to Program, Exercise 12.15
//by pandenghuang@163.com
/**
* Java Swing事件演示程序
* 12.15 (Displaying Events) It’s often useful to display the events that occur
* during the execution of an application. This can help you understand when the
* events occur and how they’re generated. Write an application that enables the
* user to generate and process every event discussed in this chapter. The
* application should provide methods from the ActionListener, ItemListener,
* ListSelectionListener, MouseListener, MouseMotionListener and KeyListener
* interfaces to display messages when the events occur. Use method toString to
* convert the event objects received in each event handler into Strings that
* can be displayed. Method toString creates a String containing all the
* information in the event object.
*
* @author Pandenghuang@163.com
* @Date Jan 15, 2019, 12:51:14 AM
*
*/
public class EventsDisplayFrame extends JFrame {
//声明基本控件
private JLabel demoJLabel;
private JButton demoJButton;
private JButton clearJButton;
private JRadioButton demoJRadioButton1;
private JRadioButton demoJRadioButton2;
private ButtonGroup demoButtonGroup;
private JCheckBox demoJCheckBox1;
private JCheckBox demoJCheckBox2;
private JComboBox<String> demoJComboBox;
private JTextArea mouseJTextArea;
private JPasswordField demoJPasswordField;
private JTextArea demoJTextArea;
private JTextArea eventsInfoJTextArea;
private String[] comboBoxValues = {"Java How to Program", "Java 编程思想", "Java核心技术"};
private JLabel eventsInfoJLabel;
private ArrayList<String> eventsInfo;
private ArrayList<Date> startTimes;
private ArrayList<Date> endTimes;
private String allEventsInfo;
Date startTime,endTime;
SimpleDateFormat simpleDateFormat;
private int eventCount;
private JTextArea statusBar;
private JLabel keyJLabel;
private JLabel statusBarJLabel;
private JLabel radioButtonJLabel;
//声明容器控件
private JPanel topJPanel;
private JPanel checkBoxJPanel; //放置checkBox
private JPanel radioButtonJPanel; //放置radioButton
private JPanel eventsInfoJPanel;
private JPanel keyJPanel;
private JPanel statusBarJPanel;
public EventsDisplayFrame() {
super("Java Swing事件演示程序(Capture and Display Events)");
// LayoutManager frameLayoutManager = new GridLayout(2,1,10,20);
// setLayout(frameLayoutManager);
//初始化所有控件及变量
//基本控件
demoJLabel = new JLabel("GUI组件");
demoJButton = new JButton("演示按钮");
clearJButton = new JButton("清除内容");
demoJRadioButton1 = new JRadioButton("高级");
demoJRadioButton2 = new JRadioButton("中级");
demoButtonGroup = new ButtonGroup();
demoJCheckBox1 = new JCheckBox("Java编程");
demoJCheckBox2 = new JCheckBox("PL/SQL编程");
demoJComboBox = new JComboBox<String>(comboBoxValues);
mouseJTextArea = new JTextArea("鼠标事件捕捉区",5,5);
mouseJTextArea.setEnabled(false);
demoJPasswordField = new JPasswordField("演示密码框");
demoJTextArea = new JTextArea(1,5);
eventsInfoJTextArea = new JTextArea(18,70);
eventsInfoJTextArea.setLineWrap(true);
eventsInfoJPanel = new JPanel();
eventsInfoJLabel = new JLabel("事件信息:");
eventsInfoJLabel.setSize(5, 2);
statusBar= new JTextArea(4,50);
keyJLabel = new JLabel("键盘事件捕捉区");
statusBarJLabel = new JLabel("键盘事件按键信息显示区:");
radioButtonJLabel = new JLabel("编程能力:");
//容器控件
topJPanel = new JPanel();
checkBoxJPanel= new JPanel();
radioButtonJPanel = new JPanel();
keyJPanel = new JPanel();
statusBarJPanel = new JPanel();
eventsInfo =new ArrayList<>();
allEventsInfo ="";
startTimes = new ArrayList<>();
endTimes = new ArrayList<>();
simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss:SSS");
startTime = new Date(System.currentTimeMillis());
startTimes.add(startTime);
endTime = new Date(System.currentTimeMillis());
endTimes.add(startTime);
//添加所有控件
// topJPanel.setLayout(new GridLayout(2,5,30,20));
topJPanel.setLayout(new FlowLayout(FlowLayout.LEFT,15,5));
topJPanel.add(demoJLabel);
topJPanel.add(mouseJTextArea);
keyJPanel.setLayout(new GridLayout(2,1,2,2));
keyJPanel.add(keyJLabel);
keyJPanel.add(new JScrollPane(demoJTextArea));
topJPanel.add(keyJPanel);
demoButtonGroup.add(demoJRadioButton1);
demoButtonGroup.add(demoJRadioButton2);
checkBoxJPanel.setLayout(new GridLayout(2,1,1,10));
checkBoxJPanel.add(demoJCheckBox1);
demoJCheckBox1.addMouseListener(new MouseHandler());
checkBoxJPanel.add(demoJCheckBox2);
topJPanel.add(checkBoxJPanel);
radioButtonJPanel.setLayout(new GridLayout(3,1,1,10));
radioButtonJPanel.add(radioButtonJLabel);
radioButtonJPanel.add(demoJRadioButton1);
demoJRadioButton1.addMouseListener(new MouseClickHandler());
radioButtonJPanel.add(demoJRadioButton2);
topJPanel.add(radioButtonJPanel);
checkBoxJPanel.setVisible(true);
topJPanel.add(demoJComboBox);
topJPanel.add(demoJPasswordField);
topJPanel.add(demoJButton);
topJPanel.add(clearJButton);
eventsInfoJPanel.setLayout(new FlowLayout(FlowLayout.LEFT,3,1));
eventsInfoJPanel.add(eventsInfoJLabel);
eventsInfoJPanel.add(new JScrollPane(eventsInfoJTextArea));
statusBarJPanel.setLayout(new GridLayout(2,1,3,2));
statusBarJPanel.add(statusBarJLabel);
statusBarJPanel.add(new JScrollPane(statusBar));
//添加事件监听
demoJButton.addActionListener(new ButtonHandler());
mouseJTextArea.addMouseListener(new MouseHandler());
clearJButton.addActionListener(new ClearButtonHandler());
demoJCheckBox1.addItemListener(new CheckBoxHandler());
demoJCheckBox2.addItemListener(new CheckBoxHandler());
demoJComboBox.addItemListener(new CheckBoxHandler());
demoJTextArea.addKeyListener(new KeyHandler());
//setLayout(new GridLayout(3,1,1,10));
add(topJPanel,BorderLayout.NORTH);
add(eventsInfoJPanel,BorderLayout.CENTER);
add(statusBarJPanel,BorderLayout.SOUTH);
setVisible(true);
}
// inner class for key event handling
private class KeyHandler implements KeyListener
{
private String line1 = ""; // first line of textarea
private String line2 = ""; // second line of textarea
private String line3 = ""; // third line of textarea
// handle press key event
@Override
public void keyPressed(KeyEvent event)
{
displayKeyEvents(event);
line1 = String.format("Key typed(按键已按下,但未松开,正在重复键入该按键): %s", event.getKeyChar());
setLines2and3(event); // set output lines two and three
}
// handle type key event
@Override
public void keyTyped(KeyEvent event)
{
displayKeyEvents(event);
line1 = String.format("Key released(按键已松开): %s",
KeyEvent.getKeyText(event.getKeyCode())); // show released key
setLines2and3(event); // set output lines two and three
}
// handle release key event
@Override
public void keyReleased(KeyEvent event)
{
displayKeyEvents(event);
line1 = String.format("Key pressed(按键已按下且未被松开): %s",
KeyEvent.getKeyText(event.getKeyCode())); // show pressed key
setLines2and3(event); // set output lines two and three
}
private void setLines2and3(KeyEvent event)
{
line2 = String.format("This key is %san action key(该按键%s操作键:箭头、Home、End、翻页键、功能键等)",
(event.isActionKey() ? "" : "not "),(event.isActionKey() ? "是" : "不是"));
String temp = KeyEvent.getKeyModifiersText(event.getModifiers());
line3 = String.format("Modifier keys pressed(已按下的辅助键(转换键SHIFT、控制键CTRL、替换键ALT等)) : %s",
(temp.equals("") ? "none(无)" : temp)); // output modifiers
statusBar.setText(String.format("%s\n%s\n%s\n",
line1, line2, line3)); // output three lines of text
}
}
// inner class for button event handling
private class ButtonHandler implements ActionListener
{
// handle button event
@Override
public void actionPerformed(ActionEvent event)
{
++eventCount; //新事件被触发,事件数加一
startTime = new Date(System.currentTimeMillis()); //获取本此事件开始时间
startTimes.add(startTime); //将开始时间添加到事件开始时间数组
//如果本次事件距离上次事件的时间超过500毫秒,则重置事件信息区
if((startTime.getTime()-endTime.getTime() >3000) &&
eventsInfo.size()>0) {
allEventsInfo = ""; //清空事件信息字符串
eventsInfoJTextArea.setText(allEventsInfo); //刷新文本区
eventCount =1; //事件数重置为1 (从本次事件开始重新统计)
eventsInfo.clear(); //清空事件信息ArrayList
startTimes.clear(); //清空事件开始时间数组
endTimes.clear(); //清空事件结束时间数组
startTime = new Date(System.currentTimeMillis()); //获取开始时间
startTimes.add(startTime); //将开始时间添加到事件开始时间数组
}
eventsInfo.add(event.toString()); //将本次事件添加到事件数组
eventsInfo.set(eventCount-1, "事件" + (eventCount) + "开始于:"+ //保存本次事件信息到数组
simpleDateFormat.format(startTimes.get(eventCount-1))+
(eventsInfo.size()>1 //只有事件数大于等于两条时,才存在上一事件
&&(eventCount-1)<endTimes.size() //防止数组越界
&&(eventCount>1)? //第一条事件不存在上一事件
", 上一事件" + (eventCount-1) + "结束于:"+
simpleDateFormat.format(endTimes.get(eventCount-2)):"") +
event.toString()); //本次事件的字符串表示
allEventsInfo = ""; //拼接事件信息前,先清空事件信息字符串
for (int i=0;i<eventsInfo.size();i++) {
allEventsInfo += String.format(eventsInfo.get(i)+"%n%n"); //拼接事件信息
}
eventsInfoJTextArea.setText(allEventsInfo); //刷新事件信息区
endTime = new Date(System.currentTimeMillis()); //记录当前时间,作为事件结束时间
endTimes.add(endTime); //添加事件结束时间
}
}
// 清除内容,重新统计事件信息
private class ClearButtonHandler implements ActionListener
{
// handle button event
@Override
public void actionPerformed(ActionEvent event)
{
eventsInfo.clear(); //清空事件信息数组
allEventsInfo = ""; //重置拼接字符串
eventsInfoJTextArea.setText(allEventsInfo); //刷新信息显示文本区
eventCount = 0; //重置事件计数器
statusBar.setText(""); //重置statusBar
demoJTextArea.setText("");
}
}
// inner class to handle mouse click events only
private class MouseClickHandler extends MouseAdapter
{
// handle mouse-click event and determine which button was pressed
@Override
public void mouseClicked(MouseEvent event)
{
displayMouseEvents(event);
}
}
// inner class to handle mouse events
private class MouseHandler implements MouseListener,
MouseMotionListener
{
// handle mouse-click event and determine which button was pressed
@Override
public void mouseClicked(MouseEvent event)
{
displayMouseEvents(event);
}
// handle event when mouse pressed
@Override
public void mousePressed(MouseEvent event)
{
displayMouseEvents(event);
}
// handle event when mouse released
@Override
public void mouseReleased(MouseEvent event)
{
displayMouseEvents(event);
}
// handle event when mouse enters area
@Override
public void mouseEntered(MouseEvent event)
{
displayMouseEvents(event);
mouseJTextArea.setBackground(Color.GREEN);
}
// handle event when mouse exits area
@Override
public void mouseExited(MouseEvent event)
{
displayMouseEvents(event);
mouseJTextArea.setBackground(Color.WHITE);
}
// MouseMotionListener event handlers
// handle event when user drags mouse with button pressed
@Override
public void mouseDragged(MouseEvent event)
{
displayMouseEvents(event);
}
// handle event when user moves mouse
@Override
public void mouseMoved(MouseEvent event)
{
displayMouseEvents(event);
}
}
private class CheckBoxHandler implements ItemListener
{
// respond to checkbox events
@Override
public void itemStateChanged(ItemEvent event)
{
++eventCount; //新事件被触发,事件数加一
startTime = new Date(System.currentTimeMillis()); //获取本此事件开始时间
startTimes.add(startTime); //将开始时间添加到事件开始时间数组
//如果本次事件距离上次事件的时间超过500毫秒,则重置事件信息区
if((startTime.getTime()-endTime.getTime() >3000) &&
eventsInfo.size()>0) {
allEventsInfo = ""; //清空事件信息字符串
eventsInfoJTextArea.setText(allEventsInfo); //刷新文本区
eventCount =1; //事件数重置为1 (从本次事件开始重新统计)
eventsInfo.clear(); //清空事件信息ArrayList
startTimes.clear(); //清空事件开始时间数组
endTimes.clear(); //清空事件结束时间数组
startTime = new Date(System.currentTimeMillis()); //获取开始时间
startTimes.add(startTime); //将开始时间添加到事件开始时间数组
}
eventsInfo.add(event.toString()); //将本次事件添加到事件数组
allEventsInfo = ""; //清空事件信息字符串
eventsInfo.set(eventCount-1, "事件" + (eventCount) + "开始于:"+ //保存本次事件信息到数组
simpleDateFormat.format(startTimes.get(eventCount-1))+
(eventsInfo.size()>1 //只有事件数大于等于两条时,才存在上一事件
&&(eventCount-1)<endTimes.size() //防止数组越界
&&(eventCount>1)? //第一条事件不存在上一事件
", 上一事件" + (eventCount-1) + "结束于:"+
simpleDateFormat.format(endTimes.get(eventCount-2)):"") +
event.toString()); //本次事件的字符串表示
for (int i=0;i<eventsInfo.size();i++) {
allEventsInfo += String.format(eventsInfo.get(i)+"%n%n"); //拼接事件信息
}
eventsInfoJTextArea.setText(allEventsInfo); //刷新事件信息区
endTime = new Date(System.currentTimeMillis()); //记录当前时间,作为事件结束时间
endTimes.add(endTime); //添加事件结束时间
}
}
private void displayMouseEvents(MouseEvent event) {
++eventCount; //新事件被触发,事件数加一
startTime = new Date(System.currentTimeMillis()); //获取本此事件开始时间
startTimes.add(startTime); //将开始时间添加到事件开始时间数组
//如果本次事件距离上次事件的时间超过500毫秒,则重置事件信息区
if((startTime.getTime()-endTime.getTime() >3000) &&
eventsInfo.size()>0) {
allEventsInfo = ""; //清空事件信息字符串
eventsInfoJTextArea.setText(allEventsInfo); //刷新文本区
eventCount =1; //事件数重置为1 (从本次事件开始重新统计)
eventsInfo.clear(); //清空事件信息ArrayList
startTimes.clear(); //清空事件开始时间数组
endTimes.clear(); //清空事件结束时间数组
startTime = new Date(System.currentTimeMillis()); //获取开始时间
startTimes.add(startTime); //将开始时间添加到事件开始时间数组
}
eventsInfo.add(event.toString()); //将本次事件添加到事件数组
allEventsInfo = ""; //清空事件信息字符串
eventsInfo.set(eventCount-1, "事件" + (eventCount) + "开始于:"+ //保存本次事件信息到数组
simpleDateFormat.format(startTimes.get(eventCount-1))+
(eventsInfo.size()>1 //只有事件数大于等于两条时,才存在上一事件
&&(eventCount-1)<endTimes.size() //防止数组越界
&&(eventCount>1)? //第一条事件不存在上一事件
", 上一事件" + (eventCount-1) + "结束于:"+
simpleDateFormat.format(endTimes.get(eventCount-2)):"") +
event.toString()); //本次事件的字符串表示
for (int i=0;i<eventsInfo.size();i++) {
allEventsInfo += String.format(eventsInfo.get(i)+"%n%n"); //拼接事件信息
}
eventsInfoJTextArea.setText(allEventsInfo); //刷新事件信息区
endTime = new Date(System.currentTimeMillis()); //记录当前时间,作为事件结束时间
endTimes.add(endTime); //添加事件结束时间
}
private void displayKeyEvents(KeyEvent event) {
++eventCount; //新事件被触发,事件数加一
startTime = new Date(System.currentTimeMillis()); //获取本此事件开始时间
startTimes.add(startTime); //将开始时间添加到事件开始时间数组
//如果本次事件距离上次事件的时间超过500毫秒,则重置事件信息区
if((startTime.getTime()-endTime.getTime() >3000) &&
eventsInfo.size()>0) {
allEventsInfo = ""; //清空事件信息字符串
eventsInfoJTextArea.setText(allEventsInfo); //刷新文本区
eventCount =1; //事件数重置为1 (从本次事件开始重新统计)
eventsInfo.clear(); //清空事件信息ArrayList
startTimes.clear(); //清空事件开始时间数组
endTimes.clear(); //清空事件结束时间数组
startTime = new Date(System.currentTimeMillis()); //获取开始时间
startTimes.add(startTime); //将开始时间添加到事件开始时间数组
}
eventsInfo.add(event.toString()); //将本次事件添加到事件数组
allEventsInfo = ""; //清空事件信息字符串
eventsInfo.set(eventCount-1, "事件" + (eventCount) + "开始于:"+ //保存本次事件信息到数组
simpleDateFormat.format(startTimes.get(eventCount-1))+
(eventsInfo.size()>1 //只有事件数大于等于两条时,才存在上一事件
&&(eventCount-1)<endTimes.size() //防止数组越界
&&(eventCount>1)? //第一条事件不存在上一事件
", 上一事件" + (eventCount-1) + "结束于:"+
simpleDateFormat.format(endTimes.get(eventCount-2)):"") +
event.toString()); //本次事件的字符串表示
for (int i=0;i<eventsInfo.size();i++) {
allEventsInfo += String.format(eventsInfo.get(i)+"%n%n"); //拼接事件信息
}
eventsInfoJTextArea.setText(allEventsInfo); //刷新事件信息区
endTime = new Date(System.currentTimeMillis()); //记录当前时间,作为事件结束时间
endTimes.add(endTime); //添加事件结束时间
}
}