java 子窗口 swt_设置Java SWT Shell窗口内部区域的大小

本文介绍如何在Java SWT中设置Shell窗口的Client Area大小。通过使用ShellListener事件监听器,当Shell激活时计算并调整窗口尺寸,以达到期望的内部显示区域大小。
摘要由CSDN通过智能技术生成

小编典典

从您的问题中,我了解到您想设置的尺寸Client Area。在SWT术语中,其定义为 a rectangle which describes the

area of the receiver which is capable of displaying data (that is, not covered

by the "trimmings").

您无法直接设置的尺寸,Client Area因为没有API。虽然您可以通过一点技巧来实现。在下面的示例代码中,我希望我的客户区域是 300 by

250 。为此,我使用了shell.addShellListener()事件监听器。当外壳完全处于活动状态时(请参阅参考资料 public

void shellActivated(ShellEvent e)

),我将计算出不同的边距,然后再次设置外壳的大小。外壳尺寸的计算和重置为我提供了所需的外壳尺寸。

>>Code:

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.ShellEvent;

import org.eclipse.swt.events.ShellListener;

import org.eclipse.swt.layout.GridData;

import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Menu;

import org.eclipse.swt.widgets.Shell;

public class MenuTest {

public static void main (String [] args)

{

Display display = new Display ();

final Shell shell = new Shell (display);

GridLayout layout = new GridLayout();

layout.marginHeight = 0;

layout.marginWidth = 0;

layout.horizontalSpacing = 0;

layout.verticalSpacing = 0;

layout.numColumns = 1;

shell.setLayout(layout);

shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,true));

final Menu bar = new Menu (shell, SWT.BAR);

shell.setMenuBar (bar);

shell.addShellListener(new ShellListener() {

public void shellIconified(ShellEvent e) {

}

public void shellDeiconified(ShellEvent e) {

}

public void shellDeactivated(ShellEvent e) {

}

public void shellClosed(ShellEvent e) {

System.out.println("Client Area: " + shell.getClientArea());

}

public void shellActivated(ShellEvent e) {

int frameX = shell.getSize().x - shell.getClientArea().width;

int frameY = shell.getSize().y - shell.getClientArea().height;

shell.setSize(300 + frameX, 250 + frameY);

}

});

shell.open ();

while (!shell.isDisposed()) {

if (!display.readAndDispatch ()) display.sleep ();

}

display.dispose ();

}

}

2020-11-26

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值