




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、本題的功能是計算二維數(shù)組arr=34,21,45,67,20),23,10,3,45,76,22,3,79,56,50中的最小值,并輸出。參考答案:第1處:j<5或j<4第2處:j0第3處:i或i1或ii1本題的功能是通過鼠標確定兩個點,然后畫兩點間的直線。窗口中有個按鈕“Draw line”,單擊該按鈕后,它就處于按下狀態(tài),然后用鼠標在窗口中單擊下,在單擊的地方就會出現(xiàn)個坐標圓點,用鼠標在另外個地方單擊下又會出現(xiàn)另外個圓點,并且此時在兩個坐標圓點間畫出條直線,且“Draw line”處于可用狀態(tài),再單擊這個按鈕就可以畫另外條直線。 · 參考答案:第1處:Poi
2、nt第2處:mevtgetPoint下面的程序中,有兩個文本框,Input和Output,用戶在Input文本框中輸入字符串后回車,字符串會在Output文本框中出現(xiàn)。· 參考答案:第1處;new Actionlistener()第2處:tflgetText()本題中定義了長度為20的維整型數(shù)組a,并將數(shù)組元素的下標值賦給數(shù)組元素,最后打印輸出數(shù)組中下標為奇數(shù)的元素。public class javalpublic static void main(Stringargs)int a=Int i:forai=ifori+)=1:i=0;i<20;i+)Systemoutprint
3、(”a”+i+”=”+ai+”,“);· 參考答案:第1處:new int020第2處:i0;i<20第3處:i%2 !0本題的功能是計算二維數(shù)組arr=34,21,45,67,20,23,10,3,45,76,22,3,79,56,50中的最小值,并輸出。 public class javalpublic static void main(Stringargs)int arr=34,21,45,67,20),23,10,3,45, 76,22,3,79,56,50; int i=0: intj=0; int min=arr00
4、; while(i<3)while( )if(arrij<min)min=arrij; j+; ; ; Systemoutprintln("The min:"+min); · 參考答案:第1處:j<5或j<=4第2處:j=0第3處:i+或i+=1或i=i+1.本題將數(shù)組中arr=5,6,3,7,9,1
5、的各個元素按下標的逆序輸出。 public class javalpublic static void main(Stringargs)int arr=5,6,3,7,9,1; ; n= ; while(n>=O)SystemOUtprint(arrn+"");
6、; ; · 參考答案:第1處:int n第2處:arr1ength-1第3處:n-或n=n-1或n-=l.本題定義了一個方法add(),用于求兩個整形數(shù)的和。方法中有兩個整形參數(shù)a和b,方法體中計算a和b的和sum,并將結果返回。程序中調(diào)用add()方法求整數(shù)24和34的和,并將結果打印輸出。 public class javalpublic static void main(Stringargs)int a=24,b=34; Systemoutprintln(add(a,b); pub
7、lic static int add( ) ; suma+b; ; · 參考答案:第1處:int a,int b第2處:int sum第3處:return sum本題將一個整形變量opl的二進制的低3位全部變成1。 public
8、 class javalpublic static void main(Stringargs) opl=21; int op2=7: res; ; Systemoutprintln(res);
9、; · 參考答案:第1處:int 第2處:int 第3處:res=oplop2本題的功能是計算l10之間除了5以外的各個自然數(shù)的和。 public class javalpublic static void main(Stringargs)int i=1 ; int sum=0; while(i<=10)if(i= =5) ; ;
10、160; ; i+; Systemoutprintln("sum="+sum); · 參考答案:第1處:i=i+1或i+或i+=1第2處:continue第3處:sum+=i或sum=sum+i本題的功能是獲取鼠標在窗口中的位置。當鼠標移進窗口中,就會實時顯示鼠標在窗口中的相對位置,比如顯示為"鼠標的當前位置:X:Y"(其中,X為橫坐標,Y為縱坐標)。 import javaawt*; import javaawtevent*
11、; import javautil*; import javaxswing*; public class java2public static void main(Stringargs)MouseFrame frame=new MouseFrame(); framesetDefaultCloseoperation(JFrameEXIT_0N CLOSE); frameshow(); class MouseFrame extends JFramepublic MouseFrame()setTitle("java2")
12、; setSize(WIDTH,HEIGHT); MousePanel panel=new MousePanel(); Container contentPane=getContentPane(); contentPaneadd(panel); public static final int WIDTH = 300; public static final int HEIGHT=200; class MousePanel extends JPanelpublic MousePanel()addMouseListe
13、ner(new MouseHandler(); addMouseMotionListener(new MouseMotionHan-dler(); public void paintComponent(Graphics g)(superpaintComponent(g); String text="鼠標指針位置:"+mousex+":"+mousey; gdrawString(text,10,10); private int mousex,mousey; private class MouseM
14、otionHandler public void mouseMoved(MouseEvent event)mousex=eventgetX(); mousey=eventgetY(); repaint(); public void mouseDragged(MouseEvent event)mousex=eventgetX(); mousey=eventgetY(); repaint(); private class MouseHandler
15、 public void mousePressed(MouseEvent eveat)mousex=eventgetX(); mousey=eventgetY(); · 參考答案:第1處:implements MouseMotionListener第2處:extends MouseAdapter本題主窗口中包括一個文本框和一個文本域,在上面的文本框中輸入一個整數(shù)并按回車鍵,就會在下面的文本域中顯示該整數(shù)的平方值;如果在文本框中輸入的不是一個整數(shù),將彈出一個警告窗口。 tmport javaawtevent*:
16、import javaawt*; import javaxswingJOptionPane; class Dwindow extends Frame implements ActionLis-tenerTextField inputNumber; TextArea show; Dwindow(String s)super(s): inputNumber=new TextField(22); inputNumberaddActionListener(this); show=new TextArea(); add(in
17、putNumber,BorderLayoutNORTH); add(show,BorderLayoutCENTER); setBounds(60,60,300,300);setVisible(true); validate(); addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)Systemexit(0); ); public void actionPerformed(ActionEvent e)boolean boo=false;
18、 if(egetSource()= =inputNumber)String s= ; char a=StoCharArray(); for(int i=0;i<alength;i+)if(!(CharacterisDigit(ai)boo=true; if(boo= =true)JOptionPaneshowMessageDialog(this,"您輸入 了非法字符","警告對話框",
19、; ); inputNumbersetText(null); else if(boo= =false)int number=IntegerparseInt(s); showappend("n"+number+"平方:"+(num-bet*number): public class java2public static void main(String args) new Dwindow("java2");
20、;· 參考答案:第1處:inputNumbergetText()第2處:JOptionPaneWARNING_MESSAGE本題是一個Applet,它的功能是在窗口上添加12×12個標簽,并且橫向和縱向標簽的顏色為黑白相間。 import javaapplet*; import javaawt*; import java,awtevent*; pubhc class java2extends AppletGridLayout grid; pubhc void init()grid=new GridLayout(12,12);
21、 setLayout(grid); Label =new Label1212; for(int i=0;i<12;i+)for(int j=0;j<12;j+)labelij= ; if(i+j)2= =0)labelijsetBackground(Colorblack); elselabelijsetBackground(colorw
22、hite); add(labelij); · 參考答案:第1處:label第2處:new label()本題中定義了一個樹型的通信錄,窗El左側是一個樹, 右側是一個文本域,單擊樹的結點,則在右側文本域中顯示 相關信息,如果單擊的是樹結點,則顯示對應名字的電話 信息。 import javaxswing*; import javaxswingtree*; import javaawt*; import javaawtevent*; import javaxswingevent*;
23、160;class Mytree2 extends JFrame JTree tree=null;JTextArea text=new JTextArea(20,20); Mytree2()Container con=getContentPane(); DefauhMutableTreeNode root=new Default-MutableTreeNode("同學通信錄"); DefaultMutableTreeNode tl=new Default-MutableTre
24、eNode("大學同學"); DefaultMutableTreeNode t2=new Default-MutableTreeNode("研究生同學"); DefaultMutableTreeNode tl l=new Default-MutatleTreeNode("陳艷"); DefaultMutableTreeNode tl 2=new Default-MutableTreeNode("李小永"); DefaultMutableTreeNode t
25、2 1=new Defauh-MutableTreeNode("王小小"); DefauhMutableTreeNode t2 2=new Defauh-MutableTreeNode("董小"); setTitle("java2"); rootadd(t1);rootadd(t2); tladd(t1_1);tladd(t1_2);t2add(t2_1);t2 add(t2_2); tree=new JTree(root); JSerollPa
26、ne scrollpane=new JScrollPane(text); JSplitPane splitpane=new JSplitPane(JSplitPane HORIZONTAL SPLIT, true,tree,scrollpane); treeaddTreeSeleetionListener(this); conadd(splitpane); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)Systemexit(0
27、);); setVisible(true);setBounds(70,80,200,300); public void valueChanged(TreeSelectionEvent e)if(egetSouree()= =tree)DefauhMutableTreeNode node=(DefaultMutableTreeNode)treegetLastSelected-PathComponent(); if(nodeisLeaf()(String str= ; if(strequa
28、ls("陳艷")(textsetText(str+":聯(lián)系電話quot;);else if(strequals("李小永") textsetText(str+":聯(lián)系電話quot;); else if(strequals("王小小")textsetText(str+":聯(lián)系電話quot;);)else if(strequals("董小")textsetText(str+"
29、:聯(lián)系電話quot;);elsetextsetText(nodegetUserObject()toString(); public class java2public static void main(String args)Mytree2 win=new Mytree2();winpack();· 參考答案:第1處:implements TreeSelectionListener第2處:nodetoString() 本題中,主窗口有一個按鈕“打開對話框”和一個文本域,單擊按鈕“打開對話框”后會彈出一個對話框,對話框上有兩個
30、按鈕“Yes"和“N0”,單擊對話框上的“Yes”和“N0”按 鈕后返回主窗口,并在右側文本域中顯示剛才所單擊的按鈕信息。 import javaawtevent*; import javaawt*; class MyDialog implements ActionListenerstatic final int YES=1,N0=0; int message=-1;Button yes,no; MyDialog(Frame fString
31、S,boolean b)super(f,S,b); ves=new Button("Yes");yesaddActionListener(this); no=new Button("No"); noaddActionListener(this)osetLayout(new FlowLayout(); add(yes);add(no); setBounds(60,60,100,100); addWindowListener(new WindowAdapter() public void windo
32、wClosing(WindowEvent e)message=-1;setVisible(false);); public void actionPerformed(ActionEvent e)if(egetSource()= =yes)message=YES; setVisible(false); else if(egetSource()= = no)message=NO; setVisible(false); public int getMessage()return message; class Dwindow extends
33、Frame implements ActionLis-tenerTextArea text;Button button;MyDialog dialog; Dwindow(String s)super(s); text=new TextArea(5,22);button=new Button("打開對話框"); buttonaddActionListener(this); setLayout(new FlowLayout(); add(button);add(text); dialog=new MyDialog(th
34、is,"Dialog",true); setBounds(60,60,300,300);setVisible(true); validate(); addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)Systemexit(0);); public void actionPerformed(ActionEvent e)if(egetSource()= =button)
35、; ; if(dialoggetMessage()= =MyDialogYES)textappend("n你單擊了對話框的yes按 鈕"); else if(dialoggetMessage()= =MyDialogNO)textappend("n你單擊了對話框的N0按 鈕"); public class java2public static void main(String args)new Dwindow("java2"); ·
36、 參考答案:第1處:extends Dialog第2處:dialogsetVisible(true)題中,在下畫線上填寫代碼,指定變量b為字節(jié)型,變量f為單精度實型,變量1為64位整型。 public class javalpublic static void main(String-args) b=49; f=89f;
37、0; l=0xfedl; Systemoutprintln("b="+b); Systemoutprintln("f="+f): SystemOUtprintln("l="+1); · 參考答案:第1處:byte第2處:float第3處:long本題的功能是讀人運行程序時所傳入的參數(shù)(一個或多個),并將參數(shù)依次顯示出來,比如運行程序:java javalpartl part2,則打印輸出為:partl part2。 public class
38、javalpublic static void main(Stringargs)int i=0: while( )SystemOUtprint( +""); ; Systemoutprintln(); · 參考答案:第1處:i<
39、argslength 第2處:argsi第3處:i+本題的功能是統(tǒng)計成績不及格的人數(shù),分數(shù)有89,90, 56,90,89,45。23,45,60,59,61。 public class javalpublic static void main(Stringargs)(int 3score=56。90。89,23,45,61,60,59; int hum=0; ; int i=0: while(
40、; )if( )sum+: i+; Systemoutprintln(”<60:”+sum); · 參考答案:第1處:int sum=0第2處:i<scorelength第3處:scorei<60或scorei<=59本題的功能是用冒泡法對數(shù)組元素arr=30,1,-9,70)進行從小到大排列。冒泡法排序是比較相鄰的兩個元素的大小,然后把小的元素交換到前面。 public
41、 class javalpublic static void main(Stringargs)int i,j; int arr=30,1,-9,70); int n= ; for(i=0;i<n-1;i+)for(j=i+1;j<n;j+)if(arri>arrj)int temp=arri; ;
42、60; ; for(i=0;i<n;i+)Systemoutprint(arri+""); · 參考答案:第1處:arrlength第2處:arr=arrj 第3處:arrj=temp 本題的功能是對下拉菜單項的操作,包括添加和刪除。頁面包括一個下拉菜單、一個文本框和兩個按鈕“刪除”和“添加”,選中下拉菜單的一項后,可以通過“刪除”按鈕從下拉菜單中刪除該項,在文本框中填入字符串后,單擊“添加”按鈕就可以將該項添加到下拉菜單中,所有信息都將
43、顯示在右側的文本域中。 import javaawt*; import javaawtevent*; public class java2 extends javaappletApplet imple-ments hemListener,ActionListenerChoice choice; TextField text; TextArea area; Button add,del; public void init() choice:new Choice(); text=new TextF
44、ield(8); area:new TextArea(6,15); choiceadd("音樂天地"); choiceadd("武術天地"); choiceadd("象棋樂園"); choiceadd("交友聊天"); add=new Button("添加"); del=new Button("刪除"); addaddActionListener(this); deladdActionL
45、istener(this); choiceaddItemListener(this); add(choice); add(del);add(text);add(add);add(area); public void itemStateChanged(hemEvent e)String name= ; int index=choicegetSelectedIndex(); areasetText("n"+index+&quo
46、t;:"+name); public void actionPerformed(ActionEvent e)if(egetSource()= =addegetSource()= =text)String name=textgetText(); if(namelength()>0)choiceadd(name); choiceselect(name); areaappend("n添加"+name); else if(egetSource()= =del)choiceremove(
47、0; ); areaappend("n刪除"+choicegetSelectedItem(); · 參考答案:第1處:choicegetSelectedltem()第2處:choicegetSelectedIndex()本題使用下拉菜單來控制字體,窗口中有一個標簽和一個下拉菜單,當選中下拉菜單中的任一項字體時,標簽上字符串的字體就隨之改變。 import javaawt*; import javaawtevent*; import javaxswing*;&
48、#160;class ComboBoxFrame extends JFrame public ComboBoxFrame()setTitle("java2"); setSize(300,200); addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)Systemexit(0); ); style=new JComboBox():
49、 stylesetEditable(true); styleaddhem("Serif"); styleaddItem("SansSerif"); styleaddhem("Monospaced"); styleaddhem("Dialog"); styleaddhem("Dialoglnput"); styleaddActionListener(this); JPanel p=new JPanel();
50、Padd(style); getContentPane()add(p,"South"); panel=new ComboBoxTestPanel(); getContentPane()add(panel,"Center"); public void actionPerformed(ActionEvent evt)JComboBox source=(JComboBox) ; String i
51、tem=(String)sourcegetSelectedhem(): panelsetStyle(item); private ComboBoxTestPanel panel; private JComboBox style; class ComboBoxTestPanel extends JPanelpublic ComboBoxTestPanel()setStyle("Serif"); public void setStyle(String s)setFont(new Font(S,F(xiàn)ontPLAIN,12);
52、0;repaint(); public void paintComponent(Graphics g)superpaintComponent(g); 9drawString("Welcome to China!",0,50); public class java2public static void main(Stringargs)JFrame frame=new ComboBoxFrame(); frameshow(); · 參考答案:第1處:implements ActionListener第2處:
53、evtgetSource()本題中,生成一個窗口,該窗口的長、寬為屏幕長、寬的一半,并且窗口的大小不能改變。 import javaawt*; import javaxswing*; public class java2publicstatic void main(Stringargs)FrameSize frame=new FrameSize(); framesetDefaultCloseoperation(JFrameEXITON_CLOSE); frameshow(); class FrameSize extends JFram
54、epublic FrameSize()setTitle("java2"); Toolkit tk=ToolkitgetDefaultToolkit(); Dimension screenSize= ; int screenHeight=screenSizeheight; int screenWidth=screenSizewidth; setSize(screenWidth2,sereenHeight2);&
55、#160; ; · 參考答案:第1處:tkgetScreenSize() 第2處:setResizable(false) 本題中定義了一個簡單的計算器,可以進行基本的四則運算。程序中包含16個按鈕用來表示09、+、-、 *、一運算符和小數(shù)點,程序頂部的文本框用來顯示操作數(shù)以及結果。 import javaawt*; import javaawtevent*; import javaxswing*; public
56、 class java2public static void main(Stringargs)tryUIManagersetLookAndFeel(UIManagergetSys-temLookAndFeelClassName(); catch(Exception e)JFrame frame=new CalculatorFrame(); frameshow(); class CalculatorPanel extends JPanel implements Ac-tionListenerprivate JTextField display;
57、;private JButton btn; private double arg=0; private String op="="; private boolean start=true; public CalculatorPanel()setLayout(new BorderLayout(); display=new JTextField("0"); displaysetEditable(false); add(display,"North"); JPa
58、nel P=new JPanel(); PsetLayout(new GridLayout(4,4); String buttons="789456*123-0=+": for(int i=0;i<buttonslength();i+)btn=new JButton(buttonssubstring(i,i+1); Padd(btn); ; add(P,"Center"); publi
59、c void actionPerformed(ActionEvent evt)String s=evtgetActionCommand(); if('0 '<=scharAt(0)scharAt(O)<='9' sequals("-")if(start)displaysetText(s): else displaysetText(displaygetText()+s); start=false; elseif(start)if(sequals("-")displayset
60、Text(s): start=false; else op=S; else(double x= ; calculate(x); op=S: start=true; public void calculate(double n)if(opequals("+")arg+=n: else if(opequals("-")arg-=n; else if(opequals
61、("*")arg*=n; else if(opequals("")arg=n; else if(opequals("=")arg=n; displaysetText(""+arg); class CalculatorFrame extends JFramepublic CalculatorFrame()setTitle("java2"); setSize(220,180); addWindowListener(new WindowAda
62、pter()public void windowClosing(WindowEvent e)Systemexit(0); ); Container contentPane=getContentPane(); contentPaneadd(new CalculatorPanel(); · 參考答案:第1處:btnaddActionListener(this)第2處:DoubleparseDouble(displaygetText()本題是個表格式的成績單,其中包括“姓名”、“英語成績”、“數(shù)學成績”和“總成績”,姓名和成績都可以進行修改,單擊按鈕
63、“計算每人總成績”,則可以統(tǒng)計出每個人的總成績并顯示在總成績欄中。· 參考答案:第1處:new JTable(a,name)第2處:aijtoString()本題中定義了個樹型的通信錄,窗口左側是個樹,右側是個文本域,單擊樹的結點,則在右側文本域中顯示相關信息,如果單擊的是樹結點,則顯示對應名字的電話信息。import javaxswing*;import j avaxswingtree*;import javaawt*;import javaawtevent*;import javaxswingevent*;class Mytree2 extends JFrameJTree tree=null;JTextArea text=new JTexLAyea(20,20);Mytree2()Container con=getContentPane();DefaultMutableTree
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 初中寒假生活周記200字(13篇)
- 尋找微塵觀后感350字8篇
- 電影電視劇版權合作協(xié)議
- 湘教版七年級下冊數(shù)學學情分析計劃
- 交通運輸行業(yè)智能交通系統(tǒng)與導航方案
- 城市規(guī)劃原理與城市設計練習題
- 小學生生態(tài)文明知識講座計劃
- 六年級日記去科技館500字7篇
- 部編三年語文上冊第二單元拓展計劃
- 續(xù)寫黃紗巾400字(15篇)
- 國家開放大學《公共行政學》章節(jié)測試參考答案
- 配電箱出廠檢驗報告
- 汽車租賃公司汽車租賃管理制度匯編
- 脊髓損傷康復講義
- 布草洗滌服務方案完整版
- 氣體安全知識培訓(72張)課件
- 共線向量與共面向量全面版課件
- JJG(晉) 22-2021 車用甲醇燃料加注機檢定規(guī)程
- 湘美版小學四年級美術下冊知識點
- 大連市住宅小區(qū)物業(yè)收費等級標準
- 包裝自動線課程設計含全套資料
評論
0/150
提交評論