import java.awt.*; import java.awt.event.*; public class A { public static void main(String args[]) { new ThreadFrame(); } } class ThreadFrame extends Frame implements ActionListener,Runnable { TextField text1,text2; boolean boo; Label label=new Label("欢迎使用本字典"); Button fast=new Button("加速"); Thread Scrollwords=null; ThreadFrame() { setLayout(new FlowLayout()); Scrollwords=new Thread(this); test1=new TextField(10); text2=new TextField(10); add(text1); add(text2); add(fast); add(label); test1.addActionListener(this); fast.addActionListener(this); setBounds(100,100,400,280); setVisible(true); validate(); addWindowListener(new WindowAdapter() { public void WindowClosing(WindowEvent e) {System.exit(0); } }); Scroll
words.start(); } public void run() {while(true) { int x=label.getBounds().x; int y=120; x=x+5; label.setLocation(x,y); if(x>380) { x=10; label.setLocation(x,y); }
try{Scrollwords.sleep(1000); } catch(InterruptedException e){} if(boo) { return; } } } public void actionPerformed(ActionEvent e) { if(text1.getTest().equals("boy")) { text2.setText("男孩"); } else if(text1.getText().equals("die")) { boo=true; } else { test2.setText("没有该单词"); } if(e.getSource()==fast) { Scrollwords.interrupt(); } } }