Skip to content

Commit d487320

Browse files
committed
增加《Core Java(第9版)》原书代码
1 parent 100b017 commit d487320

File tree

727 files changed

+98052
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

727 files changed

+98052
-0
lines changed

CoreJava/corejava.zip

3.29 MB
Binary file not shown.

CoreJava/gutenberg/alice30.txt

Lines changed: 3852 additions & 0 deletions
Large diffs are not rendered by default.

CoreJava/gutenberg/crsto10.txt

Lines changed: 62040 additions & 0 deletions
Large diffs are not rendered by default.
44.7 KB
Loading
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import java.awt.EventQueue;
2+
import java.awt.event.*;
3+
import java.io.*;
4+
import javax.swing.*;
5+
6+
/**
7+
* A program for viewing images.
8+
* @version 1.22 2007-05-21
9+
* @author Cay Horstmann
10+
*/
11+
public class ImageViewer
12+
{
13+
public static void main(String[] args)
14+
{
15+
EventQueue.invokeLater(new Runnable()
16+
{
17+
public void run()
18+
{
19+
JFrame frame = new ImageViewerFrame();
20+
frame.setTitle("ImageViewer");
21+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
22+
frame.setVisible(true);
23+
}
24+
});
25+
}
26+
}
27+
28+
/**
29+
* A frame with a label to show an image.
30+
*/
31+
class ImageViewerFrame extends JFrame
32+
{
33+
private JLabel label;
34+
private JFileChooser chooser;
35+
private static final int DEFAULT_WIDTH = 300;
36+
private static final int DEFAULT_HEIGHT = 400;
37+
38+
public ImageViewerFrame()
39+
{
40+
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
41+
42+
// use a label to display the images
43+
label = new JLabel();
44+
add(label);
45+
46+
// set up the file chooser
47+
chooser = new JFileChooser();
48+
chooser.setCurrentDirectory(new File("."));
49+
50+
// set up the menu bar
51+
JMenuBar menuBar = new JMenuBar();
52+
setJMenuBar(menuBar);
53+
54+
JMenu menu = new JMenu("File");
55+
menuBar.add(menu);
56+
57+
JMenuItem openItem = new JMenuItem("Open");
58+
menu.add(openItem);
59+
openItem.addActionListener(new ActionListener()
60+
{
61+
public void actionPerformed(ActionEvent event)
62+
{
63+
// show file chooser dialog
64+
int result = chooser.showOpenDialog(null);
65+
66+
// if file selected, set it as icon of the label
67+
if (result == JFileChooser.APPROVE_OPTION)
68+
{
69+
String name = chooser.getSelectedFile().getPath();
70+
label.setIcon(new ImageIcon(name));
71+
}
72+
}
73+
});
74+
75+
JMenuItem exitItem = new JMenuItem("Exit");
76+
menu.add(exitItem);
77+
exitItem.addActionListener(new ActionListener()
78+
{
79+
public void actionPerformed(ActionEvent event)
80+
{
81+
System.exit(0);
82+
}
83+
});
84+
}
85+
}
19.7 KB
Loading
21.5 KB
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* This program displays a greeting from the authors.
3+
* @version 1.20 2004-02-28
4+
* @author Cay Horstmann
5+
*/
6+
public class Welcome
7+
{
8+
public static void main(String[] args)
9+
{
10+
String[] greeting = new String[3];
11+
greeting[0] = "Welcome to Core Java";
12+
greeting[1] = "by Cay Horstmann";
13+
greeting[2] = "and Gary Cornell";
14+
15+
for (String g : greeting)
16+
System.out.println(g);
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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="WelcomeApplet.class" width="400" height="200">
13+
<param name="greeting" value ="Welcome to Core Java!"/>
14+
</applet>
15+
<hr/>
16+
<p><a href="WelcomeApplet.java">The source.</a></p>
17+
</body>
18+
</html>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.awt.*;
2+
import java.awt.event.*;
3+
import java.net.*;
4+
import javax.swing.*;
5+
6+
/**
7+
* This applet displays a greeting from the authors.
8+
* @version 1.22 2007-04-08
9+
* @author Cay Horstmann
10+
*/
11+
public class WelcomeApplet extends JApplet
12+
{
13+
public void init()
14+
{
15+
EventQueue.invokeLater(new Runnable()
16+
{
17+
public void run()
18+
{
19+
setLayout(new BorderLayout());
20+
21+
JLabel label = new JLabel(getParameter("greeting"), SwingConstants.CENTER);
22+
label.setFont(new Font("Serif", Font.BOLD, 18));
23+
add(label, BorderLayout.CENTER);
24+
25+
JPanel panel = new JPanel();
26+
27+
JButton cayButton = new JButton("Cay Horstmann");
28+
cayButton.addActionListener(makeAction("http://www.horstmann.com"));
29+
panel.add(cayButton);
30+
31+
JButton garyButton = new JButton("Gary Cornell");
32+
garyButton.addActionListener(makeAction("mailto:gary_cornell@apress.com"));
33+
panel.add(garyButton);
34+
35+
add(panel, BorderLayout.SOUTH);
36+
}
37+
});
38+
}
39+
40+
private ActionListener makeAction(final String urlString)
41+
{
42+
return new ActionListener()
43+
{
44+
public void actionPerformed(ActionEvent event)
45+
{
46+
try
47+
{
48+
getAppletContext().showDocument(new URL(urlString));
49+
}
50+
catch (MalformedURLException e)
51+
{
52+
e.printStackTrace();
53+
}
54+
}
55+
};
56+
}
57+
}

0 commit comments

Comments
 (0)