文档详情

Java程序设计例子源代码.doc

发布:2017-05-03约22.38万字共221页下载文档
文本预览下载声明
Java 2程序设计 建议使用文档结构图 (选择Word菜单→视图→文档结构图) Java程序设计概述 例子1:第一个Java程序 public class H ello { 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=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 void paint(Graphics g) { g.setColor(Color.red); g.drawString(我一边喝着咖啡,一边学Java呢,5,30); g.setColor(Color.blue); g.drawString(我学得很认真,10,50); } } // AppletTest.html html head titleApplet test example/title /head body h1This is a applet example/h1 hr applet code= AppletTest.class width=800 height=600 /applet hr a href= AppletTest.javaThe source/a. /body /html 例子4:Java图形用户界面例子 //JavaGUI.java import java.awt.FlowLayout; import java.awt.event.*; import javax.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 JTextField(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); setSize(400, 130); input.addKeyListener(
显示全部
相似文档