label.setFont(font); label.repaint(); validate(); }
public void actionPerformed(ActionEvent e) { if(e.getSource()==yes) { state=YES;
【代码2】 //对话框设置为不可见 }
else if(e.getSource()==cancel) { state=NO;
【代码3】 //对话框设置为不可见 } }
public int getState() { return state; }
public Font getFont() { return font; } }
FrameHaveDialog.java
import java.awt.event.*; import java.awt.*;
import javax.swing.JTextArea;
public class FrameHaveDialog extends Frame implements ActionListener { JTextArea text; Button buttonFont; FrameHaveDialog()
{ buttonFont=new Button(\设置字体\
text=new JTextArea(\实用教程(第三版)\ buttonFont.addActionListener(this); add(buttonFont,BorderLayout.NORTH); add(text);
setBounds(60,60,300,300); setVisible(true); validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e) { System.exit(0); } } ); } 41
public void actionPerformed(ActionEvent e) { if(e.getSource()==buttonFont)
{ FontDialog dialog=【代码4】 //创建对话框 【代码5】 //对话框设置为可见
【代码6】 //对话框设置设置标题为“字体对话框” if(dialog.getState()==FontDialog.YES) { text.setFont(dialog.getFont()); text.repaint(); }
if(dialog.getState()==FontDialog.NO) { text.repaint(); } } } }
FontDialogMainClass.java
public class FontDialogMainClass
{ public static void main(String args[]) { new FrameHaveDialog(); } }
实验3 英语单词拼写训练
1.答案:
【代码1】: addFocusListener(this);
【代码2】: addMouseListener(this);
【代码3】: label[k].addKeyListener(this); 【代码4】: e.getKeyCode()==KeyEvent.VK_LEFT 【代码5】: e.getKeyCode()==KeyEvent.VK_RIGHT
2.模板代码 RondomString.java
public class RondomString { String str=\
public String getRondomString(String s) { StringBuffer strBuffer=new StringBuffer(s); int m=strBuffer.length(); for(int k=0;k { int index=(int)(Math.random()*strBuffer.length()); char c=strBuffer.charAt(index); str=str+c; 42 strBuffer=strBuffer.deleteCharAt(index); } return str; } } LetterLabel.java import java.awt.*; import java.awt.event.*; public class LetterLabel extends Button implements FocusListener,MouseListener { LetterLabel() { 【代码1】 //将当前对象注册为自身的焦点视器 【代码2】 //将当前对象注册为自身的标监视器 setBackground(Color.cyan); setFont(new Font(\ } public static LetterLabel[] getLetterLabel(int n) { LetterLabel a[]=new LetterLabel[n]; for(int k=0;k return a; } public void focusGained(FocusEvent e) { setBackground(Color.red); } public void focusLost(FocusEvent e) { setBackground(Color.cyan); } public void mousePressed(MouseEvent e) { requestFocus(); } public void setText(char c) { setLabel(\ } public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e){} } SpellingWordFrame.java import java.awt.*; import java.awt.event.*; import javax.swing.Box; 43 public class SpellingWordFrame extends Frame implements KeyListener,ActionListener { TextField inputWord; Button button; LetterLabel 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); 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(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(0); } } ); } public void actionPerformed(ActionEvent e) { word=inputWord.getText(); int n=word.length(); RondomString rondom=new RondomString(); String randomWord=rondom.getRondomString(word); wordBox.removeAll(); messaageLabel.setText(hintMessage); if(n>0) { label=LetterLabel.getLetterLabel(n); for(int k=0;k 44 { 【代码3】 //将当前窗口注册为label[k]的键盘监视器 label[k].setText(\ wordBox.add(label[k]); } 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 if(index!=0) { String temp=label[index].getText(); label[index].setText(label[index-1].getText()); label[index-1].setText(temp); label[index-1].requestFocus(); } } else if(【代码5】) //判断按下的是否是→键 { for(int k=0;k if(index!=label.length-1) { String temp=label[index].getText(); 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) 45