Java 2实用大学教程实验指导与习题解答(9)

2019-08-31 22:09

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


Java 2实用大学教程实验指导与习题解答(9).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:初级测量放线工试题(带答案)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: