布局问题:自动增长标签(SWT)

编程入门 行业动态 更新时间:2024-10-28 19:20:29
本文介绍了布局问题:自动增长标签(SWT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用GridLayout尝试使标签自动增长而不隐藏其内容的任何内容.这是一个简单的测试代码:每次按下按钮时,标签文本都会变大,但是只有在水平调整窗口大小后,我才能获得正确的布局. 有什么方法可以解决此问题而无需调整窗口大小? 我想我已经尝试了所有物业,但仍然无法正常工作,这让我发疯了!

I'm using a GridLayout trying to make a label autogrow without hiding anything of its content. Here's a simple code to test: Everytime I press the button the label text grows larger, but only after I resize the window horizontally I get the correct layout. Is there any way to fix this without having to resize the window? I think I've tried every property and I still can't get this working, it's driving me nuts!

这就是我得到的

应该是这样

import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.Shell; public class UItest { protected Shell shell; private Label label; public static void main(String[] args) { try { UItest window = new UItest(); window.open(); } catch (Exception e) { e.printStackTrace(); } } public void open() { Display display = Display.getDefault(); createContents(); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } protected void createContents() { shell = new Shell(); shell.setSize(450, 300); shell.setText("SWT Application"); shell.setLayout(new GridLayout(1, false)); Button button = new Button(shell, SWT.NONE); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent arg0) { label.setText(label.getText() + " " + label.getText()); } }); button.setText("New Button"); label = new Label(shell, SWT.WRAP); label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); label.setText("New Label"); List list = new List(shell, SWT.BORDER); list.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1)); } protected Label getLabel() { return label; } }

感谢您的时间.

已解决以下更改:

Button button = new Button(shell, SWT.NONE); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent arg0) { label.setText(label.getText() + " " + label.getText()); shell.layout(); // ADDED THIS } }); button.setText("New Button"); label = new Label(shell, SWT.WRAP); // SET HORIZONTAL GRAB ON LABEL (FIRST TRUE IN GridData CONSTRUCTOR) label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1)); label.setText("New Label");

推荐答案

我认为,问题是文本更改时布局不会失效-尝试强制重传(通过调用getShell().layout(true, true)).

I think, the problem is that the layout is not invalidated when the text changes - try to force a relayouting (by calling getShell().layout(true, true)).

更多推荐

布局问题:自动增长标签(SWT)

本文发布于:2023-11-04 12:00:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557875.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布局   标签   SWT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!