黑马程序员技术交流社区
标题:
Java自学笔记01—Java尝鲜
[打印本页]
作者:
清香白莲
时间:
2015-4-1 13:01
标题:
Java自学笔记01—Java尝鲜
开始Java
命令行Java程序示例:向控制台输出消息。
public class HelloJava
{
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 Cornell";
for (String g : greeting)
System.out.println(g);
}
}
复制代码
图形化Java程序示例:加载并显示图像。
import java.awt.EventQueue;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class HelloJava
{
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);
}
});
}
}
class ImageViewerFrame extends JFrame
{
private JLabel label;
private JFileChooser chooser;
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 300;
public ImageViewerFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
label = new JLabel();
add(label);
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
JMenuBar 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)
{
int result = chooser.showOpenDialog(null);
if (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);
}
});
}
}
复制代码
Java applet示例:在网页上运行Java程序。
<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 = "HelloJava.class" width = "400" height = "200">
<param name = "greeting" value = "Welcome to Core Java!" />
</applet>
<hr/>
<p><a href = "HelloJava.java">The source.</a></p>
</body>
</html>
复制代码
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class HelloJava 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("http://www.horstmann.com"));
panel.add(cayButton);
JButton garyButton = new JButton("Gary Cornell");
garyButton.addActionListener(makeAction("mailto:gary_cornell@apress.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();
}
}
};
}
}
复制代码
作者:
清香白莲
时间:
2015-4-1 13:04
感觉只看老毕的视频还不够,争取这个月把Java核心技术卷1看完,然后去面试。
作者:
lmcci
时间:
2015-4-1 21:37
清香白莲 发表于 2015-4-1 13:04
感觉只看老毕的视频还不够,争取这个月把Java核心技术卷1看完,然后去面试。 ...
加油。:loveliness:
作者:
倒骑驴走着瞧
时间:
2015-4-1 23:22
感觉边看视频边看书还是不错的,然后多注意实践
作者:
煎饼果子
时间:
2015-4-2 12:05
共同努力!多实践
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2