2-1 Welcome
编译与运行
javac Welcome.java
java Welcome
/**
* This program displays a greeting from the authors.
* @version 1.20 2004-02-28
* @author Cay Horstmann
*/
public class Welcome
{
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);
}
}
2-2 运行图形化应用程序
编译与运行
javac ImageViewer.java
java ImageViewer
import java.awt.EventQueue;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
/**
* A program for viewing images.
* @version 1.22 2007-05-21
* @author Cay Horstmann
*/
public class ImageViewer
{
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);
}
});
}
}
/**
* A frame with a label to show an image.
*/
class ImageViewerFrame extends JFrame
{
private JLabel label;
private JFileChooser chooser;
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 400;
public ImageViewerFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// use a label to display the images
label = new JLabel();
add(label);
// set up the file chooser
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
// set up the menu bar
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)
{
// show file chooser dialog
int result = chooser.showOpenDialog(null);
// if file selected, set it as icon of the label
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);
}
});
}
}
选择 File -> Open 即可打开文件
2-3 建立并运行applet
编译与运行
javac WelcomeApplet.java
appletviewer WelcomeApplet
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
/**
* This applet displays a greeting from the authors.
* @version 1.22 2007-04-08
* @author Cay Horstmann
*/
public class WelcomeApplet 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();
}
}
};
}
}