参考我的这个问题,fxml file not behaving as expected in OS X,我能够解决第一个问题,但我仍然面临着进度条的问题。当我的应用程序窗口没有最大化但是当我的应用程序窗口最大化时,它的行为很好,我的进度条对话占据了整个屏幕。我的进度条代码如下:
public ProgressBar startProgressBar() {
primaryStage = new Stage();
ProgressBar pb = new ProgressBar(0);
//ProgressIndicator pi = new ProgressIndicator(0);
//pi.progressProperty().bind(pb.progressProperty());
HBox hb = new HBox();
hb.setSpacing(5);
hb.setAlignment(Pos.CENTER);
hb.getChildren().addAll(pb);
Scene scene = new Scene(hb, 300, 100);
primaryStage.setScene(scene);
primaryStage.setTitle("Downloading Build...");
primaryStage.show();
return pb;
}
我尝试使用以下内容,但这些都不起作用。
primaryStage.setFullScreen(false);
primaryStage.setMaxHeight(200);
primaryStage.setMaxWidth(400);
但这也行不通。如果我将高度和宽度固定为常数,则对话框仅取高度和宽度,但背景屏幕显示为黑色。
一旦用户点击下载按钮,我就会触发进度条这是我的下载进程代码。在进度条中,我正在比较我的下载文件大小和服务器中文件的大小,我正在使用本地目录中的文件大小更新进度条。
private void download() {
FTPClient ftpClient = new FTPConnection().makeConnection(loc);
try {
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
success = ftpClient.changeWorkingDirectory(PATH + preset + "/" + file_to_download + offset);
System.out.println("Download Path:-" + PATH + preset + "/" + file_to_download + offset);
if (!success) {
System.out.println("Could not changed the directory to RIBS");
return;
} else {
System.out.println("Directory changed to RIBS");
}
FTPFile[] files = ftpClient.listFiles();
for (FTPFile file : files) {
if (file.getName().contains(".zip")) {
dfile = file;
}
}
fileMap.put("build", dfile.getName());
//primaryStage = (Stage) ap.getScene().getWindow();
String homePath = System.getProperty("user.home");
File downloadPath;
if(getOS().equals("Mac"))
{
downloadPath = new File(homePath + "/file path/" + osVer);
}
else
{
downloadPath = new File(homePath + "\\file path\\" + osVer);
}
if (!downloadPath.exists()) {
if (downloadPath.mkdirs()) {
System.out.println("Directory is created!");
} else {
System.out.println("Failed to create directory!");
}
}
// System.out.println(chosenDir.getAbsolutePath());
filePath = new File(downloadPath + "/" + dfile.getName());
if (filePath.exists()) {
System.out.println("File altready exist");
JOptionPane.showMessageDialog(null, "File already exists", "InfoBox: " + "",
JOptionPane.INFORMATION_MESSAGE);
return;
} else {
downloadFile = new File(downloadPath + "/" + dfile.getName());
// Progress bar
Task<Void> progress = new Task<Void>() {
@Override
protected Void call() throws Exception {
try {
for (long progress = 0; progress < dfile.getSize() ; progress = downloadFile.length()) {
Thread.sleep(300);
System.out.println(progress);
updateProgress(progress, dfile.getSize());
}
} catch (InterruptedException e) {
}
finally {
}
return null;
}
};
ProgressBar slider = startProgressBar();
slider.progressProperty().bind(progress.progressProperty());
// download task
Task downloadTask = new Task<Void>() {
@Override
public Void call() throws IOException {
try {
long len = dfile.getSize();
System.out.println("File From Server:::::: " + len);
System.out.println("DOWNLOAD FILE:::::" + downloadFile);
outputFile = new FileOutputStream(downloadFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ftpClient.sendNoOp();
ftpClient.setConnectTimeout(1000);
// ftpClient.retrieveFile(dfile, output);
if (ftpClient.retrieveFile(dfile.getName(), outputFile) == true) {
System.out.println("ReplyCOde:-" + ftpClient.getReplyCode());
downloadButton.setDisable(true);
try {
String homePath = System.getProperty("user.home");
Desktop.getDesktop().open(new File(homePath + "/file path"));
//primaryStage.hide();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("LOCAL FILE LENGTH:-" + downloadFile.length());
if (outputFile != null) {
try {
outputFile.close();
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
};
Thread t = new Thread(downloadTask);
t.start();
Thread thread = new Thread(progress);
thread.start();
downloadTask.setOnSucceeded(evt->primaryStage.hide());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//primaryStage.hide();
}
return;
}
任何对话框都有类似Mac OS X的行为。我尝试过JavaFx警告框也有相同的行为。有什么工作吗?
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText(null);
alert.setContentText("cannot connec to server or file not present at server");
答案 0 :(得分:2)
描述的效果是没有所有者的对话框的功能。要禁止不需要的行为,请在以下示例中指定对话框的所有者:
alert.initOwner(primaryStage);
下面的minimal example可用于重现Mac OS X 10.11,El Capitan上描述的效果。单击窗口左上角的绿色全屏按钮后,应用程序将展开以填充屏幕。单击显示警报按钮会使展开的窗口滑动到一边,消失以显示警报。取消注释initOwner()
并重新测试。展开的窗口(蓝色)现在仍在警报之下
没有所有者:
所有者集:
Apple suggests两种相关的解决方法之一:
选项 - 点击应用程序窗口左上角的绿色全屏按钮,以先前版本的方式展开。
在Dock首选项窗格中选择“双击要缩放的窗口标题栏”后,双击标题栏。
代码:
import java.util.Optional;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/**
* @see https://stackoverflow.com/a/37183801/230513
*/
public class AlertTest extends Application {
@Override
public void start(Stage primaryStage) {
Button button = new Button("Show Alert");
button.setOnAction(ae -> {
Alert alert = new Alert(AlertType.CONFIRMATION, "Are you sure?");
alert.initOwner(primaryStage);
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent() && result.get() == ButtonType.OK) {
System.out.println("Confirmed!");
} else {
System.out.println("Cancelled.");
}
});
StackPane root = new StackPane(button);
root.setBackground(new Background(new BackgroundFill(
Color.CORNFLOWERBLUE, CornerRadii.EMPTY, Insets.EMPTY)));
Scene scene = new Scene(root, 320, 240);
primaryStage.setTitle("Alert Test");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}