1 Star 0 Fork 3

BillchenVip/JAVABASE

forked from wts286010056/JAVABASE 
Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
Example15_7.java 1.74 KB
Copy Edit Raw Blame History
wts286010056 authored 2019-11-13 14:56 . HashMap小英汉词典
package cn.edu.shengda;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
class WordPolice implements ActionListener {
JTextField showText;
HashMap<String,String> hashtable;
WordPolice() {
hashtable=new HashMap<String,String>();
hashtable.put("grandness", "伟大");
hashtable.put("swim", "游泳");
}
public void setJTextField(JTextField showText) {
this.showText=showText;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() instanceof JButton){
System.out.println("按钮被点击");
}else{
String englishWord=e.getActionCommand();
if (hashtable.containsKey(englishWord)) {
String chineseWord = hashtable.get(englishWord);
showText.setText(chineseWord);
} else {
showText.setText("没有此单词");
}
}
}
}
class WindowWord extends JFrame {
JTextField inputText,showText;
WordPolice police; //监视器
WindowWord() {
setLayout(new FlowLayout());
inputText=new JTextField(6);
showText=new JTextField(6);
add(inputText);
add(showText);
for(int i=0;i<10;i++){
JButton button=new JButton("按钮"+i);
button.addActionListener(police);
add(button);
}
police=new WordPolice();
police.setJTextField(showText);
inputText.addActionListener(police);
setBounds(100,100,400,280);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Example15_7 {
public static void main(String args[]) {
WindowWord win=new WindowWord();
win.setTitle("英-汉小字典");
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/billchenvip/JAVABASE.git
git@gitee.com:billchenvip/JAVABASE.git
billchenvip
JAVABASE
JAVABASE
master

Search

D67c1975 1850385 1daf7b77 1850385