当前位置:网站首页>【SWT组件】内容滚动组件 ScrolledComposite
【SWT组件】内容滚动组件 ScrolledComposite
2022-07-05 10:13:00 【小子宝丁】
目的
记录 ScrolledComposite
使用方法,总结重要。
实践
ScrolledComposite 类注解出给出了一个Demo,其代码如下:
package com.xzbd.swt01.test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class CompositeTest {
public static void main (String [] args) {
Display display = new Display ();
Color red = display.getSystemColor(SWT.COLOR_RED);
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
// set the size of the scrolled content - method 1
final ScrolledComposite sc1 = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
final Composite c1 = new Composite(sc1, SWT.NONE);
sc1.setContent(c1);
c1.setBackground(red);
GridLayout layout = new GridLayout();
layout.numColumns = 1;
c1.setLayout(layout);
Button b1 = new Button (c1, SWT.PUSH);
b1.setText("first button");
c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
// set the minimum width and height of the scrolled content - method 2
final ScrolledComposite sc2 = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
sc2.setExpandHorizontal(true);
sc2.setExpandVertical(true);
final Composite c2 = new Composite(sc2, SWT.NONE);
sc2.setContent(c2);
c2.setBackground(blue);
layout = new GridLayout();
layout.numColumns = 1;
c2.setLayout(layout);
Button b2 = new Button (c2, SWT.PUSH);
b2.setText("first button");
sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Button add = new Button (shell, SWT.PUSH);
add.setText("add children");
final int[] index = new int[]{
0};
add.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
index[0]++;
Button button = new Button(c1, SWT.PUSH);
button.setText("button "+index[0]);
// reset size of content so children can be seen - method 1
c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
c1.layout();
button = new Button(c2, SWT.PUSH);
button.setText("button "+index[0]);
// reset the minimum width and height so children can be seen - method 2
sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
c2.layout();
}
});
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
总结
demo给出了两个内容滚动的案例,分别展示了设置内容容器大小和设置最小尺寸两种方法,其中重要的方法有:
- 实例
ScrolledComposite
时在style中指定横向或纵向可滚动SWT.H_SCROLL
、SWT.V_SCROLL
。 - 创建一个子内容并指定给
ScrolledComposite
Composite c1 = new Composite(sc1, SWT.NONE);
sc1.setContent(c1);
- 当
ScrolledComposite
的内容发生变化时要及时更新内容容器,或ScrolledComposite
本身。
// reset size of content so children can be seen - method 1
c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
c1.layout();
或
// reset the minimum width and height so children can be seen - method 2
sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
c2.layout();
边栏推荐
- TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
- How to write high-quality code?
- Swift uses userdefaults and codable to save an array of class objects or structure instances
- > Could not create task ‘:app:MyTest.main()‘. > SourceSet with name ‘main‘ not found.问题修复
- Design and Simulation of fuzzy PID control system for liquid level of double tank (matlab/simulink)
- pytorch输出tensor张量时有省略号的解决方案(将tensor完整输出)
- Error: module not found: error: can't resolve 'xxx' in 'XXXX‘
- Activity enter exit animation
- NCP1342芯片替代料PN8213 65W氮化镓充电器方案
- How do programmers live as they like?
猜你喜欢
非技術部門,如何參與 DevOps?
Design of stepping motor controller based on single chip microcomputer (forward rotation and reverse rotation indicator gear)
Swift set pickerview to white on black background
What is the most suitable book for programmers to engage in open source?
AtCoder Beginner Contest 258「ABCDEFG」
如何写出高质量的代码?
How can non-technical departments participate in Devops?
驱动制造业产业升级新思路的领域知识网络,什么来头?
Events and bubbles in the applet of "wechat applet - Basics"
ByteDance Interviewer: how to calculate the memory size occupied by a picture
随机推荐
Singleton mode encapsulates activity management class
CSDN always jumps to other positions when editing articles_ CSDN sends articles without moving the mouse
NCP1342芯片替代料PN8213 65W氮化镓充电器方案
Go项目实战—参数绑定,类型转换
微信小程序触底加载与下拉刷新的实现
Design and Simulation of fuzzy PID control system for liquid level of double tank (matlab/simulink)
Implementation of wechat applet bottom loading and pull-down refresh
[JS] array dimensionality reduction
自动化规范检查软件如何发展而来?
学习笔记6--卫星定位技术(上)
面试:Bitmap像素内存分配在堆内存还是在native中
> Could not create task ‘:app:MyTest.main()‘. > SourceSet with name ‘main‘ not found.问题修复
La vue latérale du cycle affiche cinq demi - écrans en dessous de cinq distributions moyennes
面试:List 如何根据对象的属性去重?
SAP ui5 objectpagelayout control usage sharing
Learning II of workmanager
Workmanager learning 1
In wechat applet, after jumping from one page to another, I found that the page scrolled synchronously after returning
想请教一下,十大券商有哪些?在线开户是安全么?
Tianlong Babu TLBB series - single skill group injury