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

📄 picturescrollpane.java

📁 一个用java开发界面的程序集(jfc核心编程)
💻 JAVA
字号:
package JFCBook.Chapter5.jdk13;

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

public class PictureScrollPane extends JScrollPane {
	public PictureScrollPane(int vsbPolicy, int hsbPolicy,
								String[] imageNames, int picturesPerRow) {
		super(vsbPolicy, hsbPolicy);

		this.picturesPerRow = picturesPerRow;
		
		// Create labels for all of the pictures
		labels = new JLabel[imageNames.length];

		for (int i = 0; i < imageNames.length; i++) {
			labels[i] = new JLabel(new ImageIcon(getClass().getResource(imageNames[i])));
			labels[i].setHorizontalAlignment(SwingConstants.LEFT);
			labels[i].setVerticalAlignment(SwingConstants.TOP);		
		}

		JPanel pictures = new JPanel();
		pictures.setLayout(new GridBagLayout());
		
		GridBagConstraints c = new GridBagConstraints();
		c.gridwidth = 1;
		c.gridheight = 1;
		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(4, 4, 4, 4);
		c.fill = GridBagConstraints.BOTH;

		for (int i = 0; i < labels.length; i++) {
			pictures.add(labels[i], c);
			c.gridx++;
			if ((c.gridx % picturesPerRow) == 0) {
				c.gridx = 0;
				c.gridy++;
				rows++;
			}
		}

		// Correct row count if we haven't 
		// filled the last row
		if (c.gridx != 0) {
			rows++;
		}

		setViewportView(pictures);
		getViewport().setScrollMode(JViewport.BLIT_SCROLL_MODE);
	}

	public JScrollBar createVerticalScrollBar() {
		return new PictureScrollBar(JScrollBar.VERTICAL);
	}

	public JScrollBar createHorizontalScrollBar() {
		return new PictureScrollBar(JScrollBar.HORIZONTAL);
	}

	public void makeVisible(int index) {
		try {
			JComponent c = labels[index];
			Dimension d = c.getSize();
			labels[index].scrollRectToVisible(new Rectangle(0, 0, d.width, d.height));
		} catch (Exception e) {
			System.out.println("Illegal index: " + index);
		}
	}

	// Inner class to implement custom scrollbar functionality
	class PictureScrollBar extends JScrollBar {
		public PictureScrollBar(int orientation) {
			super(orientation);
		}

		public int getBlockIncrement(int direction) {
			int value = getValue();
			int targetValue;
			int index;
					
			if (positions == null) {
				// Cache the image positions
				getImagePositions();
			}

			for (index = 0; index < positions.length; index++) {
				if (value <= positions[index]) {
					break;
				}
			}

			if (direction > 0) {
				if (index < positions.length - 1) {
					index++;
				} else if (index == positions.length) {
					// Already as far right as we can go
					return 0;
				}
			} else {
				if (index > 0) {
					index--;
				}
			}
			
			targetValue = positions[index];		// Where we need to scroll to
			
			// Always return positive value
			return (targetValue - value) * direction;
		}

		public int getUnitIncrement(int direction) {
			return 1;
		}

		// Helper to cache the positions of the images
		private void getImagePositions() {
			if (getOrientation() == JScrollBar.HORIZONTAL) {
				positions = new int[picturesPerRow];			
				for (int i = 0; i < picturesPerRow; i++) {
					if (labels[i] != null) {
						positions[i] = labels[i].getLocation().x;
					}
				}
			} else {
				positions = new int[rows];					
				for (int i = 0; i < rows; i++) {
					if (labels[i * picturesPerRow] != null) {
						positions[i] = labels[i * picturesPerRow].getLocation().y;
					}
				}
			}			
		}

		private int[] positions;
	}

	private JLabel[] labels;		// Pictures, one per JLabel
	private int picturesPerRow;		// Pictures in each row
	private int rows;				// Number of rows
}

⌨️ 快捷键说明

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