动态改变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 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();

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中的pane,Java中scroll pane的使用(一)– 动态改变其client的大小 | 学步园相关推荐

  1. java下拉框及响应函数_Java 下拉框JComboBox和单选按钮JRadioButton简单举例使用 | 学步园...

    下面代码仅供参考,也可作为模板使用. package com.View; import java.awt.Color; import java.awt.GridLayout; import java. ...

  2. java定时发送短信_java实现指定时间触发一个事件(比如定时发送短信) | 学步园...

    package com.huawei.dm.datasource.system; import java.util.Calendar; import java.util.Date; import ja ...

  3. java gui 监听组合键_【CSDN常见问题解答】Swing监听组合键 | 学步园

    其实监听键盘事件和简单,键盘也就这3个事件keyTyped, keyReleased, keyPressed 下面我们就监听一个CTRL+C组合键好了. import java.awt.Color; ...

  4. java重命名sheet失败_java jxl excel 数据导出 重新命名无效的工作表名称 | 学步园...

    今天在java中使用jxl导出数据到excel工作表的时候,无论是在线直接打开还是保存到本地再打开,都会提示下面的错误信息: "Excel在'excel.xls'中发现不可读取的内容.是否恢 ...

  5. java调用dll 指针参数_java调用c dll,指针参数和结构体参数搞定 | 学步园

    终于有了比较大的成果,不过身体也累完了,昨天又去打的吊瓶,坐成铁的时候差点就晕倒在里面了,不知道什么原因,抵抗力这么差,明天放假,要在家好好休息,今天早上碰到欢欢了,她说给我做好吃的,我愿意吃她做的糖 ...

  6. java成员变量垃圾回收_JAVA 对象被垃圾回收后其成员变量可用吗? | 学步园

    我们知道JVM中一个对象实例超出作用域或无引用的时候会被垃圾回收,那么他被垃圾回收后,依赖其成员变量的其他实例是否还能正常使用呢?经过测试后发现即使一个实例被垃圾回收,如果他内部的数据被其他对象使用, ...

  7. php变量名动态生成,PHP中动态创建变量名(可变变量) | 学步园

    PHP中,我们可以动态地创建一个变量名.例如,你想要使用与数据库查询得到的字段名称一致的变量名. 使用PHP中"可变变量"的语法,即在一个其值为你想作为变量名称的变量前面加一个$: ...

  8. java 双斜杠变单斜杠_java 过滤文本特殊字符 以及单斜杠替换成双斜杠 | 学步园...

    在读取mysql数据库时,数据库中的文件路径是 由于其中包含单斜杠,我们在写入到另外的表中时,总是被莫名其妙的截断.即使将该字段的类型设置成longtext 都不行 于是,我们在读取之后使用如下方法. ...

  9. java -xmx_java默认的Xmx大小 | 学步园

    记录这个问题是因为看到CSDN上一个人问的一个问题: 为什么-Xmx的设置不管用,问题的内容大概是用top看进程资源的时候,VIRT列的内容为什么不符合自己设的值的预期(跟Xmx的值的关系). 1. ...

最新文章

  1. sparksql dataframe变成csv保存_Spark大数据分析(三):DataFrame和SQL
  2. 【蓝桥杯】基础练习 特殊回文数
  3. linux的无密码登录,linux 无密码登录
  4. npm 报错: npm ERR! Please try running this command again as root/Administrator.
  5. 第九届蓝桥杯省赛--快速排序
  6. APP长期处于后台手机打开多个APP后进程被杀
  7. Python使用多进程批量判断素数
  8. 4十4十4写成乘法算式_小学数学二年级下册数学1-4单元知识点复习提前准备才能考的更好...
  9. win7系统不能加入工作组计算机,技术员教你解决win7系统工作组计算机无法访问的修复办法...
  10. 模电笔记1 | 信号的放大与分贝计算
  11. 完全体,千字详解:“Java性能调优六大工具”之JConsole工具
  12. 在线重建索引 oracle,ORACLE重建索引详解
  13. SharePoint文件审批功能设置
  14. 37岁被裁员,大公司不愿要,无奈去小公司面试,HR的话扎心了
  15. java写triangle_Java - 制作Triangle类
  16. 电脑上的文件经常丢失,怎么办?
  17. ROS机器人操作系统教学与应用的思考
  18. Libra,一场赛博朋克噩梦!
  19. configure error /bin/bash /config sub failed
  20. 《管理学》之组织设计(进度一)

热门文章

  1. VMware 虚拟化平台规划设计方案
  2. VCSA 6.X(VMware vCenter Server Appliance)空间不足问题处理
  3. 如何配置虚拟机的快照报警
  4. Mysql学习总结(82)——MySQL逻辑删除与数据库唯一性约束如何解决?
  5. Java基础学习总结(165)——API 安全最佳实践
  6. Spring Cloud与微服务学习总结(1)——Spring Cloud及微服务入门
  7. php遍历原理,PHP数组遍历与实现原理
  8. div中内部div横向布局
  9. hackathon活动复盘
  10. java 解析/操作 xml 几种常用方式 xml的增加/删除/修改