在java SWT中打开并保存按钮

时间:2015-06-02 10:18:28

标签: java user-interface 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

    }
}

1 个答案:

答案 0 :(得分:0)

open FileDialog方法返回(第一个)所选文件的绝对路径,如果对话框已取消,则返回null。您应该读/写此文件。例如:

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

也许这SWT snippet有用吗?