1、实训八 GUI编程练习(二) 实训性质:验证性、程序设计 实训目的 (1)掌握事件监听机制 (2)掌握ActionEvent事件的处理 (3)掌握TextEvent事件的处理 实训环境 Window XP/Sever 2003、JDK、Eclipse 实训内容 (1)英语单词拼写训练 (2)简易计算器界面 (3)操作按钮移动 实训指导 1. 英语单词拼写训练 (1)编写Java程序,窗口中有一个TextField对象和一个按钮对象,将这两个对象添加到一个面板中,然后再添加到窗口北面。 用户在TextFeild对象中输入一个单词,回车或者单机按钮,程序将创建
2、若干个标签,其个数刚好等于单词字母个数,且每个标签上的字母刚好是单词的一个字母,但顺序不对。要求将这些标签添加到一个面板中,然后将该面板添加到窗口中心。 用户用鼠标单击一个标签后,通过按下键盘上的上、下、左、右键,交换相邻标签上的字母,使得标签的字母顺序与输入单词的顺序相同。 程序模板如下: RondomString.java public class RondomString { String str=""; public String getRondomString(String s) { StringBuffer strBuffer=new StringBuf
3、fer(s);
int m=strBuffer.length();
for(int k=0;k 4、ort java.awt.*;
import java.awt.event.*;
public class LetterLabel extends Button implements FocusListener,MouseListener
{ LetterLabel()
{ 【代码1】 //将当前对象注册为自身的焦点视器
【代码2】 //将当前对象注册为自身的标监视器
setBackground(Color.cyan);
setFont(new Font("",Font.BOLD,30));
}
publi 5、c static LetterLabel[] getLetterLabel(int n)
{ LetterLabel a[]=new LetterLabel[n];
for(int k=0;k 6、cusEvent e)
{ setBackground(Color.cyan);
}
public void mousePressed(MouseEvent e)
{ requestFocus();
}
public void setText(char c)
{ setLabel(""+c);
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseEx 7、ited(MouseEvent e) {}
public void mouseClicked(MouseEvent e){}
}
SpellingWordFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.Box;
public class SpellingWordFrame extends Frame implements KeyListener,ActionListener
{ TextField inputWord;
Button button;
Letter 8、Label label[];
Panel northP,centerP;
Box wordBox;
String hintMessage="用鼠标单击字母,按左右箭头交换字母,将其排列成所输入的单词";
Label messaageLabel=new Label(hintMessage);
String word="";
SpellingWordFrame()
{ inputWord=new TextField(12);
button=new Button("确定");
button.addActionListener(this); 9、
inputWord.addActionListener(this);
northP=new Panel();
northP.add(new Label("输入一个英文单词:"));
northP.add(inputWord);
northP.add(button);
centerP=new Panel();
wordBox=Box.createHorizontalBox();
centerP.add(wordBox);
add(northP,BorderLayout.NORTH);
add( 10、centerP,BorderLayout.CENTER);
add(messaageLabel,BorderLayout.SOUTH);
setBounds(100,100,350,180);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit( 11、0);
}
}
);
}
public void actionPerformed(ActionEvent e)
{ word=inputWord.getText();
int n=word.length();
RondomString rondom=new RondomString();
String randomWord=rondom.getRondomString(word);
12、 wordBox.removeAll();
messaageLabel.setText(hintMessage);
if(n>0)
{ label=LetterLabel.getLetterLabel(n);
for(int k=0;k 13、);
}
validate();
inputWord.setText(null);
label[0].requestFocus();
}
}
public void keyPressed(KeyEvent e)
{ LetterLabel sourceLabel=(LetterLabel)e.getSource();
int index=-1;
if(【代码4】) //判断按下的是否是←键)
{ for(int k=0;k 14、)
{ if(label[k]==sourceLabel)
{ index=k;
break;
}
}
if(index!=0)
{ String temp=label[index].getText();
label[index].setText(label[index-1].getText());
label[index-1].setText(temp);
label[index-1].re 15、questFocus();
}
}
else if(【代码5】) //判断按下的是否是→键
{ for(int k=0;k 16、
label[index].setText(label[index+1].getText());
label[index+1].setText(temp);
label[index+1].requestFocus();
}
}
validate();
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e)
{ String success="";
for(int k=0;k 17、gth;k++)
{ String str=label[k].getText();
success=success+str;
}
if(success.equals(word))
{ messaageLabel.setText("恭喜你,你成功了");
for(int k=0;k 18、 label[k].setBackground(Color.green);
}
inputWord.requestFocus();
}
}
}
WordMainClass.java
public class WordMainClass
{ public static void main(String args[])
{ new SpellingWordFrame();
}
}
2. 字符串逆序输出
(1)编写JAVA程序,实现简易计算器界面,如下图所示。(注,只需要实现界面效果,不需要实现计算功能)
程序运行如下图:
3. 控制按钮移动
(1)编程,在窗口中添加一个按钮,能够通过按键盘上的上、下、左、右键,控制按钮的移动。






