Skip to content

Commit fa8d1a5

Browse files
committed
corejava第7版
1 parent e89542a commit fa8d1a5

File tree

493 files changed

+105995
-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.

493 files changed

+105995
-0
lines changed

corejava第7版/v1/com/horstmann/Format.java

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

corejava第7版/v1/gutenberg/alice30.txt

Lines changed: 3852 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
@version 1.31 2004-05-07
3+
@author Cay Horstmann
4+
*/
5+
6+
import java.awt.*;
7+
import java.awt.event.*;
8+
import java.applet.*;
9+
import java.io.*;
10+
import java.net.*;
11+
import java.util.*;
12+
import javax.swing.*;
13+
14+
public class AppletFrame extends JFrame
15+
implements AppletStub, AppletContext
16+
{
17+
public AppletFrame(Applet anApplet)
18+
{
19+
applet = anApplet;
20+
add(applet);
21+
applet.setStub(this);
22+
}
23+
24+
public void setVisible(boolean b)
25+
{
26+
if (b)
27+
{
28+
applet.init();
29+
super.setVisible(true);
30+
applet.start();
31+
}
32+
else
33+
{
34+
applet.stop();
35+
super.setVisible(false);
36+
applet.destroy();
37+
}
38+
}
39+
40+
// AppletStub methods
41+
public boolean isActive() { return true; }
42+
public URL getDocumentBase() { return null; }
43+
public URL getCodeBase() { return null; }
44+
public String getParameter(String name) { return ""; }
45+
public AppletContext getAppletContext() { return this; }
46+
public void appletResize(int width, int height) {}
47+
48+
// AppletContext methods
49+
public AudioClip getAudioClip(URL url) { return null; }
50+
public Image getImage(URL url) { return null; }
51+
public Applet getApplet(String name) { return null; }
52+
public Enumeration getApplets() { return null; }
53+
public void showDocument(URL url) {}
54+
public void showDocument(URL url, String target) {}
55+
public void showStatus(String status) {}
56+
public void setStream(String key, InputStream stream) {}
57+
public InputStream getStream(String key) { return null; }
58+
public Iterator getStreamKeys() { return null; }
59+
60+
private Applet applet;
61+
}
62+
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/**
2+
@version 1.31 2004-05-07
3+
@author Cay Horstmann
4+
*/
5+
6+
import java.awt.*;
7+
import java.awt.event.*;
8+
import javax.swing.*;
9+
10+
public class CalculatorApplet extends JApplet
11+
{
12+
public void init()
13+
{
14+
add(new CalculatorPanel());
15+
}
16+
}
17+
18+
/**
19+
A panel with calculator buttons and a result display.
20+
*/
21+
class CalculatorPanel extends JPanel
22+
{
23+
public CalculatorPanel()
24+
{
25+
setLayout(new BorderLayout());
26+
27+
result = 0;
28+
lastCommand = "=";
29+
start = true;
30+
31+
// add the display
32+
33+
display = new JLabel("0");
34+
add(display, BorderLayout.NORTH);
35+
36+
ActionListener insert = new InsertAction();
37+
ActionListener command = new CommandAction();
38+
39+
// add the buttons in a 4 x 4 grid
40+
41+
panel = new JPanel();
42+
panel.setLayout(new GridLayout(4, 4));
43+
44+
addButton("7", insert);
45+
addButton("8", insert);
46+
addButton("9", insert);
47+
addButton("/", command);
48+
49+
addButton("4", insert);
50+
addButton("5", insert);
51+
addButton("6", insert);
52+
addButton("*", command);
53+
54+
addButton("1", insert);
55+
addButton("2", insert);
56+
addButton("3", insert);
57+
addButton("-", command);
58+
59+
addButton("0", insert);
60+
addButton(".", insert);
61+
addButton("=", command);
62+
addButton("+", command);
63+
64+
add(panel, BorderLayout.CENTER);
65+
}
66+
67+
/**
68+
Adds a button to the center panel.
69+
@param label the button label
70+
@param listener the button listener
71+
*/
72+
private void addButton(String label, ActionListener listener)
73+
{
74+
JButton button = new JButton(label);
75+
button.addActionListener(listener);
76+
panel.add(button);
77+
}
78+
79+
/**
80+
This action inserts the button action string to the
81+
end of the display text.
82+
*/
83+
private class InsertAction implements ActionListener
84+
{
85+
public void actionPerformed(ActionEvent event)
86+
{
87+
String input = event.getActionCommand();
88+
if (start)
89+
{
90+
display.setText("");
91+
start = false;
92+
}
93+
display.setText(display.getText() + input);
94+
}
95+
}
96+
97+
/**
98+
This action executes the command that the button
99+
action string denotes.
100+
*/
101+
private class CommandAction implements ActionListener
102+
{
103+
public void actionPerformed(ActionEvent event)
104+
{
105+
String command = event.getActionCommand();
106+
107+
if (start)
108+
{
109+
if (command.equals("-"))
110+
{
111+
display.setText(command);
112+
start = false;
113+
}
114+
else
115+
lastCommand = command;
116+
}
117+
else
118+
{
119+
calculate(Double.parseDouble(display.getText()));
120+
lastCommand = command;
121+
start = true;
122+
}
123+
}
124+
}
125+
126+
/**
127+
Carries out the pending calculation.
128+
@param x the value to be accumulated with the prior result.
129+
*/
130+
public void calculate(double x)
131+
{
132+
if (lastCommand.equals("+")) result += x;
133+
else if (lastCommand.equals("-")) result -= x;
134+
else if (lastCommand.equals("*")) result *= x;
135+
else if (lastCommand.equals("/")) result /= x;
136+
else if (lastCommand.equals("=")) result = x;
137+
display.setText("" + result);
138+
}
139+
140+
private JLabel display;
141+
private JPanel panel;
142+
private double result;
143+
private String lastCommand;
144+
private boolean start;
145+
}
146+
147+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<applet code="CalculatorAppletApplication.class"
2+
width="180" height="180">
3+
</applet>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
@version 1.31 2004-05-07
3+
@author Cay Horstmann
4+
*/
5+
6+
/*
7+
The applet viewer reads the tags below if you call it with
8+
appletviewer CalculatorAppletApplication.java (!)
9+
No separate HTML file is required.
10+
<applet code="CalculatorAppletApplication.class" width="200" height="200">
11+
</applet>
12+
*/
13+
14+
import javax.swing.*;
15+
16+
public class CalculatorAppletApplication
17+
extends CalculatorApplet
18+
// It's an applet. It's an application. It's BOTH!
19+
{
20+
public static void main(String[] args)
21+
{
22+
AppletFrame frame = new AppletFrame(new CalculatorApplet());
23+
frame.setTitle("CalculatorAppletApplication");
24+
frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
25+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
26+
frame.setVisible(true);
27+
}
28+
29+
public static final int DEFAULT_WIDTH = 200;
30+
public static final int DEFAULT_HEIGHT = 200;
31+
}
32+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
<head>
3+
<title>Bookmark Applet</title>
4+
</head>
5+
<frameset cols="320,*">
6+
<frame name="left" src="Left.html"
7+
marginheight="2" marginwidth="2"
8+
scrolling="no" noresize="noresize"/>
9+
<frame name="right" src="Right.html"
10+
marginheight="2" marginwidth="2"
11+
scrolling="yes" noresize="noresize"/>
12+
</frameset>
13+
</html>
14+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
@version 1.22 2004-05-07
3+
@author Cay Horstmann
4+
*/
5+
6+
import java.awt.*;
7+
import java.awt.event.*;
8+
import java.applet.*;
9+
import java.util.*;
10+
import java.net.*;
11+
import javax.swing.*;
12+
13+
public class Bookmark extends JApplet
14+
{
15+
public void init()
16+
{
17+
Box box = Box.createVerticalBox();
18+
ButtonGroup group = new ButtonGroup();
19+
20+
int i = 1;
21+
String urlString;
22+
23+
// read all link.n parameters
24+
while ((urlString = getParameter("link." + i)) != null)
25+
{
26+
27+
try
28+
{
29+
final URL url = new URL(urlString);
30+
31+
// make a radio button for each link
32+
JRadioButton button = new JRadioButton(urlString);
33+
box.add(button);
34+
group.add(button);
35+
36+
// selecting the radio button shows the URL in the "right" frame
37+
button.addActionListener(new
38+
ActionListener()
39+
{
40+
public void actionPerformed(ActionEvent event)
41+
{
42+
AppletContext context = getAppletContext();
43+
context.showDocument(url, "right");
44+
}
45+
});
46+
}
47+
catch (MalformedURLException e)
48+
{
49+
e.printStackTrace();
50+
}
51+
52+
i++;
53+
}
54+
55+
add(box);
56+
}
57+
}
58+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<html>
2+
<head><title>A Bookmark Applet</title></head>
3+
<body>
4+
<p>
5+
Click on one of the radio buttons.
6+
The corresponding web page
7+
will be displayed in the frame on the right.
8+
</p>
9+
<applet code="Bookmark.class" width="290" height="300">
10+
<param name="link.1" value="http://java.sun.com"/>
11+
<param name="link.2" value="http://java.net"/>
12+
<param name="link.3" value="http://linuxtoday.com"/>
13+
<param name="link.4" value="http://www.horstmann.com"/>
14+
<param name="link.5" value="http://www.phptr.com"/>
15+
<param name="link.6" value="http://usps.com"/>
16+
<param name="link.7" value="http://www.cafeaulait.org"/>
17+
</applet>
18+
</body>
19+
</html>
20+
21+
22+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<head><title>Web pages will be displayed here.</title></head>
3+
<body>
4+
<p>Click on one of the radio buttons to the left.
5+
The corresponding web page will be displayed here.</p>
6+
</body>
7+
</html>

0 commit comments

Comments
 (0)