labeldemo.java

来自「此源码为机械工业出版社出版的《Java语言程序设计》第三版所配套的书中所有源代码」· Java 代码 · 共 85 行

JAVA
85
字号
// LabelDemo.java: Use label to display images
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LabelDemo extends JFrame implements ActionListener
{
  // Declare an ImageIcon array. There are total 52 images
  private ImageIcon[] imageIcon = new ImageIcon[52];

  // The current image index
  private int currentIndex = 0;

  // Buttons for browsing images
  private JButton jbtPrior, jbtNext;

  // Label for displaying images
  private JLabel jlblImageViewer = new JLabel();

  final int TOTAL_NUMBER_OF_IMAGES = 52;

  // Main Method
  public static void main(String[] args)
  {
    LabelDemo frame = new LabelDemo();
    // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setVisible(true);
  }

  // Default Constructor
  public LabelDemo()
  {
    setTitle("Label Demo");

    // Load images into imageIcon array
    for (int i=1; i<=52; i++)
    {
      imageIcon[i-1] = new ImageIcon(
        this.getClass().getResource("images/L" + i + ".gif"));
    }

    // Show the first image
    jlblImageViewer.setIcon(imageIcon[currentIndex]);

    // Set center alignment
    jlblImageViewer.setHorizontalAlignment(SwingConstants.CENTER);
    //jlblImageViewer.setVerticalAlignment(SwingConstants.CENTER);

    // Panel jpButtons to hold two buttons for browsing images
    JPanel jpButtons = new JPanel();
    jpButtons.add(jbtPrior = new JButton());
    jbtPrior.setIcon(new ImageIcon(
      this.getClass().getResource("images/left.gif")));
    jpButtons.add(jbtNext = new JButton());
    jbtNext.setIcon(new ImageIcon(
      this.getClass().getResource("images/right.gif")));

    // Add jpButton and the label to the frame
    getContentPane().add(jlblImageViewer, BorderLayout.CENTER);
    getContentPane().add(jpButtons, BorderLayout.SOUTH);

    // Register listeners
    jbtPrior.addActionListener(this);
    jbtNext.addActionListener(this);
  }

  // Handle ActionEvent from buttons
  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() == jbtPrior)
    {
      // Make sure index is nonnegative
      if (currentIndex == 0) currentIndex = TOTAL_NUMBER_OF_IMAGES;
      currentIndex = (currentIndex - 1)%TOTAL_NUMBER_OF_IMAGES;
      jlblImageViewer.setIcon(imageIcon[currentIndex]);
    }
    else if (e.getSource() == jbtNext)
    {
      currentIndex = (currentIndex + 1)%TOTAL_NUMBER_OF_IMAGES;
      jlblImageViewer.setIcon(imageIcon[currentIndex]);
    }
  }
}

⌨️ 快捷键说明

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