试图在Eclipse中运行复合GUI

问题描述 投票:0回答:1

我正在学习如何使用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的初学者。

java eclipse swt
1个回答
0
投票

SWT程序的main必须始终创建SWT显示并运行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();
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.