Skip to content

Commit

Permalink
corejava第7版
Browse files Browse the repository at this point in the history
  • Loading branch information
yizuTomcat committed May 22, 2019
1 parent e89542a commit fa8d1a5
Show file tree
Hide file tree
Showing 493 changed files with 105,995 additions and 0 deletions.
621 changes: 621 additions & 0 deletions corejava第7版/v1/com/horstmann/Format.java

Large diffs are not rendered by default.

3,852 changes: 3,852 additions & 0 deletions corejava第7版/v1/gutenberg/alice30.txt

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions corejava第7版/v1/v1ch10/AppletApplication/AppletFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
@version 1.31 2004-05-07
@author Cay Horstmann
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;

public class AppletFrame extends JFrame
implements AppletStub, AppletContext
{
public AppletFrame(Applet anApplet)
{
applet = anApplet;
add(applet);
applet.setStub(this);
}

public void setVisible(boolean b)
{
if (b)
{
applet.init();
super.setVisible(true);
applet.start();
}
else
{
applet.stop();
super.setVisible(false);
applet.destroy();
}
}

// AppletStub methods
public boolean isActive() { return true; }
public URL getDocumentBase() { return null; }
public URL getCodeBase() { return null; }
public String getParameter(String name) { return ""; }
public AppletContext getAppletContext() { return this; }
public void appletResize(int width, int height) {}

// AppletContext methods
public AudioClip getAudioClip(URL url) { return null; }
public Image getImage(URL url) { return null; }
public Applet getApplet(String name) { return null; }
public Enumeration getApplets() { return null; }
public void showDocument(URL url) {}
public void showDocument(URL url, String target) {}
public void showStatus(String status) {}
public void setStream(String key, InputStream stream) {}
public InputStream getStream(String key) { return null; }
public Iterator getStreamKeys() { return null; }

private Applet applet;
}

147 changes: 147 additions & 0 deletions corejava第7版/v1/v1ch10/AppletApplication/CalculatorApplet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/**
@version 1.31 2004-05-07
@author Cay Horstmann
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CalculatorApplet extends JApplet
{
public void init()
{
add(new CalculatorPanel());
}
}

/**
A panel with calculator buttons and a result display.
*/
class CalculatorPanel extends JPanel
{
public CalculatorPanel()
{
setLayout(new BorderLayout());

result = 0;
lastCommand = "=";
start = true;

// add the display

display = new JLabel("0");
add(display, BorderLayout.NORTH);

ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();

// add the buttons in a 4 x 4 grid

panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));

addButton("7", insert);
addButton("8", insert);
addButton("9", insert);
addButton("/", command);

addButton("4", insert);
addButton("5", insert);
addButton("6", insert);
addButton("*", command);

addButton("1", insert);
addButton("2", insert);
addButton("3", insert);
addButton("-", command);

addButton("0", insert);
addButton(".", insert);
addButton("=", command);
addButton("+", command);

add(panel, BorderLayout.CENTER);
}

/**
Adds a button to the center panel.
@param label the button label
@param listener the button listener
*/
private void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}

/**
This action inserts the button action string to the
end of the display text.
*/
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
}

/**
This action executes the command that the button
action string denotes.
*/
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();

if (start)
{
if (command.equals("-"))
{
display.setText(command);
start = false;
}
else
lastCommand = command;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
start = true;
}
}
}

/**
Carries out the pending calculation.
@param x the value to be accumulated with the prior result.
*/
public void calculate(double x)
{
if (lastCommand.equals("+")) result += x;
else if (lastCommand.equals("-")) result -= x;
else if (lastCommand.equals("*")) result *= x;
else if (lastCommand.equals("/")) result /= x;
else if (lastCommand.equals("=")) result = x;
display.setText("" + result);
}

private JLabel display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<applet code="CalculatorAppletApplication.class"
width="180" height="180">
</applet>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
@version 1.31 2004-05-07
@author Cay Horstmann
*/

/*
The applet viewer reads the tags below if you call it with
appletviewer CalculatorAppletApplication.java (!)
No separate HTML file is required.
<applet code="CalculatorAppletApplication.class" width="200" height="200">
</applet>
*/

import javax.swing.*;

public class CalculatorAppletApplication
extends CalculatorApplet
// It's an applet. It's an application. It's BOTH!
{
public static void main(String[] args)
{
AppletFrame frame = new AppletFrame(new CalculatorApplet());
frame.setTitle("CalculatorAppletApplication");
frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static final int DEFAULT_WIDTH = 200;
public static final int DEFAULT_HEIGHT = 200;
}

14 changes: 14 additions & 0 deletions corejava第7版/v1/v1ch10/Bookmark/Bookmark.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html>
<head>
<title>Bookmark Applet</title>
</head>
<frameset cols="320,*">
<frame name="left" src="Left.html"
marginheight="2" marginwidth="2"
scrolling="no" noresize="noresize"/>
<frame name="right" src="Right.html"
marginheight="2" marginwidth="2"
scrolling="yes" noresize="noresize"/>
</frameset>
</html>

58 changes: 58 additions & 0 deletions corejava第7版/v1/v1ch10/Bookmark/Bookmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
@version 1.22 2004-05-07
@author Cay Horstmann
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.net.*;
import javax.swing.*;

public class Bookmark extends JApplet
{
public void init()
{
Box box = Box.createVerticalBox();
ButtonGroup group = new ButtonGroup();

int i = 1;
String urlString;

// read all link.n parameters
while ((urlString = getParameter("link." + i)) != null)
{

try
{
final URL url = new URL(urlString);

// make a radio button for each link
JRadioButton button = new JRadioButton(urlString);
box.add(button);
group.add(button);

// selecting the radio button shows the URL in the "right" frame
button.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
AppletContext context = getAppletContext();
context.showDocument(url, "right");
}
});
}
catch (MalformedURLException e)
{
e.printStackTrace();
}

i++;
}

add(box);
}
}

22 changes: 22 additions & 0 deletions corejava第7版/v1/v1ch10/Bookmark/Left.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<html>
<head><title>A Bookmark Applet</title></head>
<body>
<p>
Click on one of the radio buttons.
The corresponding web page
will be displayed in the frame on the right.
</p>
<applet code="Bookmark.class" width="290" height="300">
<param name="link.1" value="http://java.sun.com"/>
<param name="link.2" value="http://java.net"/>
<param name="link.3" value="http://linuxtoday.com"/>
<param name="link.4" value="http://www.horstmann.com"/>
<param name="link.5" value="http://www.phptr.com"/>
<param name="link.6" value="http://usps.com"/>
<param name="link.7" value="http://www.cafeaulait.org"/>
</applet>
</body>
</html>



7 changes: 7 additions & 0 deletions corejava第7版/v1/v1ch10/Bookmark/Right.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head><title>Web pages will be displayed here.</title></head>
<body>
<p>Click on one of the radio buttons to the left.
The corresponding web page will be displayed here.</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head><title>A Calculator</title></head>
<body>
<p>Here is a calculator, just in case you can't find yours.</p>
<applet code="CalculatorApplet.class" width="180" height="180">
</applet>
</body>
</html>
20 changes: 20 additions & 0 deletions corejava第7版/v1/v1ch10/CalculatorApplet/CalculatorApplet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
@version 1.31 2004-05-07
@author Cay Horstmann
*/

import java.awt.*;
import javax.swing.*;

public class CalculatorApplet extends JApplet
{
public void init()
{
CalculatorPanel panel = new CalculatorPanel();
add(panel);
}
}




Loading

0 comments on commit fa8d1a5

Please sign in to comment.