0

我想从 Window->prefernce->change font 选项更改文本字段的字体。我实现了代码。但是在更新文本字段时会发生什么,我得到了以下 classcast 异常。以下是在首选项中添加字体对话框的代码

public class SampleViewPreferencePage extends FieldEditorPreferencePage implements
        IWorkbenchPreferencePage {

    public SampleViewPreferencePage() {
        super(GRID);

    }

    public void createFieldEditors() {

        addField(new FontFieldEditor("font", "Font selection",
                getFieldEditorParent()));
    }

    @Override
    public void init(IWorkbench workbench) {
        setPreferenceStore(SvtActivator.getDefault().getPreferenceStore());

    }

}

SampleView-> createPartControl()

/**
     * This is a callback that will allow us to create the viewer and initialize
     * it.
     */
    public void createPartControl(Composite parent) {

        super.createPartControl(parent);
        FillLayout layout = new FillLayout();
        layout.marginHeight = layout.marginWidth = 10;
        parent.setLayout(layout);
        text = new StyledText(parent, SWT.V_SCROLL | SWT.H_SCROLL
                | SWT.MULTI | SWT.BORDER);
        text.setText("");




    }

@Override
    public void propertyChange(org.eclipse.jface.util.PropertyChangeEvent event) {
        System.out.println("Value changed..");

        Object newValue = event.getNewValue();
        System.out.println("newValue: "+newValue);
        //if (newValue instanceof FontData) {
            Font font = new Font(Display.getCurrent(),
                    (FontData) newValue);
            getText().setFont(font);
        //}

    }





java.lang.ClassCastException: [Lorg.eclipse.swt.graphics.FontData; cannot be cast to org.eclipse.swt.graphics.FontData
    at com.example.MyView.propertyChange(MyView.java:111)
    at org.eclipse.ui.preferences.ScopedPreferenceStore$3.run(ScopedPreferenceStore.java:375)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.ui.preferences.ScopedPreferenceStore.firePropertyChangeEvent(ScopedPreferenceStore.java:372)
    at org.eclipse.jface.preference.PreferenceConverter.setValue(PreferenceConverter.java:463)
    at org.eclipse.jface.preference.FontFieldEditor.doStore(FontFieldEditor.java:242)
    at org.eclipse.jface.preference.FieldEditor.store(FieldEditor.java:680)
    at org.eclipse.jface.preference.FieldEditorPreferencePage.performOk(FieldEditorPreferencePage.java:343)
    at org.eclipse.jface.preference.PreferencePage.performApply(PreferencePage.java:432)
    at org.eclipse.jface.preference.PreferencePage$2.widgetSelected(PreferencePage.java:281)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
    at org.eclipse.jface.window.Window.runEventLoop(Window.java:825)
    at org.eclipse.jface.window.Window.open(Window.java:801)
    at org.eclipse.ui.internal.dialogs.WorkbenchPreferenceDialog.open(WorkbenchPreferenceDialog.java:215)
    at org.eclipse.ui.internal.handlers.ShowPreferencePageHandler.execute(ShowPreferencePageHandler.java:54)
    at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:290)
    at org.eclipse.core.commands.Command.executeWithChecks(Command.java:499)
    at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508)
    at org.eclipse.ui.internal.handlers.HandlerService.executeCommand(HandlerService.java:169)
    at org.eclipse.ui.internal.handlers.SlaveHandlerService.executeCommand(SlaveHandlerService.java:241)
    at org.eclipse.ui.menus.CommandContributionItem.handleWidgetSelection(CommandContributionItem.java:829)
    at org.eclipse.ui.menus.CommandContributionItem.access$19(CommandContributionItem.java:815)
    at org.eclipse.ui.menus.CommandContributionItem$5.handleEvent(CommandContributionItem.java:805)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
    at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2701)
    at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2665)
    at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2499)
    at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:679)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:668)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at de.allianz.abs.konvertortool.product.Application.start(Application.java:20)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
4

2 回答 2

2

注意[L类转换异常的第一部分开头[Lorg.eclipse.swt.graphics.FontData。这意味着该值是一个数组,FontData因此您需要执行以下操作:

Font font = new Font(Display.getCurrent(),
        (FontData []) newValue);

您还应该确保调用dispose您创建的任何字体。

于 2013-10-29T14:36:44.193 回答
0

在运行时,加载的类与编译调用代码的类不同。确保您的运行时库与您的构建库相同,并且您的运行时或构建中没有多个 API 版本。

于 2013-10-29T14:20:08.943 回答