A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 清香白莲 中级黑马   /  2015-4-1 13:01  /  537 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

开始Java命令行Java程序示例:向控制台输出消息。
  1. public class HelloJava
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 String[] greeting = new String[3];
  6.                 greeting[0] = "Welcome to Core Java";
  7.                 greeting[1] = "by Cay Horstmann";
  8.                 greeting[2] = "and Gary Cornell";

  9.                 for (String g : greeting)
  10.                         System.out.println(g);
  11.         }
  12. }
复制代码

图形化Java程序示例:加载并显示图像。
  1. import java.awt.EventQueue;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import javax.swing.*;

  5. public class HelloJava
  6. {
  7.         public static void main(String[] args)
  8.         {
  9.                 EventQueue.invokeLater(new Runnable()
  10.                 {
  11.                         public void run()
  12.                         {
  13.                                 JFrame frame = new ImageViewerFrame();
  14.                                 frame.setTitle("ImageViewer");
  15.                                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16.                                 frame.setVisible(true);
  17.                         }
  18.                 });
  19.         }
  20. }

  21. class ImageViewerFrame extends JFrame
  22. {
  23.         private JLabel label;
  24.         private JFileChooser chooser;
  25.         private static final int DEFAULT_WIDTH = 300;
  26.         private static final int DEFAULT_HEIGHT = 300;
  27.        
  28.         public ImageViewerFrame()
  29.         {
  30.                 setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  31.                
  32.                 label = new JLabel();
  33.                 add(label);
  34.                
  35.                 chooser = new JFileChooser();
  36.                 chooser.setCurrentDirectory(new File("."));
  37.                
  38.                 JMenuBar menuBar = new JMenuBar();
  39.                 setJMenuBar(menuBar);
  40.                
  41.                 JMenu menu = new JMenu("File");
  42.                 menuBar.add(menu);
  43.                
  44.                 JMenuItem openItem = new JMenuItem("Open");
  45.                 menu.add(openItem);
  46.                 openItem.addActionListener(new ActionListener()
  47.                 {
  48.                         public void actionPerformed(ActionEvent event)
  49.                         {
  50.                                 int result = chooser.showOpenDialog(null);
  51.                                
  52.                                 if (result == JFileChooser.APPROVE_OPTION)
  53.                                 {
  54.                                         String name = chooser.getSelectedFile().getPath();
  55.                                         label.setIcon(new ImageIcon(name));
  56.                                 }
  57.                         }
  58.                 });
  59.                
  60.                 JMenuItem exitItem = new JMenuItem("Exit");
  61.                 menu.add(exitItem);
  62.                 exitItem.addActionListener(new ActionListener()
  63.                 {
  64.                         public void actionPerformed(ActionEvent event)
  65.                         {
  66.                                 System.exit(0);
  67.                         }
  68.                 });
  69.         }
  70. }
复制代码
Java applet示例:在网页上运行Java程序。
  1. <html>
  2.         <head>
  3.                 <title>WelcomeApplet</title>
  4.         </head>
  5.         <body>
  6.                 <hr/>
  7.                 <p>
  8.                         This applet is from the book
  9.                         <a href = "http://www.horstmann.com/corejava.html">Core Java</a>
  10.                         by <em>Cay Horstmann</em> and <em>Gary Cornell</em>.
  11.                 </p>
  12.                 <applet code = "HelloJava.class" width = "400" height = "200">
  13.                         <param name = "greeting" value = "Welcome to Core Java!" />
  14.                 </applet>
  15.                 <hr/>
  16.                 <p><a href = "HelloJava.java">The source.</a></p>
  17.         </body>
  18. </html>
复制代码
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.net.*;
  4. import javax.swing.*;

  5. public class HelloJava extends JApplet
  6. {
  7.         public void init()
  8.         {
  9.                 EventQueue.invokeLater(new Runnable()
  10.                 {
  11.                         public void run()
  12.                         {
  13.                                 setLayout(new BorderLayout());
  14.                                
  15.                                 JLabel label = new JLabel(getParameter("greeting"), SwingConstants.CENTER);
  16.                                 label.setFont(new Font("Serif", Font.BOLD, 18));
  17.                                 add(label, BorderLayout.CENTER);
  18.                                
  19.                                 JPanel panel = new JPanel();
  20.                                
  21.                                 JButton cayButton = new JButton("Cay Horstmann");
  22.                                 cayButton.addActionListener(makeAction("http://www.horstmann.com"));
  23.                                 panel.add(cayButton);
  24.                                
  25.                                 JButton garyButton = new JButton("Gary Cornell");
  26.                                 garyButton.addActionListener(makeAction("mailto:gary_cornell@apress.com"));
  27.                                 panel.add(garyButton);
  28.                                
  29.                                 add(panel, BorderLayout.SOUTH);
  30.                         }
  31.                 });
  32.         }
  33.        
  34.         private ActionListener makeAction(final String urlString)
  35.         {
  36.                 return new ActionListener()
  37.                 {
  38.                         public void actionPerformed(ActionEvent event)
  39.                         {
  40.                                 try
  41.                                 {
  42.                                         getAppletContext().showDocument(new URL(urlString));
  43.                                 }
  44.                                 catch (MalformedURLException e)
  45.                                 {
  46.                                         e.printStackTrace();
  47.                                 }
  48.                         }
  49.                 };
  50.         }
  51. }
复制代码



4 个回复

倒序浏览
感觉只看老毕的视频还不够,争取这个月把Java核心技术卷1看完,然后去面试。
回复 使用道具 举报
清香白莲 发表于 2015-4-1 13:04
感觉只看老毕的视频还不够,争取这个月把Java核心技术卷1看完,然后去面试。 ...

加油。:loveliness:
回复 使用道具 举报
感觉边看视频边看书还是不错的,然后多注意实践
回复 使用道具 举报
共同努力!多实践
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马