36.閱讀下列程序,請回答以下問題:
(1)程序要求在文本框text中輸入的內(nèi)容是什么?
(2)程序采用什么輸入方式下載網(wǎng)絡(luò)文件?
import java.net.*; import java.awt.*;
import java.awt.event.*; import java.io.*; import javax.swing.*;
public class Test36{
public static void main(String args[]){
new ConnectNet("讀取網(wǎng)絡(luò)文本文件示意程序");
}
}
class ConnectNet extends JFrame implements ActionListener{
JTextField text = new JTextField(30);
JTextArea showArea=new JTextArea();
JButton b= new JButton("下載"); JPanel p= new JPanel();
ConnectNet(String s){
super(s);Container con = this.getContentPane();
p.add(text); p.add(b);
JScrollPane jsp = new JScrollPane(showArea);
b.addActionListener(this);
con.add(p,"North"); con.add(jsp, "Center");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 400); setVisible(true);
}
public void actionPerformed(ActionEvent e){
String urlName=text.getText();
try{ URL url: new URL(urlName);∥由網(wǎng)址創(chuàng)建URL對象
URLConnection tc =url.openConnection();∥獲得URLConnection對象
tc.connect();∥設(shè)置網(wǎng)絡(luò)連接
InputStreamReader in =
new InputStreamReader(tc.getInputStream());
BufferedReader dis = new BufferedReader(in);
String inLine;
while((inLine=dis.readLine())!=null){
showArea.append(inLine+"");
}
dis.close();
} catch (MalformedURLException e2){e2.printStackTrace();}
catch(IOException e3){ e3.printStackTrace();}
}
}
六、程序設(shè)計題(本大題共2小題,每小題6分,共1 2分)
38.類InputData是用于輸入考生考號、課程和成績的窗口,
窗口的界面如右圖所示,其中三個文本框用來輸入考號、課
程和成績。
注:這里是給定程序的部分代碼,你要編寫的是完成該類的構(gòu)
造方法。
import java.awt.*;import javax.swing.*;
import java.awt.event.*;
public class InputData extends JFrame implements ActionListener{
JTextField noText, markText, courseText;
JButton ok=new JButton("確 定");
public static int no, mark;∥讓創(chuàng)建者直接獲取考號和成績
public static String course;∥讓創(chuàng)建者直接獲取課程名稱
InputData(){
super("成績輸入窗");
Container con = getContentPane();
con.setLayout(new GridLayout(4, 1));
noText = new JTextField(12);
courseText = new JTextField(12);
markText = new JTextField(12);
∥請在答題紙相應(yīng)位置編寫代碼
con.add(ok); ok.addActionListener(this);
con.setSize(250, 70); setVisible(true); pack();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() = ok) { ∥讀取考號和成績
no = Integer.parseInt(noText.getText());
course = courseText.getText();
mark = Integer.parseInt(markText.getText());
setVisible(false); dispose();
}
}
public static void main(String[] args) {
new InputData();
}
}