如何在 Java SWT 中创建不显示图像

How to create an image in Java SWT without display

我正在学习 SWT,目标是创建一个向导。我正在修改现有教程。此时,我正在尝试在向导的一页中显示图像:

class FrontPage extends WizardPage {

  FrontPage() {
    super("FrontPage");
    setTitle("Listes des joueurs");
    setDescription("Veuillez déplacer toutes les listes dans le même fichier (ex: C:\Badminton\)");
  }

  public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NULL);
    GridLayout gridLayout = new GridLayout(2, false);
    composite.setLayout(gridLayout);

    Canvas canvas = new Canvas(composite, SWT.NONE);
    canvas.setBounds(10, 10, 693, 253);

    canvas.addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent e) {
        Image image = new Image(null, "C:\Benoit\Work\Java\Badminton\Folder_List_Players.png");

        e.gc.drawImage(image, 10, 10);

        image.dispose();
      }
    });

    setControl(composite);

  }

}

当 运行 此代码时,我的图像(大约 600 x 200)显示为缩略图 (10x10)。我想显示全尺寸。

  1. 我知道我可能在 Image 构造函数中有一个 Display 对象,但我不确定如何使它与 Composite 父对象一起工作。然而有趣的是,我仍然能够使用空显示对象显示图像。

  2. canvas.setBounds(10, 10, 693, 253);好像没有什么影响。

在此先感谢您的任何提示或帮助!!!

如果使用空显示创建图像,则使用当前显示。为清楚起见,您应该在创建图像时始终提供显示。

而且也不乏对当前显示的引用,例如:

Display display = event.display;

Display display = parent.getDisplay();

为了显示图像,更喜欢使用这样的 Label

Label imageLabel = new Label( parent, SWT.NONE );
imageLabel.setImage( ... );

要布局控件,请远离使用 setBounds() 和绝对坐标。改为使用布局管理器:https://www.eclipse.org/articles/article.php?file=Article-Understanding-Layouts/index.html