用户界面-打开和保存按钮在java SWT



我做了一些作业,我不知道如何开始制作保存和打开按钮。我使用SWT,我的应用程序将读写XML文件。我做的方法,但现在的问题是打开现有的文件和保存现有的文件。如果你能帮我,那就太好了。

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Combo;
import testic.CurrentData;
import testic.ReadWrite;

public class Test {
      // These filter names are displayed to the user in the file dialog. Note that
      // the inclusion of the actual extension in parentheses is optional, and
      // doesn't have any effect on which files are displayed.
      private static final String[] FILTER_NAMES = {
          "EXtensible Markup Language (*.xml)",
          "All Files (*.*)"};
      // These filter extensions are used to filter which files are displayed.
      private static final String[] FILTER_EXTS = { "*.xml", "*.*"};
    protected Shell shell;
    private Text text;
    private static CurrentData currentData = null;
    static ReadWrite test;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            Test window = new Test();
            currentData = new CurrentData();
            test = new ReadWrite(currentData) ;
            test.readXML();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setSize(450, 300);
        shell.setText("Test App");
        //UNIXUSER
        Label lblUsername = new Label(shell, SWT.NONE);
        lblUsername.setBounds(45, 64, 55, 15);
        lblUsername.setText("Username:");
        text = new Text(shell, SWT.BORDER);
        text.setBounds(179, 61, 91, 21);
        text.setText(currentData.getUnixUser());

        // DISABLE_TRAFFIC_CHECK
        Label lblStatus = new Label(shell, SWT.NONE);
        lblStatus.setBounds(45, 111, 118, 15);
        lblStatusk.setText("Disable traffic check:");
        Button btnCheckButton = new Button(shell, SWT.CHECK);
        btnCheckButton.setSelection(currentData.getDisableTrafficCheck());
        btnCheckButton.setBounds(212, 110, 93, 16);

        // HEALTCHECL_EVERY_TC
        Label lblActive = new Label(shell, SWT.NONE);
        lblActive.setBounds(45, 156, 129, 15);
        lblActive.setText("Health check every TC:");
        Menu menu_1 = new Menu(shell); //TODO check if we really need this
        shell.setMenu(menu_1);
        String[] items = { "None", "True", "False" };
        Combo combo = new Combo(shell, SWT.NONE);
        combo.setBounds(179, 156, 91, 23);
        combo.setItems(items);
        String hC = currentData.getHealthCheckEveryTC();
        if(hC.equalsIgnoreCase("None")){
            combo.select(0);
        } else if (hC.equalsIgnoreCase("True")){
            combo.select(1);
        } else {
            combo.select(2);
        }

        Menu menu = new Menu(shell, SWT.BAR);
        shell.setMenuBar(menu);
        /** Create file menu*/
        MenuItem mntmFile = new MenuItem(menu, SWT.CASCADE);
        mntmFile.setText("File");
        Menu menu_2 = new Menu(mntmFile);
        mntmFile.setMenu(menu_2);
         /** New Window*/
        MenuItem mntmNew = new MenuItem(menu_2, SWT.NONE);
        mntmNew.setText("New");
        mntmNew.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                Test windowNew = new Test();
                windowNew.open();                  
            }
        });
        /**Open window*/
        final Text fileName = new Text(shell, SWT.BORDER);
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        data.horizontalSpan = 4;
        fileName.setLayoutData(data);
        MenuItem mntmOpen = new MenuItem(menu_2, SWT.NONE);
        mntmOpen.setText("Open");
        mntmOpen.addSelectionListener(new SelectionAdapter() {
          public void widgetSelected(SelectionEvent event) {
            // User has selected to open a single file
            FileDialog dlg = new FileDialog(shell, SWT.OPEN);
            dlg.setFilterNames(FILTER_NAMES);
            dlg.setFilterExtensions(FILTER_EXTS);
            String fn = dlg.open();
            if (fn != null) {
              fileName.setText(fn);
            }
          }
        });
        MenuItem mntmSave = new MenuItem(menu_2, SWT.NONE);

        mntmSave.setText("Save");

        MenuItem mntmSaveAs = new MenuItem(menu_2, SWT.NONE);
        mntmSaveAs.setText("Save As");
        mntmSaveAs.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
              // User has selected to save a file
              test.createXML();
              FileDialog dlg = new FileDialog(shell, SWT.SAVE);
              dlg.setFilterNames(FILTER_NAMES);
              dlg.setFilterExtensions(FILTER_EXTS);
              dlg.setFilterPath("c:\"); // Windows path
              String fn = dlg.open();
              if (fn != null) {
                fileName.setText(fn);
              }
            }
          });
        MenuItem mntmExit = new MenuItem(menu_2, SWT.NONE);
        mntmExit.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION
                        | SWT.YES | SWT.NO);
                    messageBox.setMessage("Do you really want to exit?");
                    messageBox.setText("Exiting Application");
                    int response = messageBox.open();
                    if (response == SWT.YES)
                      System.exit(0);
            }
        });
        mntmExit.setText("Exit");
    }
    protected static void event(String string, Object object) {
        // TODO Auto-generated method stub
    }
}

FileDialogopen方法返回(第一个)选定文件的绝对路径,如果对话框被取消,则返回null的绝对路径。你应该读/写这个文件。例子:

FileDialog dlg = ...
String filename = dlg.open();
if (filename != null) {
  File file = new File(filename);
  readXML(file);
}

也许这个SWT片段是有帮助的?

相关内容

  • 没有找到相关文章

最新更新