Skip to content

Commit af9d779

Browse files
committed
添加corejava第11版源码
日期:2019-5-22
1 parent e199db7 commit af9d779

File tree

525 files changed

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

525 files changed

+91425
-0
lines changed

corejava第11版/gutenberg/alice30.txt

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

corejava第11版/gutenberg/crsto10.txt

Lines changed: 62040 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.io.*;
2+
import java.nio.file.*;
3+
4+
import javafx.application.*;
5+
import javafx.scene.*;
6+
import javafx.scene.control.*;
7+
import javafx.scene.control.Alert.*;
8+
import javafx.scene.image.*;
9+
import javafx.scene.layout.*;
10+
import javafx.stage.*;
11+
import javafx.stage.FileChooser.*;
12+
13+
/**
14+
* A program for viewing images.
15+
* @version 1.40 2017-12-10
16+
* @author Cay Horstmann
17+
*/
18+
public class ImageViewer extends Application
19+
{
20+
private static final int MIN_SIZE = 400;
21+
22+
public void start(Stage stage) throws IOException
23+
{
24+
BorderPane pane = new BorderPane();
25+
MenuBar bar = new MenuBar();
26+
pane.setTop(bar);
27+
Menu fileMenu = new Menu("File");
28+
bar.getMenus().add(fileMenu);
29+
MenuItem openItem = new MenuItem("Open");
30+
openItem.setOnAction(event -> load(stage, pane));
31+
MenuItem exitItem = new MenuItem("Exit");
32+
exitItem.setOnAction(event -> System.exit(0));
33+
fileMenu.getItems().addAll(openItem, exitItem);
34+
stage.setScene(new Scene(pane, MIN_SIZE, MIN_SIZE));
35+
stage.setTitle("ImageViewer");
36+
stage.show();
37+
}
38+
39+
/**
40+
* Loads an image.
41+
* @param stage the stage above which to place the file chooser
42+
* @param pane the pane into which to place the image view
43+
*/
44+
public void load(Stage stage, BorderPane pane)
45+
{
46+
FileChooser fileChooser = new FileChooser();
47+
fileChooser.getExtensionFilters().addAll(
48+
new ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"),
49+
new ExtensionFilter("All Files", "*.*"));
50+
File file = fileChooser.showOpenDialog(stage);
51+
if (file != null)
52+
{
53+
try
54+
{
55+
Path path = file.toPath();
56+
Image image = new Image(Files.newInputStream(path));
57+
pane.setCenter(new ImageView(image));
58+
}
59+
catch (IOException e)
60+
{
61+
Alert alert = new Alert(AlertType.ERROR,
62+
"Cannot open file.");
63+
alert.showAndWait();
64+
}
65+
}
66+
}
67+
}
75.6 KB
Loading
2.66 KB
Loading
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.awt.*;
2+
import java.io.*;
3+
import javax.swing.*;
4+
5+
/**
6+
* A program for viewing images.
7+
* @version 1.31 2018-04-10
8+
* @author Cay Horstmann
9+
*/
10+
public class ImageViewer
11+
{
12+
public static void main(String[] args)
13+
{
14+
EventQueue.invokeLater(() -> {
15+
var frame = new ImageViewerFrame();
16+
frame.setTitle("ImageViewer");
17+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
18+
frame.setVisible(true);
19+
});
20+
}
21+
}
22+
23+
/**
24+
* A frame with a label to show an image.
25+
*/
26+
class ImageViewerFrame extends JFrame
27+
{
28+
private static final int DEFAULT_WIDTH = 300;
29+
private static final int DEFAULT_HEIGHT = 400;
30+
31+
public ImageViewerFrame()
32+
{
33+
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
34+
35+
// use a label to display the images
36+
var label = new JLabel();
37+
add(label);
38+
39+
// set up the file chooser
40+
var chooser = new JFileChooser();
41+
chooser.setCurrentDirectory(new File("."));
42+
43+
// set up the menu bar
44+
var menuBar = new JMenuBar();
45+
setJMenuBar(menuBar);
46+
47+
var menu = new JMenu("File");
48+
menuBar.add(menu);
49+
50+
var openItem = new JMenuItem("Open");
51+
menu.add(openItem);
52+
openItem.addActionListener(event -> {
53+
// show file chooser dialog
54+
int result = chooser.showOpenDialog(null);
55+
56+
// if file selected, set it as icon of the label
57+
if (result == JFileChooser.APPROVE_OPTION)
58+
{
59+
String name = chooser.getSelectedFile().getPath();
60+
label.setIcon(new ImageIcon(name));
61+
}
62+
});
63+
64+
var exitItem = new JMenuItem("Exit");
65+
menu.add(exitItem);
66+
exitItem.addActionListener(event -> System.exit(0));
67+
}
68+
}
75.6 KB
Loading
21.5 KB
Loading
2.66 KB
Loading
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* This program displays a greeting for the reader.
3+
* @version 1.30 2014-02-27
4+
* @author Cay Horstmann
5+
*/
6+
public class Welcome
7+
{
8+
public static void main(String[] args)
9+
{
10+
String greeting = "Welcome to Core Java!";
11+
System.out.println(greeting);
12+
for (int i = 0; i < greeting.length(); i++)
13+
System.out.print("=");
14+
System.out.println();
15+
}
16+
}

0 commit comments

Comments
 (0)