日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java2实用教程(第二版)程序代码——第十四章 Component类的常用方法

發布時間:2023/12/10 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java2实用教程(第二版)程序代码——第十四章 Component类的常用方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??1//例子1
??2import?java.applet.*;import?java.awt.*;
??3import?java.awt.event.*;
??4import?javax.swing.JTextArea;
??5public?class?Example14_1?extends?Applet?implements?ItemListener
??6{??List?list??;?
??7???JTextArea?text;
??8???public?void?init()
??9???{??list=new?List(6,false);?
?10?????text=new?JTextArea(6,15);text.setForeground(Color.blue);
?11?????GraphicsEnvironment?ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
?12?????String?fontName[]=ge.getAvailableFontFamilyNames();
?13??????for(int?i=0;i<fontName.length;i++)
?14????????{??list.add(fontName[i]);
?15????????}

?16??????add(list);
?17??????Panel?p=new?Panel();
?18??????p.setBackground(Color.pink);?
?19??????p.add(text);
?20??????add(p);
?21??????list.addItemListener(this);
?22???}
?
?23???public?void?itemStateChanged(ItemEvent?e)
?24???{??String?name=list.getSelectedItem();
?25??????Font?f=new?Font(name,Font.BOLD,16);
?26??????text.setFont(f);
?27??????text.setText("\nWelcome?歡迎您");
?28???}

?29}

?30
?31//例子2
?32import?java.awt.*;import?java.awt.event.*;
?33public?class?Example14_2?extends?java.applet.Applet?implements?ActionListener
?34{??Button?button;
?35???Label?label;
?36???public?void?init()
?37???{??button=new?Button("橫向走動");?button.setBackground(Color.red);
?38??????button.addActionListener(this);
?39??????label=new?Label("我可以被碰掉",Label.CENTER);
?40??????label.setBackground(Color.yellow);
?41??????add(button);?add(label);
?42???}

?43?public?void?actionPerformed(ActionEvent?e)
?44???{??Rectangle?rect=button.getBounds();
?45??????if(rect.intersects(label.getBounds()))
?46?????????{??label.setVisible(false);
?47?????????}

?48??????if(label.isVisible())
?49?????????{??button.setLocation(rect.x+3,rect.y);
?50?????????}

?51??????else
?52?????????{??button.setLocation(rect.x,rect.y+3);?
?53????????????button.setLabel("縱向走動");
?54?????????}

?55???}

?56}

?57
?58//例子3
?59import?java.awt.*;import?java.awt.event.*;
?60class?MyCanvas?extends?Canvas?
?61{??int?n=-1;
?62???MyCanvas()
?63???{??setSize(150,120);setBackground(Color.pink);
?64???}

?65???public?void?paint(Graphics?g)
?66???{??g.setColor(Color.red);
?67??????g.drawString("部分清除時我將消失",10,12);
?68??????g.drawString("我們正在學習repaint方法",10,80);
?69???}

?70???public?void?setN(int?n)
?71???{??this.n=n;
?72???}

?73???public?void?update(Graphics?g)?
?74???{??int?width=0,?height=0;
?75??????width=getSize().width;height=getSize().height;
?76??????if(n==0)
?77????????{??g.clearRect(0,0,width,height);
?78???????????//paint(g);?//如果取消該注釋,update的功能就與父類相同。
?79????????}

?80??????else?if(n==1)
?81????????{??g.clearRect(2,2,width,40);
?82????????}

?83???}

?84}

?85public?class?Example14_3?extends?java.applet.Applet?implements?ActionListener
?86{??Button?b1,b2;MyCanvas?canvas;
?87???public?void?init()
?88???{??canvas=new?MyCanvas();
?89??????b1=new?Button("全部清除");?b1.addActionListener(this);
?90??????b2=new?Button("部分清除");?b2.addActionListener(this);
?91??????add(b1);?add(b2);?add(canvas);
?92???}
?
?93???public?void?actionPerformed(ActionEvent?e)
?94???{??if(e.getSource()==b1)
?95???????{??canvas.setN(0);canvas.repaint();
?96???????}
??
?97??????if(e.getSource()==b2)
?98???????{??canvas.setN(1);canvas.repaint();
?99???????}
??
100???}

101}

102

轉載于:https://www.cnblogs.com/rookieport/archive/2005/05/27/163359.html

總結

以上是生活随笔為你收集整理的Java2实用教程(第二版)程序代码——第十四章 Component类的常用方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。