📄 pnviewer2.java
字号:
// PNViewer2.java
// Planetary Nebulae Viewer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PNViewer2 extends JFrame
{
private String [] imageFileNames =
{
"mycn18.jpg",
"ngc2022.jpg",
"ngc2867.jpg",
"ngc3918.jpg",
"ngc5307.jpg",
"ngc6369.jpg",
"ngc7009.jpg",
"ngc7027.jpg"
};
private String [] imageTitleNames =
{
"Hourglass Nebula",
"NGC 2022",
"NGC 2867",
"NGC 3918",
"NGC 5307",
"NGC 6369",
"NGC 7009",
"NGC 7027"
};
private ImageIcon [] icons = new ImageIcon [imageFileNames.length];
private JLabel image;
private final Dimension imageSize = new Dimension (420, 420);
public PNViewer2 (String title)
{
// Pass the title to the superclass so it will appear in the
// frame window.
super (title);
// Swing obviates the need to register a window listener to
// respond to window closing events, by providing the
// following method call.
setDefaultCloseOperation (EXIT_ON_CLOSE);
// Pre-load all images.
for (int i = 0; i < imageFileNames.length; i++)
icons [i] = new ImageIcon (imageFileNames [i]);
// Create a JLabel component that displays an image. Initialize
// this component to display the first nebula image.
image = new JLabel (icons [0]);
// Establish a 10-pixel empty border area around the image.
image.setBorder (BorderFactory.createEmptyBorder (10, 10, 10, 10));
// Establish the image's preferred size. This size takes the
// border into account. When the layout manager lays out this
// component, it will take this size into account.
image.setPreferredSize (imageSize);
// Add JLabel component to North region of frame window's
// content pane. At this point, image will be visible on
// screen.
getContentPane ().add (image, BorderLayout.NORTH);
// Create a JPanel for holding JLabel and JComboBox components.
JPanel p = new JPanel ();
// Create JLabel component and initialize it with descriptive
// information.
JLabel l = new JLabel ("Planetary Nebula:");
// Add JLabel component to JPanel.
p.add (l);
// Create a JComboBox component to hold textual items.
JComboBox c = new JComboBox (imageTitleNames);
// When the user selects an item, an action event will be fired.
// Register an object created from an anonymous inner class that
// implements the ActionListener interface, to listen for these
// events. Once an event occurs, the image is updated.
c.addActionListener (new ActionListener ()
{
public void actionPerformed
(ActionEvent e)
{
// Retrieve reference to JComboBox
// object that fired event.
JComboBox cb = (JComboBox)
e.getSource ();
// Display new image.
int i = cb.getSelectedIndex ();
image.setIcon (icons [i]);
}
});
// Add JComboBox component to JPanel.
p.add (c);
// Add JPanel component to South region of frame window's
// content pane. At this point, JLabel/JComboBox will be visible
// on screen.
getContentPane ().add (p, BorderLayout.SOUTH);
// Size this frame window to fit the preferred size and layouts
// of its components.
pack ();
// Do not allow users to resize the window.
setResizable (false);
// Ensure that window is visible.
setVisible (true);
}
public static void main (String [] args)
{
new PNViewer2 ("Planetary Nebulae Viewer");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -