[Java核心技术]学习笔记--第二章

来源:互联网 发布:东方财富ddx源码 编辑:程序博客网 时间:2024/05/22 15:44

第二章

本章主要讲了Java相关开发环境的配置,以及Java程序的编译运行等基础知识。

2.1安装Java 开发工具箱(JDK)



Java术语术语名缩写解释Java Development KitJDK编写Java程序的程序员使用的软件Java Runtime EnvironmentJRE运行Java程序的用户使用的软件Standard EditionSE用于桌面活简单的服务器的Java平台Enterprise EditionEE用于复杂的服务器应用的Java平台Micro EditionME用于手机和其他小型设备的Java平台Java2J2一个过时的术语,用于描述1998~2006年之间的Java版本Software Development KitSDK一个过时的术语,用于描述1998~2006年之间的JDKUpdateuOracle的术语,用于发布修改的BugNetBeans-Oracle的集成开发环境
















下面描述的是在Ubuntu14.10 64位下安装JDK的方法:


1.下载JDK安装文件(JDK下载):

























2. 安装JDK(参照):
※首先切换到root用户:
# sudo su
①cd到上述下载目录(假设当前在用户的~目录下,JDK安装文件下载到Downloads文件夹中):
# cd Downloads
②在/usr/local目录下,创建development文件夹
# mkdir /usr/local/development
③将文件解压到刚才创建的目录中
# tar -zxvf jdk-8u25-linux-x64.tar.gz -C /usr/local/development/
创建文件/etc/profile.d/development.sh
# touch /etc/profile.d/development.sh
下面的内容复制到该文件。
将下面"JAVA_HOME="后面的路径,替换为之前③解压完成,生成文件夹后的路径。
export JAVA_HOME=/usr/local/development/jdk1.8.0_25export JRE_HOME=$JAVA_HOME/jreexport CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATHexport PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
⑥ 重启电脑,使用命令java -version检查JDK是否安装成功。

3.安装库文件:
①cd到jdk目录。
# cd /usr/local/development/jdk1.8.0_25
②建立一个子目录src(需要切换root用户)。
# mkdir src
③cd 到src目录中。
cd src
④解压。
# unzip ../src.zip

3.安装库文档:
①下载(下载):
②解压:
unzip jdk-8u25-docs-all.zip -d /usr/local/development/jdk1.8.0_25/
③在浏览器中设置一个指向docs/api/index.html的书签。

2.3第一个Java程序


使用vi编写如下代码:
public class Welcome{    public static void main(String[] args)    {        String[] greeting = new String[3];        greeting[0] = "Welcome to Core Java";        greeting[1] = "by Cay Horstmann";        greeting[2] = "and Gary Cornel";        for (String g : greeting)        {            System.out.println(g);        }    }}

键入下面命令:
javac Welcome.java
java Welcome

运行以后输出如下结构:


2.4使用集成开发环境

①Eclipse下载:http://eclipse.org
②将下载后的压缩文件解压到指定目录下
③进到解压后的eclipse文件夹中,运行eclipse。

2.5运行图形化应用程序

一个简单的图像文件查看器(viewer),它可以加载并显示一个图像。

Code:
import java.awt.EventQueue;import java.awt.event.*;import java.io.*;import javax.swing.*;/* * A program for viewing images. * @version 1.0 * @author XXX * */public class ImageViewer {public static void main(String[] args){EventQueue.invokeLater(new Runnable(){public void run(){JFrame frame = new ImageViewerFrame();frame.setTitle("ImageViewer");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}});}}/* * A frame with a label to show an image * */class ImageViewerFrame extends JFrame{private JLabel label;private JFileChooser chooser;private static final int DEFAULT_WIDTH = 300;private static final int DEFAULT_HEIGH = 400;public ImageViewerFrame(){setSize(DEFAULT_WIDTH, DEFAULT_HEIGH);// use a label to display the imageslabel = new JLabel();add(label);// set up the file chooserchooser = new JFileChooser();chooser.setCurrentDirectory(new File("."));// set up the menu barJMenuBar menuBar = new JMenuBar();setJMenuBar(menuBar);JMenu menu = new JMenu("File");menuBar.add(menu);JMenuItem openItem = new JMenuItem("Open");menu.add(openItem);openItem.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){// show file chooser dialogint result = chooser.showOpenDialog(null);// if file selected, set it as icon of the labelif (result == JFileChooser.APPROVE_OPTION){String name = chooser.getSelectedFile().getPath();label.setIcon(new ImageIcon(name));}}});JMenuItem exitItem = new JMenuItem("Exit");menu.add(exitItem);exitItem.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){System.exit(0);}});}}

2.6建立并运行applet

applet(百度百科)是采用Java编程语言编写的程序,该程序可以包含在 HTML(标准通用标记语言的一个应用)页中,与在页中包含图像的方式大致相同。

使用任意文本编辑器编辑如下代码:

WelcomeApplet.html
<html>        <head>                <title>WelcomeApplet</title>        </head>        <body>                <hr/>                        <p>                                This applet is from the book                                <a href= "http://www.horstmann.com/corejava.html">Core Java</a>                                by <em>Cay Horstmann</em> and <em>Gary Cornell</em>.                        </p>                        <applet code = "WelcomeApplet.class" width="400", height="200">                                <param name="greeting" value="Welcome to Core Java!"/>                        </applet>                <hr/>                <p><a href="WelcomeApplet.java">The source.</a></p>        </body></html>

WelcomeApplet.java
import java.awt.*;import java.awt.event.*;import java.net.*;import javax.swing.*;/* * This applet display a greeting from the authors. * @version * @author * */public class WelcomeApplet extends JApplet {public void init(){EventQueue.invokeLater(new Runnable(){public void run(){setLayout(new BorderLayout());JLabel label = new JLabel(getParameter("greeting"), SwingConstants.CENTER);label.setFont(new Font("Serif", Font.BOLD, 18));add(label, BorderLayout.CENTER);JPanel panel = new JPanel();JButton cayButton = new JButton("Cay Horstmann");cayButton.addActionListener(makeAction("mailto:garry_cornel@press.com"));panel.add(cayButton);JButton garyButton = new JButton("Gary Cornel");garyButton.addActionListener(makeAction("mailto:gary_cornel@press.com"));panel.add(garyButton);add(panel, BorderLayout.SOUTH);}});}private ActionListener makeAction(final String urlString){return new ActionListener(){public void actionPerformed(ActionEvent event){try{getAppletContext().showDocument(new URL(urlString));}catch (MalformedURLException e){e.printStackTrace();}}};}}

使用JDK自带的工具查看applet:
javac WelcomeApplet.java
appletviewer WelcomeApplet.html

在applet查看器中发送邮件的两个按钮不好用,因为applet查看器没有能力发送邮件或显示一个网页,因此会忽略这里的操作请求。applet查看器用于单独地测试applet,但是,最终需要将applet放到浏览器中,以便检测与浏览器以及Internet的交互情况。

如果想要在浏览器中查看applet,就需要在浏览器中配置Add-ons。

在FireFox上运行applet
①FireFox安装applet插件
#sudo ln -s /usr/lib/java/jre1.8.0_25/lib/amd64/libnpjp2.so /usr/lib/firefox/browser/plugins
②需要修改Java的Security策略。
※由于applet实在不常用,加上刚学习Java,这个就不鼓捣了(弄了半天都没弄明白)。吐舌头

第二章的学习到此结束。奋斗

0 0
原创粉丝点击