全國2013年10月自考《Java語言程序設(shè)計(jì)(一)》真題_第5頁
34.閱讀下列程序,請寫出該程序的功能。
import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class Test34 {
public static void main(String[]args){
ComboBoxDemo myComboBoxGUI = newComboBoxDemo();
}
}
Class ComboBoxDemo extends JFrame implemems ItemListener{
String CountryList[]={"美國","中國","英國","俄羅斯","韓國","德國"};
im GoldMedalNumber[]={46,38,29,24,13,11};
JLabel label;JComboBox Combobox;
Public ComboBoxDemo(){
setSize(350,150);
setTitle("第三十屆倫敦奧運(yùn)會(huì)金牌查詢");
Comainer conPane = getContentPane();
conPane.setLayout(new FlowLayout());
combobox=new JComboBox(CountryList);
combobox.addItemListener(this);
conPane.add(combobox);
label=new JLabel(); conPane.add(1abel); this.setVisible(true);
}
public void itemStateChanged(ItemEvem e){
if(e.getS ource()==combobox)
label.setText(
String.valueOf(GoldMedalNumber[combobox.getSelectedIndex()]));
}
}
35.閱讀下列程序,請回答下面的問題:
(1)兩個(gè)文本區(qū)內(nèi)分別每次追加何種字符?有多少個(gè)?
(2)兩個(gè)文本區(qū)里面內(nèi)容的顯示速度有何區(qū)別?
import iava.awt.*;import java.applet.*;import javax.swing.*;
public class Test35 extends Applet{
JTextArea 1Text,rText;
public void init() {
setSize(600,500); setLayout(null);
1Text=new JTextArea(); rText=new JTextArea();
lText.setFont(new Font ("Times New Roman",1,40));
rText.setFont(new Font("Modern",1,40));
JScrollPane jsp1=new JScrollPane(1Text);
JScrollPane jsp2=new JScrollPane(rText);
add(jsp1); add(jsp2);
jspl.setBounds(20,20,250,450); jsp2.setBounds(310,20,200,450);
(new threadl(1Text,"1")).start(); (new threadl(rText,"2")).start();
}
}
class threadl extends Thread{
protected JTextArea t;
String text="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
public thread1(JTextArea text,String m){ super(m); t=text; }
public void run() {
while(true){
String S=" ";
for(int i=0;i<6;i++){
int k;
if(getName().equals("1"))
k=((int)(Math.random()*100)%26);
else
k=((int)(Math.random()*100)%10)+26;
s+=text.charAt(k); //獲取k位置字符
}
t.append(s+"\n");
t.setCaretPosition(t.getText().1ength());//最近輸出的內(nèi)容在文本區(qū)中顯示
try{sleep(getName().equals("1")?400:700);}
catch (InterruptedException e){}
}
}
}
責(zé)編:abcwuli1234