java - 尝试在 Eclipse 中运行复合 GUI

标签 java eclipse swt

我正在学习如何使用 Eclipse IDE 并尝试运行 Eclipse 中内置的组合。但是,我无法让它运行 GUI,有人可以帮忙吗?我知道我需要 public static void main(String[] args) 但是我需要向此方法添加什么才能使其正确运行?谁能帮帮我吗?

package gui;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Combo;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.plaf.metal.MetalIconFactory;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class GUI extends Composite {

// Strings to use as list items
private static final String[] items = { "Item 1", "Item 2", "Item 3", "Item 4" };

public Framework(Composite parent, int style) {
    super(parent, style);

    Combo comboBox= new Combo(this, SWT.DROP_DOWN);
    comboBox.setBounds(174, 36, 534, 20);
    comboBox.setItems(Algorithms);

    Label lblOut = new Label(this, SWT.NONE);
    lblOut.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
    lblOut.setBounds(38, 145, 534, 327);

    Button btnExit = new Button(this, SWT.NONE);
    btnExit.setText("EXIT");
    btnExit.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
    btnExit.setBounds(591, 421, 166, 51);
    btnExit.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            System.exit(0);
        }
    });
}

@Override
protected void checkSubclass() {
    // Disable the check that prevents subclassing of SWT components
}

public static void main(String[] args) {
    Framework framework = new Framework(); 
    Composite c = new Composite(framework, SWT.NONE); 
}
}

我在 Framework Framework = new Framework(); 行上收到错误,因为我需要两个参数 parent, font。请帮忙解释一下我只是Java的初学者。

最佳答案

SWT 程序的main 必须始终创建 SWT Display 并运行 SWT 事件调度循环。

Composite 等控件必须包含在 Shell 控件中。

所以你的main可能看起来像:

public static void main(final String [] args)
{
  // Create the display
  Display display = new Display();
  try
   {
     // Create main shell
     Shell shell = new Shell(display, SWT.SHELL_TRIM);

     shell.setText("Shell Title");

     shell.setLayout(new FillLayout());

     // Put framework in the shell
     new Framework(shell, SWT.NONE);

     // Open the shell
     shell.open();

     // Main event dispatch loop
     while (!shell.isDisposed())
      {
        if (!display.readAndDispatch())
          display.sleep();
      }
    }
  finally
   {
     // Clean up
     display.dispose();
   }
}

关于java - 尝试在 Eclipse 中运行复合 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59118821/

相关文章:

Java/SWT : an error has occurred see log for more

java - 当需要多线程时,如何在Java中使用List类?

java - 如何在Java中获取您所在时区的当前时间

java - Maven jersey Artifact 创建一个没有目标运行时选项的项目

java - 从 eclipse-update-site 升级到 eclipse-repository Maven 打包时出错

java - SWT - 表格/组合框

java - Eclipse 中典型窗口的适当对象

java - Apache Tiles 和 Spring MVC 中的全局异常页面

java - ScrolledComposite 内大量复合 Material 的 SWT 性能

java - 如何在 java 中以自下而上的方式读取文本文件?