我创建了2个SWT应用程序,我想使用第二个程序(SWT应用程序)中的texteditor值到第一个应用程序,在同一个程序包中应该可以使用但是不是,任何人都可以帮助我吗?
计划1
package gui;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Text;
public class Editor {
public static Text texteditor;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shlNotepad = new Shell();
shlNotepad.setSize(450, 300);
shlNotepad.setText("Notepad");
Menu menu = new Menu(shlNotepad, SWT.BAR);
shlNotepad.setMenuBar(menu);
MenuItem mntmFile_1 = new MenuItem(menu, SWT.CASCADE);
mntmFile_1.setText("File");
Menu menu_1 = new Menu(mntmFile_1);
mntmFile_1.setMenu(menu_1);
MenuItem mntmNew = new MenuItem(menu_1, SWT.NONE);
mntmNew.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
}
});
mntmNew.setText("New");
MenuItem mntmSave = new MenuItem(menu_1, SWT.NONE);
mntmSave.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
}
});
mntmSave.setText("Save");
MenuItem mntmSaveAs = new MenuItem(menu_1, SWT.NONE);
mntmSaveAs.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
FileEditor.main(args);
}
});
mntmSaveAs.setText("Save As");
MenuItem mntmExit = new MenuItem(menu_1, SWT.NONE);
mntmExit.setText("Exit");
texteditor = new Text(shlNotepad, SWT.BORDER);
texteditor.setBounds(0, 0, 434, 241);
String texted = texteditor.getText();
shlNotepad.open();
shlNotepad.layout();
while (!shlNotepad.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
计划2
package gui;
import java.io.*;
import java.util.Scanner;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Text;
public class FileEditor{
private static Text text;
private static Text text_1;
/**
* Launch the application.
*
* @param args
*/
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shlSaveAs = new Shell();
shlSaveAs.setSize(417, 267);
shlSaveAs.setText("Save As");
text = new Text(shlSaveAs, SWT.BORDER);
text.setBounds(43, 28, 163, 25);
Button btnCreateDirectory = new Button(shlSaveAs, SWT.NONE);
btnCreateDirectory.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
try {
Scanner in = new Scanner(System.in);
String directory = text.getText();
System.out.println(directory);
File dir = new File(directory);
dir.mkdir();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
btnCreateDirectory.setBounds(276, 28, 107, 25);
btnCreateDirectory.setText("Create Directory");
text_1 = new Text(shlSaveAs, SWT.BORDER);
text_1.setBounds(43, 82, 163, 23);
Button btnNewButton = new Button(shlSaveAs, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
try {
Scanner in = new Scanner(System.in);
String filename = text_1.getText();
String directory = text.getText();
System.out.println(directory);
File newfilenp = new File(directory, filename);
FileWriter fw = new FileWriter(newfilenp);
String datain = texted;
fw.write(datain);
fw.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
});
btnNewButton.setBounds(276, 80, 107, 25);
btnNewButton.setText("Create File");
Button btnSave = new Button(shlSaveAs, SWT.NONE);
btnSave.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
}
});
btnSave.setBounds(168, 172, 75, 25);
btnSave.setText("Save");
shlSaveAs.open();
shlSaveAs.layout();
while (!shlSaveAs.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}