• Java GUI小程序之图片浏览器


    以下是一个简单的图片浏览器示例代码,它包含了图片放大缩小、拖拽、上一张/下一张查看等功能。你可以根据它进行扩展,提高用户体验。

    1. import java.awt.BorderLayout;
    2. import java.awt.Dimension;
    3. import java.awt.event.ActionEvent;
    4. import java.awt.event.ActionListener;
    5. import java.awt.event.MouseAdapter;
    6. import java.awt.event.MouseEvent;
    7. import java.awt.event.MouseMotionAdapter;
    8. import java.awt.image.BufferedImage;
    9. import java.io.File;
    10. import java.io.IOException;
    11. import javax.imageio.ImageIO;
    12. import javax.swing.BorderFactory;
    13. import javax.swing.ImageIcon;
    14. import javax.swing.JButton;
    15. import javax.swing.JFileChooser;
    16. import javax.swing.JFrame;
    17. import javax.swing.JLabel;
    18. import javax.swing.JOptionPane;
    19. import javax.swing.JPanel;
    20. import javax.swing.JScrollPane;
    21. import javax.swing.JSlider;
    22. import javax.swing.SwingConstants;
    23. import javax.swing.UIManager;
    24. import javax.swing.event.ChangeEvent;
    25. import javax.swing.event.ChangeListener;
    26. public class ImageViewer extends JFrame {
    27. private static final long serialVersionUID = 1L;
    28. private JLabel imageLabel;
    29. private JScrollPane scrollPane;
    30. private BufferedImage image;
    31. private Dimension imageSize;
    32. private JPanel controlPanel;
    33. private JSlider zoomSlider;
    34. private JButton previousButton;
    35. private JButton nextButton;
    36. private int currentImageIndex = 0;
    37. private String[] imagePaths = {"image1.jpg", "image2.jpg", "image3.jpg"};
    38. private int zoomLevel = 100; // Default zoom level
    39. public ImageViewer() {
    40. setTitle("Image Viewer");
    41. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    42. setPreferredSize(new Dimension(800, 600));
    43. setLayout(new BorderLayout());
    44. // Create image label and scroll pane
    45. imageLabel = new JLabel();
    46. imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
    47. scrollPane = new JScrollPane(imageLabel);
    48. add(scrollPane, BorderLayout.CENTER);
    49. // Create control panel
    50. controlPanel = new JPanel();
    51. controlPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    52. add(controlPanel, BorderLayout.SOUTH);
    53. // Create zoom slider
    54. zoomSlider = new JSlider(SwingConstants.HORIZONTAL, 50, 200, zoomLevel);
    55. zoomSlider.setMajorTickSpacing(50);
    56. zoomSlider.setMinorTickSpacing(10);
    57. zoomSlider.setPaintTicks(true);
    58. zoomSlider.setPaintLabels(true);
    59. zoomSlider.addChangeListener(new ChangeListener() {
    60. public void stateChanged(ChangeEvent e) {
    61. zoomLevel = zoomSlider.getValue();
    62. updateImage();
    63. }
    64. });
    65. controlPanel.add(zoomSlider);
    66. // Create previous button
    67. previousButton = new JButton("Previous");
    68. previousButton.addActionListener(new ActionListener() {
    69. public void actionPerformed(ActionEvent e) {
    70. if (currentImageIndex > 0) {
    71. currentImageIndex--;
    72. loadImage();
    73. }
    74. }
    75. });
    76. controlPanel.add(previousButton);
    77. // Create next button
    78. nextButton = new JButton("Next");
    79. nextButton.addActionListener(new ActionListener() {
    80. public void actionPerformed(ActionEvent e) {
    81. if (currentImageIndex < imagePaths.length - 1) {
    82. currentImageIndex++;
    83. loadImage();
    84. }
    85. }
    86. });
    87. controlPanel.add(nextButton);
    88. // Load the first image
    89. loadImage();
    90. // Add mouse listeners to enable dragging
    91. imageLabel.addMouseListener(new MouseAdapter() {
    92. public void mousePressed(MouseEvent e) {
    93. imageLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.MOVE_CURSOR));
    94. }
    95. public void mouseReleased(MouseEvent e) {
    96. imageLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    97. }
    98. });
    99. imageLabel.addMouseMotionListener(new MouseMotionAdapter() {
    100. public void mouseDragged(MouseEvent e) {
    101. int dx = e.getX() - (imageSize.width - scrollPane.getViewport().getWidth()) / 2;
    102. int dy = e.getY() - (imageSize.height - scrollPane.getViewport().getHeight()) / 2;
    103. int newHValue = scrollPane.getHorizontalScrollBar().getValue() - dx;
    104. int newVValue = scrollPane.getVerticalScrollBar().getValue() - dy;
    105. if (newHValue < 0)
    106. newHValue = 0;
    107. if (newVValue < 0)
    108. newVValue = 0;
    109. if (newHValue + scrollPane.getViewport().getWidth() > imageSize.width)
    110. newHValue = imageSize.width - scrollPane.getViewport().getWidth();
    111. if (newVValue + scrollPane.getViewport().getHeight() > imageSize.height)
    112. newVValue = imageSize.height - scrollPane.getViewport().getHeight();
    113. scrollPane.getHorizontalScrollBar().setValue(newHValue);
    114. scrollPane.getVerticalScrollBar().setValue(newVValue);
    115. }
    116. });
    117. pack();
    118. setLocationRelativeTo(null);
    119. setVisible(true);
    120. }
    121. private void loadImage() {
    122. try {
    123. image = ImageIO.read(new File(imagePaths[currentImageIndex]));
    124. imageSize = new Dimension(image.getWidth(), image.getHeight());
    125. updateImage();
    126. } catch (IOException e) {
    127. JOptionPane.showMessageDialog(this, "Error loading image: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    128. }
    129. }
    130. private void updateImage() {
    131. int newWidth = (int) (imageSize.width * zoomLevel / 100.0);
    132. int newHeight = (int) (imageSize.height * zoomLevel / 100.0);
    133. BufferedImage scaledImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
    134. scaledImage.getGraphics().drawImage(image, 0, 0, newWidth, newHeight, null);
    135. imageLabel.setIcon(new ImageIcon(scaledImage));
    136. }
    137. public static void main(String[] args) {
    138. try {
    139. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    140. } catch (Exception e) {
    141. // Ignore
    142. }
    143. new ImageViewer();
    144. }
    145. }

    运行效果如下:

  • 相关阅读:
    设计模式5——简单工厂模式
    打造千万级流量秒杀第二十六课 限流器:如何实现熔断器和限流器防止宕机和雪崩?
    C. Even Picture(构造)
    Java10新增特性
    Android-Framework 三方应用默认权限都不弹窗
    亚马逊、Shopee、速卖通卖家想要在旺季打造爆品提高销量该怎样做
    Qml中的那些坑(三)---MouseArea上的ListView滚轮事件穿透
    Linux之initd管理系统(海思、ZYNQ、复旦微)添加密码登录验证
    Stable Diffusion — ControlNet 超详细讲解
    【Java 进阶篇】JavaScript 表格全选案例详解
  • 原文地址:https://blog.csdn.net/m0_37649480/article/details/134422207