package Works_JavaLesson;
import java.awt.*;
import java.awt.event.*;
public class WordShow {
/**
* @param 向一个数组中存入若干单词,
* 创建一个label和两个button,
* 通过按动按钮实现单词的前后翻动
*
* @author ChenJian
*/
public static void main(String[] args) {
WordShowFrame frame = new WordShowFrame();
frame.setSize(300, 200);
frame.setVisible(true);
frame.setLocation(500, 400);
frame.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
}
class WordShowFrame extends Frame{
public WordShowFrame(){
index = 0;
words = new String[]{
"Nice", "Date", "Java", "Good", "Time", "Idioms",
"wear", "cucumber", "thorn", "sleeve", "Irregular", "ASAP"
};
label = new Label(words[index], Label.CENTER);
Button btn1 = new Button("Pre");
Button btn2 = new Button("Next");
btn1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
if(index <= 0){
index = words.length-1;
}
else{
index--;
}
label.setText(words[index]);
}
}
);
btn2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
if(index >= words.length-1){
index = 0;
}
else{
index++;
}
label.setText(words[index]);
}
});
setLayout(new BorderLayout());
add(label, BorderLayout.CENTER);
add(btn1, BorderLayout.WEST);
add(btn2, BorderLayout.EAST);
}
private int index;
private final String[] words;
private final Label label;
}
2009-05-28 09:49:14