//java程序如下public class Example15_8{
public static void main(String args[]){
WindowWord win=new WindowWord();
win.setTitle("带滚动字幕的小词典");
}}//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WindowWord extends JFrame implements ActionListener,Runnable{
JTextField inputText,showText;
JLabel lable=new JLabel("欢迎使用本字典");
JButton fast=new JButton("加速滚动");
Thread scrollWord=null;
Police police;
WindowWord(){
setLayout(new FlowLayout());
scrollWord=new Thread(this);
inputText=new JTextField(6);
showText=new JTextField(6);
lable.setFont(new Font("楷体_GB2312",Font.BOLD,24));
add(inputText);
add(showText);
add(fast);
add(lable);
police=new Police();
police.setJTextField(showText);
inputText.addActionListener(police);
fast.addActionListener(this);
setBounds(100,100,400,280);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
scrollWord.start();
}
public void run(){
while(true){
int x=lable.getBounds().x;
int y=50;
x=x+5;
lable.setLocation(x,y);
if(x>380){
x=10;
lable.setLocation(x,y);
try{Thread.sleep(200);}
catch(InterruptedException e){}
String str=inputText.getText();
if(str.equals("end"))
{return;}
}}}
public void actionperformed(ActionEvent e)
{scrollWord.interrupt();}}
//
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Police implements ActionListener{
JTextField showText;
HashMap<String,String>hashtable;
File file=new File("word.txt");
Scanner sc=null;
Police(){
hashtable=new HashMap<String,String>();
try{sc=new Scanner(file);
while(sc.hasNext()){
String englishWord=sc.next();
String chineseWord=sc.next();
hashtable.put(englishWord,chineseWord);
}
}
catch(Exception e){}
}
public void setJTextField(JTextField showText){
this.showText=showText;}
public void actionPerformed(ActionEvent e){
String englishWord=e.getActionCommand();
if(hashtable.containsKey(englishWord))
{String chineseWord=hashtable.get(englishWord);
showText.setText(chineseWord);
}
else{showText.setText("没有此单词");}
}}

解决方案 »

  1.   

    //java程序如下public class Example15_8{
    public static void main(String args[]){
    WindowWord win=new WindowWord();
    win.setTitle("带滚动字幕的小词典");
    }}//
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class WindowWord extends JFrame implements ActionListener,Runnable{
    JTextField inputText,showText;
    JLabel lable=new JLabel("欢迎使用本字典");
    JButton fast=new JButton("加速滚动");
    Thread scrollWord=null;
    Police police;
    WindowWord(){
    setLayout(new FlowLayout());
    scrollWord=new Thread(this);
    inputText=new JTextField(6);
    showText=new JTextField(6);
    lable.setFont(new Font("楷体_GB2312",Font.BOLD,24));
    add(inputText);
    add(showText);
    add(fast);
    add(lable);
    police=new Police();
    police.setJTextField(showText);
    inputText.addActionListener(police);
    fast.addActionListener(this);
    setBounds(100,100,400,280);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    scrollWord.start();
    }
    public void run(){
    while(true){
    int x=lable.getBounds().x;
    int y=50;
    x=x+5;
    lable.setLocation(x,y);
    if(x>380){
    x=10;
    lable.setLocation(x,y);
    try{Thread.sleep(200);}
    catch(InterruptedException e){}
    String str=inputText.getText();
    if(str.equals("end"))
    {return;}
    }}}
    public void actionPerformed(ActionEvent e)
    {scrollWord.interrupt();}}
    //
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    public class Police implements ActionListener{
    JTextField showText;
    HashMap<String,String>hashtable;
    File file=new File("word.txt");
    Scanner sc=null;
    Police(){
    hashtable=new HashMap<String,String>();
    try{sc=new Scanner(file);
    while(sc.hasNext()){
    String englishWord=sc.next();
    String chineseWord=sc.next();
    hashtable.put(englishWord,chineseWord);
    }
    }
    catch(Exception e){}
    }
    public void setJTextField(JTextField showText){
    this.showText=showText;}
    public void actionPerformed(ActionEvent e){
    String englishWord=e.getActionCommand();
    if(hashtable.containsKey(englishWord))
    {String chineseWord=hashtable.get(englishWord);
    showText.setText(chineseWord);
    }
    else{showText.setText("没有此单词");}
    }}
    P大写