gpt4 book ai didi

java - 字体在新复合 Material 的初始构建期间未生效

转载 作者:行者123 更新时间:2023-12-02 07:32:23 26 4
gpt4 key购买 nike

我想要一个 Text 小部件,当用户尚未在字段中输入值时,它可以在其中显示一条消息。我扩展了复合并实质上在其中包装了一个文本字段。添加了焦点监听器以删除焦点上的消息,并在焦点丢失(如果字段为空)时替换消息。一切都按预期进行。

我遇到的问题是我希望提示在放置在文本字段中时具有不同的样式。该字体最初似乎没有被使用。一旦字段获得焦点和失去焦点,它看起来都是正确的。

例如,这是它最初加载时的样子:
Text is not styled correctly

这就是它在初始加载时的外观以及在获得和失去焦点后的外观:
Text is styled correctly

它变得有点奇怪,因为当我在一个简单的 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();
}

没有焦点和空文本:

enter image description here

带有焦点或文本:

enter image description here

关于java - 字体在新复合 Material 的初始构建期间未生效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12729349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com