《Java2程序设计经典例子.doc》由会员分享,可在线阅读,更多相关《Java2程序设计经典例子.doc(221页珍藏版)》请在金锄头文库上搜索。
1、Java 2程序设计第一章 Java程序设计概述例子1:第一个Java程序public class Hello public static void main (String args ) System.out.println(你好,很高兴学习Java); 例子2:第二个java程序class People int height; String ear; void speak(String s) System.out.println(s); public class A public static void main(String args) People zhubajie; zhubajie
2、=new People(); zhubajie.height=170; zhubajie.ear=两只大耳朵; System.out.println(身高:+zhubajie.height); System.out.println(zhubajie.ear); zhubajie.speak(师傅,咱们别去西天了,改去月宫吧); 例子3:Applet程序(由AppletTest.class和AppletTest.html组成)import java.applet.*; import java.awt.*;public class AppletTest extends Applet public
3、void paint(Graphics g) g.setColor(Color.red); g.drawString(我一边喝着咖啡,一边学Java呢,5,30); g.setColor(Color.blue); g.drawString(我学得很认真,10,50); / AppletTest.html Applet test example This is a applet example The source. 例子4:Java图形用户界面例子/JavaGUI.javaimport java.awt.FlowLayout;import java.awt.event.*;import jav
4、ax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;/* * author 同步计算输入的各个数的总和与平均值 */public class JavaGUI extends JFrame private static final long serialVersionUID = 6515574844960224544L;JTextField input = new JTextField(30);JTextField output = new JText
5、Field(30);JButton close = new JButton(关闭);JButton reset = new JButton(清空);public JavaGUI() setupGUI();public void setupGUI() this.setTitle(计算总和与平均值);this.setLayout(new FlowLayout();this.add(new JLabel(数据);this.add(input);this.add(new JLabel(结果);this.add(output);this.add(close);this.add(reset);setSiz
6、e(400, 130);input.addKeyListener(new KeyAdapter() public void keyReleased(KeyEvent e) String str = input.getText();if (str.equals() | str = null) output.setText(); else String num = str.split( );int sum = 0;double avg = 0.0;for (int i = 0; i b) t=a; a=b; b=t; if(ac) t=a; a=c; c=t; if(bc) t=b; b=c; c=t; System.out.println(a=+a+,b=+b+,c=+c); 例子6:用if语句判断给定的成绩是否及格public class Score publ