2、 用户从键盘上输入10个0到99之间的数字,统计数字分布,使用Java Appl...
发布网友
发布时间:2024-10-08 01:40
我来回答
共1个回答
热心网友
时间:2024-10-08 22:48
import javax.swing.*;
import java.util.*;
import java.util.Map.*;
import java.awt.event.*;
import java.applet.Applet;
/**
* @author wubenhua
* @program 监听键盘事件统计字符个数
**/
public class IntegerCount extends Applet {
private JButton bt_Clear = new JButton("Clear"); //清除文本框及容器内容
private JTextArea disTextArea = new JTextArea(); //显示统计好的字符情况
private JTextArea inputTextArea = new JTextArea(); //接收键盘输入的字符并且监听键盘的输入
//为显示文本框添加滚动条(水平与垂直方向)
private JScrollPane scrollOut = new JScrollPane(disTextArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//为接收文本框添加垂直文本框
private JScrollPane scrollIn = new JScrollPane(inputTextArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//声明一个Map接口的引用指向其子类的一个泛型对象,用来记录每一个数字出现的个数;
private Map<Integer,Integer> map = null;
private StringBuffer array = new StringBuffer();
private static final int ONE = 1 ; //若监听到的字符没有出现过,则设置其出现一次;
private JRadioButton [] rb = {new JRadioButton("keyTyped"), new JRadioButton("keyPressed"), new JRadioButton("keyReleased")};
public void init() {
setLayout(null); //设置布局为null
setSize(600,350);
map = new HashMap<Integer,Integer>(); //为Map引用new一个HashMap的对象
/*-----------------接收文本框------------------*/
inputTextArea.setLineWrap(true); //输入文本框设置为自动换行
scrollIn.setLocation(5, 200);
scrollIn.setSize(580, 80);
add(scrollIn);
inputTextArea.addKeyListener(new textAreaKeyListener());//为接收文本框注册一个监听器
/*----------------显示文本框---------------------*/
disTextArea.setEditable(false); //显示文本框设置为不可编辑
scrollOut.setLocation(5, 5);
scrollOut.setSize(580, 190);
add(scrollOut);
bt_Clear.setLocation(450,300);
bt_Clear.setSize(100,30);
add(bt_Clear);
bt_Clear.addActionListener(new ActionListener(){
//为清除按钮注册一个监听事件
public void actionPerformed(ActionEvent e) {
disTextArea.setText("");
inputTextArea.setText("");
map.clear();
System.out.println("\n清空完毕!\n");
JOptionPane.showConfirmDialog(null, "清空完毕!", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
inputTextArea.requestFocus();
}
});
ButtonGroup rg = new ButtonGroup();
for(int i = 0; i<rb.length; i++)
{
rb[i].setLocation(10+150*i,300);
rb[i].setSize(100,30);
rg.add(rb[i]);
add(rb[i]);
}
rb[0].setSelected(true);//默认选中KeyTyped
}
private class textAreaKeyListener implements KeyListener {
public void accepteMethod(KeyEvent e,Integer data){
if (!map.containsKey(data)) {
map.put(data, ONE);
} else {
int value = map.get(data);
map.put(data, value + 1);
}
disPlay();
//System.out.println(map);
}
public void accepteKey(KeyEvent e) {
char myChar = e.getKeyChar(); // 获得此时按下键盘的ASCII
if(myChar>='0' && myChar<='9')//只接收0-9的字符
{
array.append(myChar);
}
else if(array.length()>0 && myChar == KeyEvent.VK_SPACE)
{
if(array.length()<=2)
{
Integer data = new Integer(array.toString());
array.delete(0, array.length()); //每次都清空
//System.out.println("data:" + data + " length:" + array.length());
accepteMethod(e,data);
}else{
array.delete(0, array.length()); //数据超过100是无效数据清空
}
}
}
/**
*重写KeyListener接口的KeyTyped的方法
*/
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
if(rb[0].isSelected()){
accepteKey(e);
}
}
/**
*重写KeyListener接口的KeyPressed的方法
*/
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(rb[1].isSelected()){
accepteKey(e);
}
}
/**
*重写KeyListener接口的KeyReleased的方法
*/
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if(rb[2].isSelected()){
accepteKey(e);
}
}
}
/**
*输出HashMap容器的键-值对
*/
public void disPlay(){
Iterator<Entry<Integer,Integer>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer,Integer> entry = (Entry<Integer,Integer>) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
if(key.equals('\n')) {
disTextArea.append(" 'enter' ->"+ value + "个 " );
System.out.print(" 'enter' ->"+ value + "个 " );
}
else{
disTextArea.append(" " + ""+key +"->"+ value + "个 " );
System.out.print(" " + ""+key +"->"+ value + "个");
}
}
disTextArea.append("\n");
System.out.println();
}
}