zoukankan      html  css  js  c++  java
  • Java入门【01】运行Java程序

    使用的教材:Java核心技术

    Java核心技术第一章主要讲述Java的特性以及Java的发展史

    第二章搭建开发环境,由于书比较老,对于现在也不是很适合,需要安装可以去百度搜索搭建Java环境的方法,现在的计算机越来越强大,环境目录都基本不用配置,应该说是傻瓜式操作。

    然后就是介绍使用命令行和集成开发环境的使用,并给出代码运行。

    命令行窗口并不能引起人的兴奋。所以又给出一个图形化应用程序—–图片查看器。

    代码如下:

     1 import javax.swing.*;
     2 import java.awt.*;
     3 import java.awt.event.ActionEvent;
     4 import java.awt.event.ActionListener;
     5 import java.io.File;
     6  
     7 public class ImageViewer {
     8     public static void main(String[] args) {
     9         EventQueue.invokeLater(new Runnable() {
    10             @Override
    11             public void run() {
    12                 JFrame frame = new ImageViewerFrame();
    13                 frame.setTitle("ImageViewer");
    14                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    15                 frame.setVisible(true);
    16             }
    17         });
    18     }
    19 }
    20 class ImageViewerFrame extends JFrame{
    21     private JLabel label;
    22     private JFileChooser chooser;
    23     private static final int DEFAULT_WIDTH = 300;
    24     private static final int DEFAULT_HEIGHT = 400;
    25  
    26     public ImageViewerFrame() throws HeadlessException {
    27         setSize(DEFAULT_WIDTH,DEFAULT_WIDTH);
    28         //use a lable to display the images
    29         label = new JLabel();
    30         add(label);
    31  
    32         //set up the file chooser
    33         chooser = new JFileChooser();
    34         chooser.setCurrentDirectory(new File("."));
    35  
    36         //set up the menu bar
    37         JMenuBar menuBar = new JMenuBar();
    38         setJMenuBar(menuBar);
    39  
    40         JMenu menu = new JMenu("File");
    41         menuBar.add(menu);
    42  
    43         JMenuItem openItem = new JMenuItem("open");
    44         menu.add(openItem);
    45  
    46         openItem.addActionListener(new ActionListener() {
    47             @Override
    48             public void actionPerformed(ActionEvent actionEvent) {
    49                 //show file chooser dialog
    50                 int result = chooser.showOpenDialog(null);
    51  
    52                 //if file selected,set it as icon of the label
    53                 if (result == JFileChooser.APPROVE_OPTION){
    54                     String name = chooser.getSelectedFile().getPath();
    55  
    56                     label.setIcon(new ImageIcon(name));
    57                 }
    58             }
    59         });
    60         JMenuItem exitItem = new JMenuItem("Exit");
    61         menu.add(exitItem);
    62         exitItem.addActionListener(new ActionListener() {
    63             @Override
    64             public void actionPerformed(ActionEvent actionEvent) {
    65                 System.exit(0);
    66             }
    67         });
    68  
    69     }
    70 }
    View Code

    代码看不懂无所谓,先敲上去,运行之后,感受一番,相信会有更大的动力。

    然后还演示了Applet的使用,很遗憾,这个API已经被淘汰掉了

    还是把代码放上来:WelcomeApplet.java

     1  
     2 import java.awt.*;
     3 import java.awt.event.*;
     4 import java.net.*;
     5 import javax.swing.*;
     6  
     7 public class WelcomeApplet extends JApplet{
     8     public void init(){
     9         EventQueue.invokeLater(new Runnable() {
    10             @Override
    11             public void run() {
    12                 setLayout(new BorderLayout());
    13                 JLabel label = new JLabel(getParameter("geeting"),SwingConstants.CENTER);
    14                 label.setFont(new Font("Serif",Font.BOLD,18));
    15                 add(label,BorderLayout.CENTER);
    16                 JPanel panel = new JPanel();
    17  
    18                 JButton cayButton = new JButton("Cay Horstman");
    19                 cayButton.addActionListener(makeAction("http://horstman.com"));
    20                 panel.add(cayButton);
    21  
    22                 JButton garyButton = new JButton("Gray Cornell");
    23                 garyButton.addActionListener(makeAction("mailto:gray_cornell@apress.com"));
    24                 panel.add(garyButton);
    25  
    26                 add(panel,BorderLayout.SOUTH);
    27             }
    28         });
    29     }
    30     private ActionListener makeAction(final String urlString){
    31         return new ActionListener() {
    32             @Override
    33             public void actionPerformed(ActionEvent actionEvent) {
    34                 try {
    35                     getAppletContext().showDocument(new URL(urlString));
    36                 }catch (MalformedURLException e){
    37                     e.printStackTrace();
    38                 }
    39             }
    40         };
    41     }
    42 }
    View Code

    WelcomeApplet.html

     1 <html>
     2 <head>
     3     <meta charset="UTF-8">
     4     <title>WelcomeApplet</title>
     5 </head>
     6 <body>
     7     <hr/>
     8     <p>
     9         This applet is from the book
    10         <a href="http://www.horstman.com/corejava.html">Core Java</a>
    11         by <em>Cay Horstman</em> and <em>Gary Cornel</em>
    12     </p>
    13     <applet code="WelcomeApplet.class" width="400" height="200">
    14         <param name="greeting" value="Welcome to Core Java!">
    15     </applet>
    16     <hr/>
    17     <p><a href="WelcomeApplet.java">The source.</a></p>
    18  
    19 </body>
    20 </html>
    View Code
    
    
    
    
  • 相关阅读:
    jquery 总体架构
    字符串字典排序
    浏览器渲染原理
    TCP/IP协议网络模型
    web
    js 继承
    js 模块化
    动画 球
    css z-index
    验证用户是否已经登录和自动登录
  • 原文地址:https://www.cnblogs.com/smk110/p/12907380.html
Copyright © 2011-2022 走看看