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

📄 displayimageapplet.java

📁 此源码为机械工业出版社出版的《Java语言程序设计》第三版所配套的书中所有源代码。
💻 JAVA
字号:
// DisplayImageApplet.java: Display an image on a panel in the applet
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
                          
public class DisplayImageApplet extends JApplet
  implements ActionListener
{
  // The panel for displaying the image  private
  private ImagePanel imagePanel = new ImagePanel();

  // The text field for entering the name of the image file
  private JTextField jtfFilename = new JTextField(20);

  // The button for displaying the image
  private JButton jbtShow = new JButton("Show Image");

  // Initialize the applet
  public void init()
  {
    // Panel p1 to hold a text field and a button
    JPanel p1 = new JPanel();
    p1.setLayout(new FlowLayout());
    p1.add(new Label("Image Filename"));
    p1.add(jtfFilename);
    p1.add(jbtShow);

    // Place an ImagePanel object and p1 in the applet
    getContentPane().add(imagePanel, BorderLayout.CENTER);
    getContentPane().add(p1, BorderLayout.SOUTH);

    // Set line border on the image panel
    imagePanel.setBorder(new LineBorder(Color.black, 1));

    // Register listener
    jbtShow.addActionListener(this);
    jtfFilename.addActionListener(this);
  }

  // Handle the ActionEvent
  public void actionPerformed(ActionEvent e)
  {
    if ((e.getSource() instanceof JButton) ||
      (e.getSource() instanceof JTextField))
      displayImage();
  }

  // Display image on the panel
  private void displayImage()
  {
    // Retrieve image
    Image image = getImage(getCodeBase(),
      jtfFilename.getText().trim());

    // Show image in the panel
    imagePanel.showImage(image);
  }
}

// Define the panel for showing an image
class ImagePanel extends JPanel
{
  // Image filename
  private String filename;

  // Image instance
  private Image image = null;

  // Default constructor
  public ImagePanel()
  {
  }

  // Set image and show it
  public void showImage(Image image)
  {
    this.image = image;
    repaint();
  }

  // Draw image on the panel
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);

    if (image != null)
      g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
  }
}

⌨️ 快捷键说明

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