package test; import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.awt.*;
import java.io.*;
import java.lang.*; import org.eclipse.swt.widgets.Dialog;
public class alex { private Text text_1;
private Text text;
protected Shell shell;
private String tmp;
private String fn="";
static String filename;
/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
alex window = new alex();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Open the window
*/
public void open() {
final 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(520, 512);
shell.setText("SWT Application"); final Button loadButton = new Button(shell, SWT.NONE);
loadButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
//shell.setText("ilouyou");
FileDialog dlg=new FileDialog(shell,SWT.OPEN);
String filename=dlg.open();
try
{
if(filename!=null)
{
FileInputStream fis=new FileInputStream(filename);
text.setText("");
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
String s=null;
while((s=br.readLine())!=null){
text.append(s+"\r\n");
}
} if (filename== null)
{
fn = filename;
shell.setText(fn);
MessageBox successBox = new MessageBox(shell);
successBox.setText("信息");
successBox.setMessage("打开文件成功!");
successBox.open();
}}
catch(Exception e)
{
MessageBox errorBox = new MessageBox(shell, SWT.ICON_ERROR);
errorBox.setText("错误");
errorBox.setMessage("打开文件失败!");
errorBox.open(); }
}
});
loadButton.setText("load");
loadButton.setBounds(46, 407, 48, 22); text = new Text(shell, SWT.MULTI);
text.setBounds(10, 60, 237, 341);
final Button compileButton = new Button(shell, SWT.NONE);
compileButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent arg0){
Process hp=Runtime.getRuntime().exec(tmp="cmd.exe/c java";);
//hp.waitFor();
BufferedInputStream fis=new BufferedInputStream(hp.getInputStream());
BufferedReader br1=new BufferedReader(new InputStreamReader(fis));
//String s=null;
String s;
//StringBuffer sb;
//String tmp;
//try{
//while((tmp=br1.readLine())!=null)
//{
//sb.append(tmp);
//sb.append("\n");
//}
try{
while((s=br1.readLine())!=null){
text.append(s+"\r\n");
}
}
catch(Exception e)
{
MessageBox errorBox = new MessageBox(shell, SWT.ICON_ERROR);
errorBox.setText("错误");
errorBox.setMessage("打开文件失败!");
errorBox.open(); }
}
}); compileButton.setText("compile");
compileButton.setBounds(296, 407, 48, 22); text_1 = new Text(shell, SWT.MULTI);
text_1.setBounds(275, 60, 227, 341); final Button runButton = new Button(shell, SWT.NONE);
runButton.setText("run");
runButton.setBounds(434, 407, 48, 22);
//
}
//tmp="cmd.exe/c javac"+"\""+filename+" >tmp\" 2 >&1"
}
兄弟们帮忙分析一下哈,我要实现的就是在左边一个text框中load上.java代码,然后在右边的text框中实现编译和运行,把运行结果显示到右边的text文本框中,现在是load代码没有问题,就是编译这边出现问题,具体可能就是在Process hp=Runtime.getRuntime().exec(tmp="cmd.exe/c java";);出现了问题。请求解决方案。
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.awt.*;
import java.io.*;
import java.lang.*; import org.eclipse.swt.widgets.Dialog;
public class alex { private Text text_1;
private Text text;
protected Shell shell;
private String tmp;
private String fn="";
static String filename;
/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
alex window = new alex();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Open the window
*/
public void open() {
final 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(520, 512);
shell.setText("SWT Application"); final Button loadButton = new Button(shell, SWT.NONE);
loadButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
//shell.setText("ilouyou");
FileDialog dlg=new FileDialog(shell,SWT.OPEN);
String filename=dlg.open();
try
{
if(filename!=null)
{
FileInputStream fis=new FileInputStream(filename);
text.setText("");
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
String s=null;
while((s=br.readLine())!=null){
text.append(s+"\r\n");
}
} if (filename== null)
{
fn = filename;
shell.setText(fn);
MessageBox successBox = new MessageBox(shell);
successBox.setText("信息");
successBox.setMessage("打开文件成功!");
successBox.open();
}}
catch(Exception e)
{
MessageBox errorBox = new MessageBox(shell, SWT.ICON_ERROR);
errorBox.setText("错误");
errorBox.setMessage("打开文件失败!");
errorBox.open(); }
}
});
loadButton.setText("load");
loadButton.setBounds(46, 407, 48, 22); text = new Text(shell, SWT.MULTI);
text.setBounds(10, 60, 237, 341);
final Button compileButton = new Button(shell, SWT.NONE);
compileButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent arg0){
Process hp=Runtime.getRuntime().exec(tmp="cmd.exe/c java";);
//hp.waitFor();
BufferedInputStream fis=new BufferedInputStream(hp.getInputStream());
BufferedReader br1=new BufferedReader(new InputStreamReader(fis));
//String s=null;
String s;
//StringBuffer sb;
//String tmp;
//try{
//while((tmp=br1.readLine())!=null)
//{
//sb.append(tmp);
//sb.append("\n");
//}
try{
while((s=br1.readLine())!=null){
text.append(s+"\r\n");
}
}
catch(Exception e)
{
MessageBox errorBox = new MessageBox(shell, SWT.ICON_ERROR);
errorBox.setText("错误");
errorBox.setMessage("打开文件失败!");
errorBox.open(); }
}
}); compileButton.setText("compile");
compileButton.setBounds(296, 407, 48, 22); text_1 = new Text(shell, SWT.MULTI);
text_1.setBounds(275, 60, 227, 341); final Button runButton = new Button(shell, SWT.NONE);
runButton.setText("run");
runButton.setBounds(434, 407, 48, 22);
//
}
//tmp="cmd.exe/c javac"+"\""+filename+" >tmp\" 2 >&1"
}
兄弟们帮忙分析一下哈,我要实现的就是在左边一个text框中load上.java代码,然后在右边的text框中实现编译和运行,把运行结果显示到右边的text文本框中,现在是load代码没有问题,就是编译这边出现问题,具体可能就是在Process hp=Runtime.getRuntime().exec(tmp="cmd.exe/c java";);出现了问题。请求解决方案。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货