Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

java - 尝试在Eclipse中运行复合GUI(Trying to run a composite GUI in Eclipse)

I am learning how to use the Eclipse IDE and trying to run a composite built in Eclipse.

(我正在学习如何使用Eclipse IDE,并尝试运行在Eclipse中构建的组合。)

However, I can't get it to run the GUI can anyone help?

(但是,我无法运行GUI,有人可以帮忙吗?)

I know I need public static void main(String[] args) but what do I need to add to this method to get it to run correctly?

(我知道我需要public static void main(String[] args)但是我需要添加什么方法才能使其正常运行?)

Can anyone please help me.

(谁能帮帮我吗。)

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); 
}
}

I get an error on the line Framework framework = new Framework();

(我在Framework framework = new Framework();上遇到错误Framework framework = new Framework();)

as I need two arguments parent, font .

(因为我需要两个参数parent, font 。)

Please help and explain I'm only a beginner of Java.

(请帮助并解释我只是Java的初学者。)

  ask by Grace Devlin translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The main for an SWT program must always create the SWT Display and run the SWT event dispatch loop.

(SWT程序的main必须始终创建SWT显示并运行SWT事件分发循环。)

Controls such as Composite must be contained in a Shell control.

(诸如Composite控件必须包含在Shell控件中。)

So your main might look like:

(因此您的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();
   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...