⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pnviewer4.java

📁 java 完全探索的随书源码
💻 JAVA
字号:
// PNViewer4.java

// Planetary Nebulae Viewer

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

public class PNViewer4 extends JApplet
{
   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;

   public void init ()
   {
      // Pre-load all images.

      for (int i = 0; i < imageFileNames.length; i++)
      {
           Image im = getImage (getDocumentBase (), imageFileNames [i]);
           icons [i] = new ImageIcon (im);
      }

      // 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 (new Dimension (420, 420));

      // 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);
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -