【发布时间】:2015-03-12 21:15:24
【问题描述】:
我有两个fxml窗口登录和主窗口。它们各自的控制器如下:
public class MainwindowController extends Stage implements Initializable {
@FXML private Button Send;
@FXML private TextField txtBcast;
@FXML private ListView listviewUsers;
@FXML Label lblDisplayName;
/**
* Initializes the controller class.
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
ObservableList<String> chat =FXCollections.observableArrayList ("default");
listviewUsers.setItems(chat);
}
public void setLblName(String msg){
lblDisplayName.setText(msg);
}
@FXML public void ActionSend(ActionEvent e){
send();
txtBcast.setText("");
}
private void send() {
if (txtBcast.getText().isEmpty())
return;
// chatManager.sendPublicMsg(format,txtBcast.getText());
}
/**
*
* @param e
* @throws Exception
*/
@FXML public void ActionUserSelected( MouseEvent e) throws Exception{
// String lineRest = e.getActionCommand();
if(e.getClickCount()==2)
{
if(!listviewUsers.getSelectionModel().isEmpty())
{
String str=(String)listviewUsers.getSelectionModel().getSelectedItem();
Parent main= FXMLLoader.load(getClass().getResource("/letschat/fxwindows/Usertab.fxml"));
Scene scene = new Scene(main);
Stage stage = new Stage();
stage.setTitle(str);
stage.setScene(scene);
stage.show();
}
else { JOptionPane.showMessageDialog(null, "Oops! it seems you are trying to click the list view"); }
}
//Stage pstage = (Stage)listUsers.getScene().getWindow();
//pstage.close();
}
}
还有
public class LoginwindowController extends Stage implements Initializable {
@FXML private LoginwindowController loginwindowController;
@FXML private MainwindowController mainwindowController;
@FXML private Button btnSignIn;
@FXML private TextField txtDisplayName;
@FXML private ToggleGroup Gender;
@FXML private ComboBox comboStatus;
/**
* Initializes the controller class.
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
ObservableList<String> items =FXCollections.observableArrayList ("Online","Offline");
comboStatus.setItems(items);
writeToTextField();
}
public void writeToTextField() {
String username = System.getProperty("user.name");
txtDisplayName.setText(""+ username);
}
@FXML protected void ActionSignIn(ActionEvent event) throws Exception {
mainwindowController.setLblName(txtDisplayName.getText());
InetAddress addr = InetAddress.getLocalHost();
if(addr.isLoopbackAddress())
{
Dialogs.create().message("Oops! It seems you are not connected to any network..\n :(").showError();
}
else{
start(txtDisplayName.getText());// start chat manager
Parent root= FXMLLoader.load(getClass().getResource("/letschat/fxwindows/Mainwindow.fxml"));
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setTitle("LetsChat-Welcome "+ txtDisplayName.getText());
// Context.getInstance().setDisplayName(txtDisplayName.getText());
stage.setScene(scene);
stage.getIcons().add(new Image("/letschat/images/logo.png"));
Stage pstage = (Stage)btnSignIn.getScene().getWindow();
stage.show();
pstage.close();
}
}
private void start(String name) {
try {
ChatManager ic = new ChatManager(name);
ic.start();
} catch (Exception ex) {
Dialogs.create().message( "Could not start the chat session\nCheck that there no other instances running :(").showError();
}
}
}
当用户单击signin 按钮时,我希望主窗口中的标签lblDisplayName 使用登录窗口中的txt 显示名称中的文本进行更新。有人可以帮助如何做到这一点......请尽快
【问题讨论】:
-
应用从这里开始
-
public class LetsChat extends Application { public void start(Stage primaryStage)throws Exception { Parent root=FXMLLoader.load(getClass().getResource("/letschat/fxwindows/Loginwindow.fxml"));场景场景 = 新场景(根); primaryStage.getIcons().add(new Image("/letschat/images/logo.png")); primaryStage.setTitle("LetsChat - 登录"); primaryStage.setScene(场景); primaryStage.setResizable(false);主舞台.show(); } public static void main(String[] args) { 启动(args);}}
标签: javafx