Java中scroll pane的使用(一)-- 动态改变其client的大小
2012-12-24 18:22
260 查看
动态改变scroll pane的client的大小,并达到其滚动条出现当client超出我们设定的view port大小时。
package com.han; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.Shape; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.Ellipse2D; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.SwingUtilities; /** * This application aims to dynamically change the client's size in a scroll * pane. * * @author HAN * */ @SuppressWarnings("serial") public class ScrollDemo2 extends JPanel { /** * We can also use Vector to store a variable array. But the overhead will * be more than ArrayList. So the difference between them is thread-safe or * not. */ private ArrayList<Shape> circles; private final int CIRCLE_DIAMETER = 100; private Dimension area;// indicate area taken up by graphics. private Color[] colors = { Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.YELLOW }; ScrollDemo2() { // The constructor serves also as a content pane. This is a common // trick: invoke the constructor with parameter from a constructor // without parameter. super(new BorderLayout()); // Allocate memory for member variables if necessary. circles = new ArrayList<Shape>(); area = new Dimension(0, 0); // Create "labelPanel". JPanel labelPanel = new JPanel(new GridLayout(0, 1)); labelPanel .add(new JLabel("Click left mouse button to place a circle.")); labelPanel.add(new JLabel( "Click right mouse button to clear drawing area.")); // Create "drawingPane". final JPanel drawingPane = new JPanel() { @Override protected void paintComponent(Graphics g) { // Paint first the default behavior of the JPanel painting, // which includes fill the whole area with a background color we // set. This arrives to the effect that "clear the panel", which // can also be realized by setColor followed by fillRect. super.paintComponent(g); // g.setColor(getBackground()); // g.fillRect(0, 0, getWidth(), getHeight()); Graphics2D g2 = (Graphics2D) g; // For showing always all drawn circles even if you resize the // window, we are forced to draw all circles in the "current" // painting. for (int i = 0; i < circles.size(); i++) { g2.setColor(colors[i % colors.length]); g2.fill(circles.get(i)); } } }; drawingPane.setOpaque(true); // Set the background property, which you can get by getBackground. drawingPane.setBackground(Color.WHITE); drawingPane.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { boolean changed = false; int x = e.getX() - CIRCLE_DIAMETER / 2; int y = e.getY() - CIRCLE_DIAMETER / 2; if (x < 0) x = 0; if (y < 0) y = 0; if (SwingUtilities.isLeftMouseButton(e)) { circles.add(new Ellipse2D.Double(x, y, CIRCLE_DIAMETER, CIRCLE_DIAMETER)); // Here we can use two repaint methods: the one with // parameter and the other without. // repaint(clip) will repaint updates only in the desired // region (as clip), which can have a more efficient // performance than use directly repaint(). drawingPane.repaint(x, y, CIRCLE_DIAMETER, CIRCLE_DIAMETER); // drawingPane.repaint(); // Update the area taken up by graphics. int thisWidth = x + CIRCLE_DIAMETER; if (thisWidth > area.width) { area.width = thisWidth; changed = true; } int thisHeight = y + CIRCLE_DIAMETER; if (thisHeight > area.height) { area.height = thisHeight; changed = true; } } else if (SwingUtilities.isRightMouseButton(e)) { circles.clear(); // Because in this case we want to change the whole painting // area, use the repaint() directly. drawingPane.repaint(); area.width = 0; area.height = 0; changed = true; } if (changed) { // The setPreferredSize method is to be used in a // combination way with the revalidate() method. drawingPane.setPreferredSize(area); // Let the scroll pane know to update itself especially its // scroll bars. drawingPane.revalidate(); } } }); // Put the drawingPane to a scroll pane. JScrollPane scrollPane = new JScrollPane(drawingPane); // setPreferredSize for the first "pack()" use. scrollPane.setPreferredSize(new Dimension(200, 200)); // Lay out the content pane. add(labelPanel, BorderLayout.NORTH); add(scrollPane, BorderLayout.CENTER); } /** * For thread safety, this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { // Create and set up the window. JFrame frame = new JFrame("ScrollDemo2"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create and set up the content pane. JPanel contentPane = new ScrollDemo2(); contentPane.setOpaque(true);// Content pane must be opaque. frame.setContentPane(contentPane); // Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { // Schedule a job for the event-dispatching thread: // creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createAndShowGUI(); } }); } }
相关文章推荐
- 分享非常有用的Java程序 (关键代码)(四)---动态改变数组的大小
- Java script 动态改变 图片大小
- 使用JavaScript实现动态改变控件大小
- 关于easyUI datagrid、layout动态改变大小使用的方法
- 分享非常有用的Java程序 (关键代码)(四)---动态改变数组的大小
- java实现控件的移动及使用鼠标改变控件大小
- 使用jQuery动态改变图片显示大小
- IOS开发UI篇--使用CAShapeLayer实现一个音量大小动态改变的控件
- 使用CAShapeLayer实现一个音量大小动态改变的控件
- jQuery easyui dataGrid 动态改变排序字段名,一般情况下,在使用的时候,我们会点击相应字段进行排序,这里以JAVA为例,后端的实体类字段有可能和数据库的字段不一致; 如:实体类中的
- Java 如何动态改变改变组件大小(更随窗口) 和 根据窗口大小改变程序窗口大小f
- 【转载整理】eclipse 开发c/c++、Java使用JNI调用C程序、生成64位dll动态链接库
- java 等比例 改变图片的质量(等大小压缩图片)
- 使用动态的JNLP动态部署Java应用程序
- 移动端之动态改变rem的大小
- android样式学习(一) 使用selector改变来动态改变背景颜色
- 使用脚本引擎增加程序运行时动态执行能力(Java篇)
- Android 自定义SeekBar动态改变硬件音量大小实现和音量键的同步(推荐)
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- Java Socket以及界面动态加载的简单使用