में लॉगिन संवाद को कैसे हिलाएं, जब भी कोई उपयोगकर्ता गलत लॉगिन नाम/पासवर्ड जोड़ी इनपुट करता है तो मैं अपने जावाफ़ैक्स/8 संवाद बॉक्स को और अधिक सुन्दर तरीके से कैसे हिला सकता हूं?जावाएफ़एक्स/8
चूंकि java8u40 में संवाद में कोई नहीं है, इसलिए मैंने इसे स्वयं बनाने के लिए तैयार किया है। हालांकि, यह काफी अच्छा नहीं लग रहा है।
इसके साथ क्या गलत है? क्या कोई मदद कर सकता है? क्या ऐसा करने में कोई बेहतर तरीका है?
public void loginDialog() {
// Create the custom dialog.
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("Mars Simulation Project");
dialog.setHeaderText("Log in");
dialog.setContentText("Enter your username and password : ");
dialog.initModality(Modality.NONE);
// Set the button types.
ButtonType loginButtonType = new ButtonType("Login", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
// Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField tfPlayer = new TextField();
tfPlayer.setPromptText("e.g. m03j");
PasswordField tfPassword = new PasswordField();
tfPassword.setPromptText("xxxx");
Button defaultPWB = new Button("Use Default");
Button guestB = new Button("As Guest");
defaultPWB.setOnAction(event -> {
tfPassword.setText("msp0");
});
guestB.setOnAction(event -> {
tfPlayer.setText("Guest_");
tfPassword.setText("msp0");
});
grid.add(new Label("Player Name :"), 0, 0);
grid.add(tfPlayer, 1, 0);
grid.add(guestB, 2, 0);
grid.add(new Label("Password :"), 0, 1);
grid.add(tfPassword, 1, 1);
grid.add(defaultPWB, 2, 1);
// Enable/Disable login button depending on whether a username was entered.
Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
loginButton.setDisable(true);
// Do some validation (using the Java 8 lambda syntax).
tfPlayer.textProperty().addListener((observable, oldValue, newValue) -> {
loginButton.setDisable(newValue.trim().isEmpty());
});
dialog.getDialogPane().setContent(grid);
// Request focus on the player name field by default.
Platform.runLater(() -> tfPlayer.requestFocus());
// Convert the result to a player name /host address pair when the login
// button is clicked.
dialog.setResultConverter(dialogButton -> {
if (dialogButton == loginButtonType) {
return new Pair<>(tfPlayer.getText(), tfPassword.getText());
}
return null;
});
Optional<Pair<String, String>> result = dialog.showAndWait();
result.ifPresent(input -> {
playerName = tfPlayer.getText();
logger.info("Player " + input.getKey() + " connecting to server at " + serverAddressStr);
try {
dialog.show();
makeContact(serverAddressStr);
// obtain a client id
boolean isSuccessful = sendRegister();
if (isSuccessful) {
dialog.close();
// establish chat...
} else {
// shake the dialog or send an alert to inform the user the
// player name is NOT valid
DialogEarthquakeCenter dec = new DialogEarthquakeCenter(dialog);
dec.startTimer();
try {
System.out.println("start sleeping ");
Thread.sleep(2000);
System.out.println("done sleeping ");
}
catch (InterruptedException e) {}
loginDialog();
}
} catch (Exception e) {
e.printStackTrace();
}
});
अब तक, मेरी समस्या यह है कि जैसे ही मैंने बटन "प्रवेश" मारा, संवाद डिफ़ॉल्ट रूप से बंद हो जाएगा है।
इसलिए मुझे इसे फिर से दिखाने के लिए dialog.show() का उपयोग करना होगा।
[संपादित करें] यह अभी भी क्षणिक अंतर को होने से रोक नहीं सकता है (संवाद गायब हो रहा है और फिर से दिखाई देता है)।
उसके बाद, मैं संवाद को हिलाकर डायलॉगइर्थक्वैक सेंटर का एक उदाहरण बना देता हूं।
ध्यान दें कि नीचे मेरी DialogEarthquakeCenter इस मूल का एक सीधा संशोधन है:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.scene.control.Dialog;
import javafx.util.Duration;
import javafx.util.Pair;
public class DialogEarthquakeCenter {
public static final int SHAKE_DISTANCE = 10;
public static final double SHAKE_CYCLE = 50;
public static final int SHAKE_DURATION = 500;
public static final int SHAKE_UPDATE = 5;
private Dialog<Pair<String, String>> dialog;
private int x, y;
private long startTime;
private Timer shakeTimer;
private final double TWO_PI = Math.PI * 2.0;
private Timeline timeline;
public DialogEarthquakeCenter(Dialog<Pair<String, String>> parent) {
dialog = parent;
}
/**
* Creates and starts the timer
*
* @return Scene
*/
public void startTimer() {
x = (int) dialog.getX();
y = (int) dialog.getY();
startTime = System.currentTimeMillis();
// Set up earth time text update
timeline = new Timeline(new KeyFrame(Duration.millis(SHAKE_DURATION), ae -> startNudging()));
//timeline.setCycleCount(javafx.animation.Animation.INDEFINITE);
timeline.play();
}
public void startNudging() {
x = (int) dialog.getX();
y = (int) dialog.getY();
startTime = System.currentTimeMillis();
shakeTimer = new Timer(SHAKE_UPDATE, new ActionListener() {
public void actionPerformed(ActionEvent e) {
shake();
}
});
shakeTimer.start();
}
public void shake() {
// calculate elapsed time
long elapsed = System.currentTimeMillis() - startTime;
//System.out.println("elapsed is " + elapsed);
// use sin to calculate an x-offset
double waveOffset = (elapsed % SHAKE_CYCLE)/SHAKE_CYCLE;
double angle = waveOffset * TWO_PI;
// offset the x-location by an amount
// proportional to the sine, up to shake_distance
int shakenX = (int) ((Math.sin(angle) * SHAKE_DISTANCE) + x);
Platform.runLater(() -> {
//dialog.hide();
dialog.setX(shakenX);
//System.out.println("set shakenX to " + shakenX);
dialog.setY(y);
dialog.show();
});
//try {Thread.sleep(20);}
//catch (InterruptedException ex) {}
// should we stop timer
if (elapsed >= SHAKE_DURATION) {
stopShake();
}
}
public void stopShake() {
shakeTimer.stop();
Platform.runLater(() -> {
timeline.stop();
dialog.close();
});
}
}
मैं सूचना है कि controlsfx संवाद एक शेक() विधि है किया था।
क्या किसी को पता है कि यह अच्छी तरह से काम करता है?
धन्यवाद किसी भी टिप्पणी के लिए बहुत!
आपको नियंत्रणों का प्रयास करना चाहिए संवाद नियंत्रण स्वयं को हिलाएं और देखें कि यह आपके लिए अच्छा काम करता है या नहीं। आप [fxexperience से डिब्बाबंद एनिमेशन] को भी देख सकते हैं (http://fxexperience.com/2012/03/canned-imimations/) - हालांकि वे नोड आधारित हैं और आपको उन्हें मंच के साथ काम करने के लिए अनुकूलित करना होगा । – jewelsea
JavaFX अनुप्रयोग थ्रेड पर थ्रेड.sleep() को कभी भी कॉल न करें, इसके बजाए एक विराम चिह्न का उपयोग करें। – jewelsea
ठीक है। controlfx संवाद को बहिष्कृत किया जाएगा और इसलिए मैं इसका उपयोग नहीं करता हूं। लेकिन यह मुझ पर डॉन करता है कि मैं वास्तव में देख सकता हूं कि वे कोड कैसे हिलाते हैं()। डिब्बाबंद एनिम देखेंगे। और मुझे रोकथाम के लिए स्विच करने के लिए याद दिलाने के लिए thx! आपके संशोधित शेकट्रांसशन के लिए – mk7