35.閱讀下列程序,請寫(xiě)出該程序的功能。
import java.applet.*:import java.awt.event.*;import javax.swing.*
public class Class 1 extends Applet implements KeyListener{
JButton button=new JButton(″開(kāi)始″);
JTextArea text=new JTextArea(5,20);
public void init() {
button.addKeyListener(this); add(button);add(text);
}
public void keyPressed(KeyEvent e){
int t=e.getKeyCode();
if(t>=KeyEvent.VK_A&& t<=KeyEvent.VK_Z) {
text.append(″ ″+(char)t);
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
36.閱讀下列程序,請寫(xiě)出該程序的功能。
import java.applet.*; import java.awt.*;
public class Test36 extends java.applet.Applet implements Runnable{
Thread myThread = null;
double seta=0.0;
public void start() {
setSize(500,400);
if(myThread=null){ myThread=new Thread(this); myThread.start();}
}
public void run() {
while(myThread!=null) {
try {myThread.sleep(40);
} catch(InterruptedException e){}
seta+=3.0; if(seta>=360)seta=0; repaint();
}
}
public void paint(Graphics g) {
final double pi=3.14159; final double r = 100.0;
int x0=250+(int)(r*Math.cos(3.1415926/180.0*seta));
int y0=200+(int)(r*Math.sin(3.1415926/180.0*seta));
g.setColor(Color.red); g.drawOval(x0,y0,10,10);
}
}
六、程序設計題(本大題共2小題,每小題6分,共1 2分)
37.請編寫(xiě)方法void strReverse(String str),該方法的功能是輸出一個(gè)新字符串,新字符串字符排列順序與原字符串str的字符排列順序相反。例如,strReverse(″ABCD″) 所輸出的結果是″DCBA″。請使用字符串與字節數組的相互轉換方法進(jìn)行設計。
38.請設計實(shí)現如下用于輸入學(xué)號和姓名的對話(huà)框界面,其中空白格是文本框,用于輸入相應的內容。
這里給出的是程序的一部分,你要編寫(xiě)的是類(lèi)InputNoNameDialog的構造方法InputNoNameDialog(JFrame f,String s,JTextField t)。其中參數f是對話(huà)框的依賴(lài)窗口,s是對話(huà)框標題,t是依賴(lài)窗口中顯示對話(huà)框輸入內容的文本框。
以下是類(lèi)InputNoNameDialog的程序框架。
class InputNoNameDialog extends JDialog implements ActionListener{
JLabel title;JTextField textl,text2,mainText;JButton done;
InputNoNameDialog(JFrame f String s,JTextField t) {
super(f,s,true); mainText = t;Container con = getContentPane();
title=new JLabel(s); textl=new JTextField(10);
text2=new JTextField(10); con.setLayout(new GridLayout(3,2));
con.setSize(200,100); setModal(false);
//請在以下位置續寫(xiě)其余代碼
}
public void actionPerformed(ActionEvent e) {
//輸入結束按確定按鈕后,將對話(huà)框中輸入的學(xué)號和姓名在它依賴(lài)窗口的文本框中顯示。
mainText.setText(″學(xué)號:″+textl.getText()+″ 姓名:″+text2.getText());
setVisible(false);dispose();
}
}
聯(lián)系客服