【发布时间】:2023-03-18 05:11:01
【问题描述】:
当用户将鼠标放在 SWT 文本框上时,我们正在尝试显示一个悬停框。从悬停框用户将能够复制内容。这是要求。
请提供一个实现相同的示例来帮助我。或者请提供一个见解如何在编辑器中实现相同的eclipse。
我们正在使用 Eclipse 氧气 IDE,解释的功能是针对独立的 Eclipse RCP 应用程序
提前致谢!
【问题讨论】:
标签: java eclipse eclipse-plugin swt
当用户将鼠标放在 SWT 文本框上时,我们正在尝试显示一个悬停框。从悬停框用户将能够复制内容。这是要求。
请提供一个实现相同的示例来帮助我。或者请提供一个见解如何在编辑器中实现相同的eclipse。
我们正在使用 Eclipse 氧气 IDE,解释的功能是针对独立的 Eclipse RCP 应用程序
提前致谢!
【问题讨论】:
标签: java eclipse eclipse-plugin swt
请通过tutorial 浏览SWT Text 上的悬停文本。
以下代码是该教程代码的修改版本
package test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Example {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout(2, true);
shell.setLayout(gridLayout);
Label label1 = new Label(shell, SWT.NONE);
label1.setText("First Name");
Text text1 = new Text(shell, SWT.BORDER);
Label label2 = new Label(shell, SWT.NONE);
label2.setText("Last Name");
Text text2 = new Text(shell, SWT.BORDER);
shell.pack();
final HoverShell hShell = new HoverShell(shell);
text1.addListener(SWT.MouseHover, new Listener() {
@Override
public void handleEvent(Event event) {
hShell.text.setText("Enter First Name");
hShell.hoverShell.pack();
hShell.hoverShell.open();
}
});
text1.addListener(SWT.MouseExit, new Listener() {
@Override
public void handleEvent(Event event) {
hShell.hoverShell.setVisible(false);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
class HoverShell {
Shell hoverShell;
Text text;
public HoverShell(Shell shell) {
hoverShell = new Shell(shell, SWT.ON_TOP | SWT.TOOL);
hoverShell.setLayout(new FillLayout());
text = new Text(hoverShell, SWT.NONE);
text.setBackground(hoverShell.getBackground());
text.setEditable(false);
}
}
输出
【讨论】:
我知道这有点晚了,但我有另一个解决方案。您可以使用内置函数 setToolTipText 和 getToolTipText。要复制,我会插入一个按钮,该按钮使用 Java 剪贴板操作调用 getToolTipText 函数,例如 here。
【讨论】: