- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要一个 Text
小部件,当用户尚未在字段中输入值时,它可以在其中显示一条消息。我扩展了复合并实质上在其中包装了一个文本字段。添加了焦点监听器以删除焦点上的消息,并在焦点丢失(如果字段为空)时替换消息。一切都按预期进行。
我遇到的问题是我希望提示在放置在文本字段中时具有不同的样式。该字体最初似乎没有被使用。一旦字段获得焦点和失去焦点,它看起来都是正确的。
例如,这是它最初加载时的样子:
这就是它在初始加载时的外观以及在获得和失去焦点后的外观:
它变得有点奇怪,因为当我在一个简单的 shell 中运行它时,它按照它应该的方式工作。当我将它作为 Eclipse 应用程序运行时,它的样式不正确。
这是我的组合的代码:
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
/**
* The <code>PromptingTextInput</code> component is a small enhancement to
* standard <code>Text</code>. It adds the ability to specify a prompt value
* that displays when the text is empty and the field does not have focus.
*/
public class PromptingTextInput extends Composite {
private String prompt;
private Text input;
private boolean textEmpty;
Font emptyFont;
Font inputFont;
public PromptingTextInput(String prompt, Composite parent, int style, boolean passwordField) {
super(parent, style);
this.prompt = prompt;
setLayout(new FillLayout());
this.textEmpty = true;
this.input = new Text(this, (passwordField ? SWT.PASSWORD : SWT.NONE));
setEmptyInputStyle();
this.input.setText(this.prompt);
this.input.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
PromptingTextInput.this.focusGained();
}
public void focusLost(FocusEvent e) {
PromptingTextInput.this.focusLost();
}
});
addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
disposeFonts();
}
});
}
protected void focusGained() {
if (this.textEmpty) {
this.input.setText("");
setInputStyle();
}
}
protected void focusLost() {
if (input.getText() == null || input.getText().trim().length() == 0) {
this.input.setText(this.prompt);
setEmptyInputStyle();
this.textEmpty = true;
} else {
this.textEmpty = false;
}
}
protected void setInputStyle() {
if (this.inputFont == null){
this.inputFont = new Font(Display.getCurrent(), "Verdana", 8, SWT.DEFAULT);
}
this.input.setFont(this.inputFont);
this.input.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
}
protected void setEmptyInputStyle() {
if (this.emptyFont == null){
this.emptyFont = new Font(Display.getCurrent(), "Verdana", 6, SWT.ITALIC);
}
this.input.setFont(this.emptyFont);
this.input.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
}
public String getPrompt() {
return prompt;
}
public void setPrompt(String prompt) {
this.prompt = prompt;
if(!this.input.isFocusControl()){
this.input.setText(this.prompt);
setEmptyInputStyle();
}
}
public Text getInput() {
return input;
}
public boolean isTextEmpty() {
return textEmpty;
}
public String getText() {
return this.input.getText();
}
public void addModifyListener (ModifyListener listener) {
this.input.addModifyListener(listener);
}
public void disposeFonts(){
if (this.inputFont != null){
this.inputFont.dispose();
}
if (this.emptyFont != null){
this.emptyFont.dispose();
}
}
}
更新: 正如 Baz 所表明的那样,这不是 Indigo 中的问题,而似乎只是 Juno 中 E4 应用程序的问题。
最佳答案
根据 sambi 的回答,我得到了以下工作。也许这在朱诺中有效:
private static Font italic;
public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1,false));
italic = new Font(Display.getCurrent(), "Verdana", 6, SWT.ITALIC);
final Text text = new Text(shell, SWT.BORDER);
text.addListener(SWT.Paint, new Listener() {
@Override
public void handleEvent(Event event) {
if(text.getText().length() < 1 && !text.isFocusControl())
{
GC gc = event.gc;
gc.setFont(italic);
gc.setForeground(display.getSystemColor(SWT.COLOR_GRAY));
Point size = text.computeSize(SWT.DEFAULT, SWT.DEFAULT);
/* Strangely the y positioning doesn't work correctly */
//gc.drawText("Please enter text", 1, (size.y / 2) - (italic.getFontData()[0].getHeight() / 2));
gc.drawText("Please enter text", 1, 4);
}
}
});
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
Button button = new Button(shell, SWT.PUSH);
button.setText("Dummy");
button.forceFocus();
shell.setSize(200, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
italic.dispose();
display.dispose();
}
没有焦点和空文本:
带有焦点或文本:
关于java - 字体在新复合 Material 的初始构建期间未生效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12729349/
其他一切都生效但权限没有改变,我错过了什么吗? FROM joomla:3.9-php7.2-apache RUN apt-get update \ && apt-get install -y apt
我试图在我的 .htaccess 文件中指定 SSLCipherSuite,但更改似乎没有生效。我在共享主机帐户上运行,因此只能访问 .htaccess,而不能访问服务器/虚拟主机配置文件。 Apac
通过 Mailchimp 编写电子邮件模板,同时我已经让它适用于每个基于 HTML 的电子邮件客户端。 Outlook 的基于 Microsoft Word 的电子邮件客户端出现了一些问题。我关注了M
我第一次尝试创建子主题,使用二十十主题作为模板(我知道它现在有点旧,但我正在学习的教程使用它。)我的教程做的第一件事是更改主题中主要元素的背景颜色,以突出显示 DIV 所在的位置。我是这样关注的: #
我在让 Google map 样式在我的 Android 上的 Flutter 应用程序中工作时遇到问题。我正在使用 google_maps_flutter 插件 version 0.5.30。我从
在我的 WPF UserControl 中我有这个资源,请观察 Xml 示例中的注释。
当我运行线程检查工具 helgrind (valgrind toolsuite) 时,我在 glib 库中看到了很多错误。我想压制那些。 抑制文件是一种选择。但我发现:How to use helgr
我有下面的代码,我用它来创建一个 asp.net 菜单。我已将 CssClasses 分配给每个动态元素并创建了特定的类。但是,dynamicHoverStyle 类没有生效。有谁知道为什么会这样?
在Django文档的这一节中,它说明了通过url conf传递reDirect_field_name,如下所示。URL(r‘^ACCOUNTS/LOGIN/$’,‘django.contri.auth
我是一名优秀的程序员,十分优秀!