我需要从另一个类获取字符串,如: 名为url的字符串在下面的第2类中确定,我需要获取它并copyUrlToFile(url,result +“\”\ plugins \ TentMod“)文件夹,其中url是我需要从另一个类获取的字符串,结果是文件夹路径来自文件夹选择器。
第1类
package me.ArsenArsen.TentName.installer;
import java.io.File;
public class TMInstaller {
protected Shell shlTentmodInstaller;
private Text txtInstallerHasStarted;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
TMInstaller window = new TMInstaller();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shlTentmodInstaller.open();
shlTentmodInstaller.layout();
while (!shlTentmodInstaller.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shlTentmodInstaller = new Shell();
shlTentmodInstaller.setImage(null);
shlTentmodInstaller.setTouchEnabled(true);
shlTentmodInstaller.setSize(450, 228);
shlTentmodInstaller.setText("TentMod Installer");
Button btnNewButton = new Button(shlTentmodInstaller, SWT.NONE);
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
DirectoryDialog dialog = new DirectoryDialog(shlTentmodInstaller, SWT.OPEN);
dialog.setFilterPath("c:\\");
dialog.setText("Select your server folder");
String result = dialog.open();
boolean success = (new File(result + "//" + "TentMod")).mkdirs();
String textboxtxt = txtInstallerHasStarted.getText();
if (!success) {
txtInstallerHasStarted.setText(textboxtxt + "\r\nMaking folder FAILED! Did folder exist???");
} else {
txtInstallerHasStarted.setText(textboxtxt + "\r\nMaking folder SUCESS\r\nDownloading files!");
try {
URL url = new URL(Adv_Settings.class.urls);
File destination = new File(result + "plugins\\TentMod\\tent.schematic");
FileUtils.copyURLToFile(url, destination);
} catch (IOException e1) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e1.printStackTrace(pw);
String err = sw.toString();
txtInstallerHasStarted.setText(txtInstallerHasStarted.getText() + "\r\nINTERNAL ERROR\r\nWIEW STACK TRACE:\r\n" + err);
}
}
}
});
btnNewButton.setBounds(10, 10, 414, 33);
btnNewButton.setText("Install TentMod");
String currentDir = System.getProperty("user.dir");
txtInstallerHasStarted = new Text(shlTentmodInstaller, SWT.BORDER | SWT.READ_ONLY | SWT.MULTI);
txtInstallerHasStarted.setText("Current Working Directory is: " + currentDir + "\r\nInstaller has started...\r\nWaiting for command...");
txtInstallerHasStarted.setBounds(10, 49, 414, 104);
Button btnNewButton_1 = new Button(shlTentmodInstaller, SWT.NONE);
btnNewButton_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
Adv_Settings config = new Adv_Settings();
config.open();
}
});
btnNewButton_1.setBounds(10, 159, 127, 25);
btnNewButton_1.setText("Settings (ADVANCED)");
Button btnExit = new Button(shlTentmodInstaller, SWT.NONE);
btnExit.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
shlTentmodInstaller.close();
}
});
btnExit.setBounds(349, 159, 75, 25);
btnExit.setText("Exit");
}
}
我需要来自第二类的公共字符串网址:
package me.ArsenArsen.TentName.installer;
import org.eclipse.swt.widgets.Display;
public class Adv_Settings {
private Text text;
public String urls = new String("http://arsenovic.host56.com/tent.schematic");
public String jar_url = new String("http://dev.bukkit.org/bukkit-plugins/tentmod/files/");
private Text text_1;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
Adv_Settings window = new Adv_Settings();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
Shell shell = new Shell();
shell.setSize(276, 134);
shell.setText("Advanced Settings - TentMod Installer");
Button btnApply = new Button(shell, SWT.NONE);
btnApply.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
if(text.getText().equals("")){
urls = "http://arsenovic.host56.com/tent.schematic";
} else urls = text.getText();
if(text_1.getText().equals("")){
jar_url = "http://dev.bukkit.org/bukkit-plugins/tentmod/files/";
} else jar_url = text.getText();
}
});
btnApply.setBounds(91, 64, 75, 25);
btnApply.setText("Apply");
Button btnOk = new Button(shell, SWT.NONE);
btnOk.setBounds(10, 64, 75, 25);
btnOk.setText("Ok");
Button btnCancel = new Button(shell, SWT.NONE);
btnCancel.setBounds(172, 64, 75, 25);
btnCancel.setText("Cancel");
text = new Text(shell, SWT.BORDER);
text.setBounds(10, 10, 237, 21);
text.setMessage("Insert your specific URL");
text_1 = new Text(shell, SWT.BORDER);
text_1.setBounds(10, 37, 237, 21);
text_1.setMessage("Insert your specific URL for JAR file");
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
字符串是公开的,这是以swt为主的开发,带有一点apache来下载文件。
答案 0 :(得分:2)
将urls字符串设为静态变量,并将该变量作为className.variableName。
访问public class Adv_Settings {
private Text text;
public static String urls = new String("http://arsenovic.host56.com/tent.schematic");
public String jar_url = new String("http://dev.bukkit.org/bukkit-plugins/tentmod/files/");
private Text text_1;
...
}
public class TMInstaller {
protected Shell shlTentmodInstaller;
private Text txtInstallerHasStarted;
protected void createContents() {
.....
URL url = new URL(Adv_Settings.urls);
}