我附上了下面的 javafx 示例应用程序,其中有一个用于对话框的对话框类。当单击对话框中存在的按钮时 - 它的内存增加太多。仅当对话框显示时 - 在 taskmanager 中支持它需要 57kb,然后当我们单击按钮并处理对话框时 - taskmanager 显示其内存开始增加 - 最后它崩溃,获取转储内存异常。
示例中有以下类 Dialog.java :它显示带有 ok - 取消按钮的对话框 MessageDialog.fxml :此 fxml 创建对话框 MessageDialogController 是 MessageDialog.fxml 的关联文件 JavaFXSample.java 是运行此示例的主要类。
Dialog.java
package javafxsample;
import java.io.IOException;
import java.io.InputStream;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
*
* @author Admin
*/
public class Dialog {
public static void ShowinfoDialog(String title, String Message, Stage parentStage, double w, double h) {
if (title == null || title.trim().isEmpty()) {
title = "Info";
}
showMessageDialog(title, Message, parentStage, "sidetheme.png", w, h);
}
public static void showMessageDialog(String title, String Message, Stage parentStage, String image, double w, double h) {
Stage stage = new Stage();
MessageDialogController messageDialogController = (MessageDialogController) replaceScene("/javafxsample/MessageDialog.fxml", stage);
messageDialogController.init(stage, Message, image);
if (parentStage != null) {
stage.initOwner(parentStage);
}
stage.initModality(Modality.APPLICATION_MODAL);
stage.initStyle(StageStyle.UTILITY);
stage.setResizable(false);
if (title != null && !title.trim().isEmpty()) {
stage.setTitle(title);
}
stage.setWidth(w);
stage.setHeight(h);
// Utility.setCentreLocation(stage, parentStage);
InputStream inputStream = null;
try {
inputStream = Dialog.class.getResourceAsStream(image);
stage.getIcons().add(new Image(inputStream));
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ex) {
}
}
}
stage.showAndWait();
}
public static Initializable replaceScene(String fXml, Stage mystage) {
InputStream in = null;
try {
FXMLLoader loader = new FXMLLoader();
in = Dialog.class.getResourceAsStream(fXml);
loader.setLocation(Dialog.class.getResource(fXml));
loader.setBuilderFactory(new JavaFXBuilderFactory());
AnchorPane page;
try {
page = (AnchorPane) loader.load(in);
} finally {
in.close();
}
Scene scene = new Scene(page);
mystage.setScene(scene);
return loader.getController();
} catch (Exception ex) {
System.out.println("Exception in replaceScene. " + fXml + ".Error:" + ex.getMessage());
return null;
}
}
}
JavaFXSample.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javafxsample;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* @author JavaUser1
*/
public class JavaFXSample extends Application {
@Override
public void start(final Stage primaryStage) {
Button btn = new Button();
btn.setText(" Click on ME ");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Dialog.ShowinfoDialog("Sample", "Clicked on button", primaryStage, 400.0, 150.0);
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
MessageDialog.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" prefHeight="138.0" prefWidth="306.0" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxsample.MessageDialogController">
<children>
<TitledPane animated="false" collapsible="false" prefHeight="138.0" prefWidth="347.0" text="" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="-40.0">
<content>
<AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<GridPane prefHeight="112.0" prefWidth="282.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label fx:id="lblicon" text="" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.rowIndex="0">
<GridPane.margin>
<Insets top="20.0" fx:id="x1" />
</GridPane.margin>
</Label>
<Label fx:id="lblMessage" text="" wrapText="true" GridPane.columnIndex="1" GridPane.rowIndex="0">
<font>
<Font size="14.0" />
</font>
<GridPane.margin>
<Insets left="2.0" top="20.0" />
</GridPane.margin>
</Label>
<Separator prefWidth="200.0" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="1" />
<Separator prefWidth="200.0" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="1">
<GridPane.margin>
<Insets top="4.0" />
</GridPane.margin>
</Separator>
<HBox id="HBox" fx:id="hBox" alignment="CENTER" spacing="5.0" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.halignment="CENTER" GridPane.rowIndex="2" />
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="82.0" minWidth="82.0" prefWidth="82.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-1.0" minWidth="10.0" prefWidth="220.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="-1.0" minHeight="10.0" prefHeight="68.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="15.0" minHeight="15.0" prefHeight="15.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="29.0" minHeight="29.0" prefHeight="29.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</children>
</AnchorPane>
</content>
</TitledPane>
</children>
</AnchorPane>
MessageDialogController.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javafxsample;
import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
/**
* FXML Controller class
*
* @author Admin
*/
public class MessageDialogController implements Initializable {
@FXML
private Label lblMessage;
@FXML
private Label lblicon;
@FXML
private HBox hBox;
private Stage myStage;
private String clicked = "cancel";
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
public void btnOkEvent(ActionEvent event) {
clicked = "yes";
myStage.close();
}
public void btnCancelEvent(ActionEvent event) {
clicked = "no";
myStage.close();
}
public void init(Stage stage, String Message, String image) {
Button btnOk = new Button("OK");
btnOk.setPrefSize(70.0, 23.0);
btnOk.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
btnOkEvent(t);
}
});
hBox.getChildren().add(btnOk);
lblMessage.setText(Message);
if (image != null && !image.trim().isEmpty()) {
Image imageRunBackupPlan = new Image(getClass().getResourceAsStream(image));
lblicon.setGraphic(new ImageView(imageRunBackupPlan));
}
this.myStage = stage;
}
public void init(Stage stage, String Message, String btnOkText, String btnCancelText, String image) {
clicked = "cancel";
Button btnOk = new Button(btnOkText);
btnOk.setPrefHeight(23);
Button btnCancel = new Button(btnCancelText);
btnCancel.setPrefHeight(23);
hBox.getChildren().addAll(btnOk, btnCancel);
btnOk.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
btnOkEvent(t);
}
});
btnCancel.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
btnCancelEvent(t);
}
});
lblMessage.setText(Message);
if (image != null && !image.trim().isEmpty()) {
InputStream inputStream = null;
Image imageRunBackupPlan = new Image(getClass().getResourceAsStream(image));
lblicon.setGraphic(new ImageView(imageRunBackupPlan));
}
this.myStage = stage;
}
public String clickedOn() {
return clicked;
}
}
最佳答案
如果您的 Dialog.java 的目的只是创建一个对话框,则删除它并使用它。 它只是一个您必须导入到项目中的 jar 文件
关于java - 在 javafx 应用程序中单击对话框按钮时内存增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15004106/
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我想用ruby编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/