




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、23/23本題的功能是計算二維數(shù)組arr=34,21,45,67,20),23,10,3,45,76,22,3,79,56,50中的最小值,并輸出。參考答案:第1處:j5或j4第2處:j0第3處:i或i1或ii1本題的功能是通過鼠標確定兩個點,然后畫兩點間的直線。窗口中有個按鈕“Draw line”,單擊該按鈕后,它就處于按下狀態(tài),然后用鼠標在窗口中單擊下,在單擊的地方就會出現(xiàn)個坐標圓點,用鼠標在另外個地方單擊下又會出現(xiàn)另外個圓點,并且此時在兩個坐標圓點間畫出條直線,且“Draw line”處于可用狀態(tài),再單擊這個按鈕就可以畫另外條直線。參考答案:第1處:Point第2處:mevtgetPoi
2、nt下面的程序中,有兩個文本框,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;i20;i+)Systemoutprint(”a”+i+”=”+ai+”,“);參考答案:第1處
3、:new int020第2處:i0;i20第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;while(i3)while( )if(arrijmin)min=arrij;j+; ; ;Systemoutprintln(The
4、min:+min);參考答案:第1處:j5或j=O)SystemOUtprint(arrn+);參考答案:第1處:int n第2處:arr1ength-1第3處:n-或n=n-1或n-=l.本題定義了一個方法add(),用于求兩個整形數(shù)的和。方法中有兩個整形參數(shù)a和b,方法體中計算a和b的和sum,并將結(jié)果返回。程序中調(diào)用add()方法求整數(shù)24和34的和,并將結(jié)果打印輸出。public class javalpublic static void main(Stringargs)int a=24,b=34;Systemoutprintln(add(a,b);public static int
5、add( );suma+b;參考答案:第1處:int a,int b第2處:int sum第3處:return sum本題將一個整形變量opl的二進制的低3位全部變成1。public class javalpublic static void main(Stringargs)opl=21;int op2=7:res;Systemoutprintln(res);參考答案:第1處:int第2處:int第3處:res=oplop2本題的功能是計算l10之間除了5以外的各個自然數(shù)的和。public class javalpublic static void main(Stringargs)int i=
6、1 ;int sum=0;while(i=10)if(i= =5);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*;import javautil*;import javaxswing*;public class java2public static
7、 void main(Stringargs)MouseFrame frame=new MouseFrame();framesetDefaultCloseoperation(JFrameEXIT_0N CLOSE);frameshow();class MouseFrame extends JFramepublic MouseFrame()setTitle(java2);setSize(WIDTH,HEIGHT);MousePanel panel=new MousePanel();Container contentPane=getContentPane();contentPaneadd(panel
8、);public static final int WIDTH = 300;public static final int HEIGHT=200;class MousePanel extends JPanelpublic MousePanel()addMouseListener(new MouseHandler();addMouseMotionListener(new MouseMotionHan-dler();public void paintComponent(Graphics g)(superpaintComponent(g);String text=鼠標指針位置:+mousex+:+m
9、ousey;gdrawString(text,10,10);private int mousex,mousey;private class MouseMotionHandlerpublic void mouseMoved(MouseEvent event)mousex=eventgetX();mousey=eventgetY();repaint();public void mouseDragged(MouseEvent event)mousex=eventgetX();mousey=eventgetY();repaint();private class MouseHandlerpublic v
10、oid mousePressed(MouseEvent eveat)mousex=eventgetX();mousey=eventgetY();參考答案:第1處:implements MouseMotionListener第2處:extends MouseAdapter本題主窗口中包括一個文本框和一個文本域,在上面的文本框中輸入一個整數(shù)并按回車鍵,就會在下面的文本域中顯示該整數(shù)的平方值;如果在文本框中輸入的不是一個整數(shù),將彈出一個警告窗口。tmport javaawtevent*:import javaawt*;import javaxswingJOptionPane;class Dwindo
11、w extends Frame implements ActionLis-tenerTextField inputNumber;TextArea show;Dwindow(String s)super(s):inputNumber=new TextField(22);inputNumberaddActionListener(this);show=new TextArea();add(inputNumber,BorderLayoutNORTH);add(show,BorderLayoutCENTER);setBounds(60,60,300,300);setVisible(true);valid
12、ate();addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)Systemexit(0););public void actionPerformed(ActionEvent e)boolean boo=false;if(egetSource()= =inputNumber)String s=;char a=StoCharArray();for(int i=0;ialength;i+)if(!(CharacterisDigit(ai)boo=true;if(boo= =true)JOption
13、PaneshowMessageDialog(this,您輸入了非法字符,警告對話框,);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);參考答案:第1處:inputNumbergetText()第2處:JOptionPaneWARNING_MESSAGE本題是一個Ap
14、plet,它的功能是在窗口上添加1212個標簽,并且橫向和縱向標簽的顏色為黑白相間。import javaapplet*;import javaawt*;import java,awtevent*;pubhc class java2extends AppletGridLayout grid;pubhc void init()grid=new GridLayout(12,12);setLayout(grid);Label=new Label1212;for(int i=0;i12;i+)for(int j=0;j12;j+)labelij=;if(i+j)2= =0)labelijsetBack
15、ground(Colorblack);elselabelijsetBackground(colorwhite);add(labelij); 參考答案:第1處:label第2處:new label()本題中定義了一個樹型的通信錄,窗El左側(cè)是一個樹,右側(cè)是一個文本域,單擊樹的結(jié)點,則在右側(cè)文本域中顯示相關(guān)信息,如果單擊的是樹結(jié)點,則顯示對應(yīng)名字的電話信息。import javaxswing*;import javaxswingtree*;import javaawt*;import javaawtevent*;import javaxswingevent*;class Mytree2 exten
16、ds JFrameJTree tree=null;JTextArea text=new JTextArea(20,20);Mytree2()Container con=getContentPane();DefauhMutableTreeNode root=new Default-MutableTreeNode(同學通信錄);DefaultMutableTreeNode tl=new Default-MutableTreeNode(大學同學);DefaultMutableTreeNode t2=new Default-MutableTreeNode(研究生同學);DefaultMutableTr
17、eeNode tll=new Default-MutatleTreeNode(陳艷);DefaultMutableTreeNode tl2=new Default-MutableTreeNode(李小永);DefaultMutableTreeNode t21=new Defauh-MutableTreeNode(王小小);DefauhMutableTreeNode t22=new Defauh-MutableTreeNode(董小);setTitle(java2);rootadd(t1);rootadd(t2);tladd(t1_1);tladd(t1_2);t2add(t2_1);t2add
18、(t2_2);tree=new JTree(root);JSerollPane scrollpane=new JScrollPane(text);JSplitPane splitpane=new JSplitPane(JSplitPaneHORIZONTAL SPLIT,true,tree,scrollpane);treeaddTreeSeleetionListener(this);conadd(splitpane);addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)Systemexit(
19、0););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(strequals(陳艷)(textsetText(str+:聯(lián)系電話;else if(strequal
20、s(李小永)textsetText(str+:聯(lián)系電話;else if(strequals(王小小)textsetText(str+:聯(lián)系電話;)else if(strequals(董小)textsetText(str+:聯(lián)系電話;elsetextsetText(nodegetUserObject()toString();public class java2public static void main(String args)Mytree2 win=new Mytree2();winpack();參考答案:
21、第1處:implements TreeSelectionListener第2處:nodetoString()本題中,主窗口有一個按鈕“打開對話框”和一個文本域,單擊按鈕“打開對話框”后會彈出一個對話框,對話框上有兩個按鈕“Yes和“N0”,單擊對話框上的“Yes”和“N0”按 鈕后返回主窗口,并在右側(cè)文本域中顯示剛才所單擊的按鈕信息。import javaawtevent*;import javaawt*;class MyDialogimplements ActionListenerstatic final int YES=1,N0=0;int message=-1;Button yes,no
22、;MyDialog(Frame fString 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 windowClosing(WindowEvent e)message=-1;se
23、tVisible(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 Frame implements ActionLis-tenerTextArea text;Button button;MyDialog dia
24、log;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(this,Dialog,true);setBounds(60,60,300,300);setVisible(true);validate();addWindowListener(new WindowAdapter()public void wi
25、ndowClosing(WindowEvent e)Systemexit(0););public void actionPerformed(ActionEvent e)if(egetSource()= =button);if(dialoggetMessage()= =MyDialogYES)textappend(n你單擊了對話框的yes按鈕);else if(dialoggetMessage()= =MyDialogNO)textappend(n你單擊了對話框的N0按鈕);public class java2public static void main(String args)new Dwi
26、ndow(java2);參考答案:第1處:extends Dialog第2處:dialogsetVisible(true)題中,在下畫線上填寫代碼,指定變量b為字節(jié)型,變量f為單精度實型,變量1為64位整型。public class javalpublic static void main(String-args)b=49;f=89f;l=0 xfedl;Systemoutprintln(b=+b);Systemoutprintln(f=+f):SystemOUtprintln(l=+1);參考答案:第1處:byte第2處:float第3處:long本題的功能是讀人運行程序時所傳入的參數(shù)(一個
27、或多個),并將參數(shù)依次顯示出來,比如運行程序:java javalpartl part2,則打印輸出為:partl part2。public class javalpublic static void main(Stringargs)int i=0:while()SystemOUtprint(+);Systemoutprintln();參考答案:第1處:iargslength第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(
28、Stringargs)(int 3score=56。90。89,23,45,61,60,59;int hum=0;int i=0:while()if()sum+:i+;Systemoutprintln(”60:”+sum);參考答案:第1處:int sum=0第2處:iscorelength第3處:scorei60或scorei=59本題的功能是用冒泡法對數(shù)組元素arr=30,1,-9,70)進行從小到大排列。冒泡法排序是比較相鄰的兩個元素的大小,然后把小的元素交換到前面。public class javalpublic static void main(Stringargs)int i,j;
29、int arr=30,1,-9,70);int n=;for(i=0;in-1;i+)for(j=i+1;jarrj)int temp=arri;for(i=0;i0)choiceadd(name);choiceselect(name);areaappend(n添加+name);else if(egetSource()= =del)choiceremove();areaappend(n刪除+choicegetSelectedItem();參考答案:第1處:choicegetSelectedltem()第2處:choicegetSelectedIndex()本題使用下拉菜單來控制字體,窗口中有一
30、個標簽和一個下拉菜單,當選中下拉菜單中的任一項字體時,標簽上字符串的字體就隨之改變。import javaawt*;import javaawtevent*;import javaxswing*;class ComboBoxFrame extends JFrame public ComboBoxFrame()setTitle(java2);setSize(300,200);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)Systemexit(0););style=new JComboBox
31、():stylesetEditable(true);styleaddhem(Serif);styleaddItem(SansSerif);styleaddhem(Monospaced);styleaddhem(Dialog);styleaddhem(Dialoglnput);styleaddActionListener(this);JPanel p=new JPanel();Padd(style);getContentPane()add(p,South);panel=new ComboBoxTestPanel();getContentPane()add(panel,Center);public
32、 void actionPerformed(ActionEvent evt)JComboBox source=(JComboBox);String item=(String)sourcegetSelectedhem():panelsetStyle(item);private ComboBoxTestPanel panel;private JComboBox style;class ComboBoxTestPanel extends JPanelpublic ComboBoxTestPanel()setStyle(Serif);public void setStyle(String s)setF
33、ont(new Font(S,F(xiàn)ontPLAIN,12);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處:evtgetSource()本題中,生成一個窗口,該窗口
34、的長、寬為屏幕長、寬的一半,并且窗口的大小不能改變。import javaawt*;import javaxswing*;public class java2publicstatic void main(Stringargs)FrameSize frame=new FrameSize();framesetDefaultCloseoperation(JFrameEXITON_CLOSE);frameshow();class FrameSize extends JFramepublic FrameSize()setTitle(java2);Toolkit tk=ToolkitgetDefaultT
35、oolkit();Dimension screenSize=;int screenHeight=screenSizeheight;int screenWidth=screenSizewidth;setSize(screenWidth2,sereenHeight2);參考答案:第1處:tkgetScreenSize()第2處:setResizable(false)本題中定義了一個簡單的計算器,可以進行基本的四則運算。程序中包含16個按鈕用來表示09、+、-、 *、一運算符和小數(shù)點,程序頂部的文本框用來顯示操作數(shù)以及結(jié)果。import javaawt*;import javaawtevent*;i
36、mport javaxswing*;public 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;priva
37、te 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);JPanel P=new JPanel();PsetLayout(new GridLayout(4,4);String buttons=789456*123-0=+:for(int i=0
38、;ibuttonslength();i+)btn=new JButton(buttonssubstring(i,i+1);Padd(btn);add(P,Center);public 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(sequal
39、s(-)displaysetText(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(*)arg*=n;else if(opequals()arg=n;else if(opequals(=)arg=n;displaysetText(+arg);class CalculatorFrame extends JFramepu
40、blic CalculatorFrame()setTitle(java2);setSize(220,180);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)Systemexit(0););Container contentPane=getContentPane();contentPaneadd(new CalculatorPanel();參考答案:第1處:btnaddActionListener(this)第2處:DoubleparseDouble(displaygetText()本題是
41、個表格式的成績單,其中包括“姓名”、“英語成績”、“數(shù)學成績”和“總成績”,姓名和成績都可以進行修改,單擊按鈕“計算每人總成績”,則可以統(tǒng)計出每個人的總成績并顯示在總成績欄中。參考答案:第1處:new JTable(a,name)第2處:aijtoString()本題中定義了個樹型的通信錄,窗口左側(cè)是個樹,右側(cè)是個文本域,單擊樹的結(jié)點,則在右側(cè)文本域中顯示相關(guān)信息,如果單擊的是樹結(jié)點,則顯示對應(yīng)名字的電話信息。import javaxswing*;import j avaxswingtree*;import javaawt*;import javaawtevent*;import javaxs
42、wingevent*;class Mytree2 extends JFrameJTree tree=null;JTextArea text=new JTexLAyea(20,20);Mytree2()Container con=getContentPane();DefaultMutableTreeNode root=new DefaultMutableTreeNode(”同學通信錄”);DefaultMutableTreeNode tl=new DefauhMutableTreeNode(”大學同學”);DefaultMutableTreeNode t2=new DefaultMutableT
43、reeNode(”研究生同學”);DefaultMutableTreeNode tll=new DerauhMutableTreeNode(”陳艷”);DefaultMutableTreeNode tl2=new DefaultMutableTreeNode(”李小永”);DefaultMutableTreeNode t21=new DefaultMutableTreeNode(”王小小”);DefauhMutableTreeNode t22=new DefaultMutableTreeNode(”董小”);setTitle(”java2”);rootadd(t1);rootadd(t2);tladd(ti_1);tladd(t1_2);t2add(t2_1);t2add(t2_2):tree=new JTtee(root);JScrollPane scrollpane=newJScrollPane(text);JSplitPane
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度解除雙方影視制作合作合同
- 2025年度科幻電影總導演專業(yè)聘用合同
- 二零二五年度電子商務(wù)平臺軟件使用及推廣許可協(xié)議
- 2025年度生態(tài)果園產(chǎn)權(quán)及種植技術(shù)引進合同
- 2025年度紡織品普通采購合同書
- 二零二五年度醫(yī)療健康行業(yè)業(yè)務(wù)員委托合同
- 二零二五年度手農(nóng)機售后服務(wù)與技術(shù)支持合同
- 2025年度環(huán)保項目投資欠款付款協(xié)商協(xié)議書
- 二零二五年度民間借貸合同-跨境電商供應(yīng)鏈融資
- 二零二五年度員工股權(quán)激勵與股權(quán)鎖定期協(xié)議
- GA/T 761-2024停車庫(場)安全管理系統(tǒng)技術(shù)要求
- 《設(shè)施節(jié)水灌溉技術(shù)》課件
- 2023年涼山州西昌市人民醫(yī)院招聘衛(wèi)生專業(yè)技術(shù)人員考試真題
- 《中國傳統(tǒng)文化儒家》課件
- 小學三年級每日英語單選題100道及答案解析
- 咨詢公司顧問崗位聘用協(xié)議
- 2024年糖尿病指南解讀
- 環(huán)衛(wèi)應(yīng)急預(yù)案8篇
- 《與顧客溝通的技巧》課件
- 2024年大學生創(chuàng)業(yè)投資意向書
- 【蘇寧易購建設(shè)財務(wù)共享服務(wù)中心的現(xiàn)存問題及優(yōu)化建議探析(論文)13000字】
評論
0/150
提交評論