Netbeans 如何在单击按钮时设置网页?使用JavaFX

Netbeans 如何在单击按钮时设置网页?使用JavaFX,netbeans,webview,javafx,Netbeans,Webview,Javafx,我正试图从TextFieldexapmle:http://www.google.com我有一个WebView通过单击“Enter key”可以看到它,但问题是,当我运行应用程序时,它没有显示任何注意到我正在使用FXML文件。这是我所跟踪的代码: @FXML private void onpressed (ActionEvent ee) { text1.setOnKeyPressed(new EventHandler<KeyEvent>() { public

我正试图从
TextField
exapmle:
http://www.google.com
我有一个
WebView
通过单击“Enter key”可以看到它,但问题是,当我运行应用程序时,它没有显示任何注意到我正在使用
FXML文件
。这是我所跟踪的代码:

@FXML
private void onpressed (ActionEvent ee) {
     text1.setOnKeyPressed(new EventHandler<KeyEvent>() {
         public void handle(KeyEvent evt) {
       if (evt.getCode() == KeyCode.ENTER){
       String az = text1.getText();
       //c.1
       if(text1.getText().equals("1")){
           web1.setVisible(true);
            String hh = text11.getText();
            Socket socket = new Socket();

    try {
        //open cursor
        text1.setCursor(Cursor.WAIT);
        que.setCursor(Cursor.WAIT);
        writ.setCursor(Cursor.WAIT);
        ancpa.setCursor(Cursor.WAIT);
        web1.setCursor(Cursor.WAIT);
        web2.setCursor(Cursor.WAIT);
        web3.setCursor(Cursor.WAIT);
        web4.setCursor(Cursor.WAIT);
        web5.setCursor(Cursor.WAIT);
        web6.setCursor(Cursor.WAIT);
        web7.setCursor(Cursor.WAIT);
        web8.setCursor(Cursor.WAIT);
        web9.setCursor(Cursor.WAIT);
        //do work
        WebEngine myWebEngine = web1.getEngine();
        myWebEngine.load("http://www.google.com");
        //close the window chooser
        Stage stage = new Stage();
          Parent root = FXMLLoader.load(getClass().getResource("Choose.fxml"));
          Scene scene = new Scene(root);
         stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
              @Override public void handle(WindowEvent t) { } });
        //close cursor
        ancpa.setCursor(Cursor.DEFAULT);
        web1.setCursor(Cursor.DEFAULT);
        web2.setCursor(Cursor.DEFAULT);
        web3.setCursor(Cursor.DEFAULT);
        web4.setCursor(Cursor.DEFAULT);
        web5.setCursor(Cursor.DEFAULT);
        web6.setCursor(Cursor.DEFAULT);
        web7.setCursor(Cursor.DEFAULT);
        web8.setCursor(Cursor.DEFAULT);
        web9.setCursor(Cursor.DEFAULT);
    }
   catch (IOException e){
       final  Stage stg = new Stage();           
        stg.initModality(Modality.APPLICATION_MODAL);
        stg.initOwner(stg);
        stg.setTitle("Cannot connect to the internet /n Please Verify your connection internet");
        labelno.setText("Cannot connect to the internet...");
        //close chooser
        Stage stage = new Stage();
         stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
              @Override public void handle(WindowEvent t) { } });

       //set cursor
         ancpa.setCursor(Cursor.DEFAULT);
        web1.setCursor(Cursor.DEFAULT);
        web2.setCursor(Cursor.DEFAULT);
        web3.setCursor(Cursor.DEFAULT);
        web4.setCursor(Cursor.DEFAULT);
        web5.setCursor(Cursor.DEFAULT);
        web6.setCursor(Cursor.DEFAULT);
        web7.setCursor(Cursor.DEFAULT);
        web8.setCursor(Cursor.DEFAULT);
        web9.setCursor(Cursor.DEFAULT);
   } finally{
       try{ socket.close(); } catch (Exception e){ }
       }

          } 
         }
         }

 });

}
@FXML
已按下私有void(ActionEvent ee){
text1.setOnKeyPressed(新的EventHandler(){
公共无效句柄(KeyEvent evt){
if(evt.getCode()==KeyCode.ENTER){
字符串az=text1.getText();
//c、 一,
if(text1.getText().equals(“1”)){
web1.setVisible(true);
字符串hh=text11.getText();
套接字=新套接字();
试一试{
//打开光标
text1.setCursor(Cursor.WAIT);
que.setCursor(Cursor.WAIT);
write.setCursor(Cursor.WAIT);
ancpa.setCursor(Cursor.WAIT);
web1.setCursor(Cursor.WAIT);
web2.setCursor(Cursor.WAIT);
web3.setCursor(Cursor.WAIT);
web4.setCursor(Cursor.WAIT);
web5.setCursor(Cursor.WAIT);
web6.setCursor(Cursor.WAIT);
web7.setCursor(Cursor.WAIT);
web8.setCursor(Cursor.WAIT);
web9.setCursor(Cursor.WAIT);
//工作
WebEngine myWebEngine=web1.getEngine();
myWebEngine.load(“http://www.google.com");
//关闭窗口选择器
阶段=新阶段();
父根=fxmloader.load(getClass().getResource(“Choose.fxml”);
场景=新场景(根);
stage.setOnCloseRequest(新的EventHandler(){
@重写公共无效句柄(WindowEvent t{});
//关闭光标
ancpa.setCursor(Cursor.DEFAULT);
web1.setCursor(Cursor.DEFAULT);
web2.setCursor(Cursor.DEFAULT);
web3.setCursor(Cursor.DEFAULT);
web4.setCursor(Cursor.DEFAULT);
web5.setCursor(Cursor.DEFAULT);
web6.setCursor(Cursor.DEFAULT);
web7.setCursor(Cursor.DEFAULT);
web8.setCursor(Cursor.DEFAULT);
web9.setCursor(Cursor.DEFAULT);
}
捕获(IOE异常){
最后阶段stg=新阶段();
stg.initmodel(model.APPLICATION_model);
stg.业主(stg);
stg.setTitle(“无法连接到internet/n请验证您的internet连接”);
labelno.setText(“无法连接到internet…”);
//闭合选择器
阶段=新阶段();
stage.setOnCloseRequest(新的EventHandler(){
@重写公共无效句柄(WindowEvent t{});
//设置光标
ancpa.setCursor(Cursor.DEFAULT);
web1.setCursor(Cursor.DEFAULT);
web2.setCursor(Cursor.DEFAULT);
web3.setCursor(Cursor.DEFAULT);
web4.setCursor(Cursor.DEFAULT);
web5.setCursor(Cursor.DEFAULT);
web6.setCursor(Cursor.DEFAULT);
web7.setCursor(Cursor.DEFAULT);
web8.setCursor(Cursor.DEFAULT);
web9.setCursor(Cursor.DEFAULT);
}最后{
请尝试{socket.close();}捕获(异常e){}
}
} 
}
}
});
}

因此,请任何人都能为我解释一下这段代码的问题出在哪里,我将非常感激:)

我不确定你是否想要'if(text1.getText().equals(“1”){'如果有人输入字符“1”,if语句才是真的,但是你是如何通过从文本字段(text1)获取文本来设置web引擎的让web引擎加载它,最好在结尾处放一个.trim(),以防用户意外地在结尾处键入空格。
所以您的代码应该如下所示:

String urlString = text1.getText().trim();
WebEngine myWebEngine = web1.getEngine();
myWebEngine.load(urlString);
@FXML
private void onpressed (ActionEvent ee) {
     text1.setOnKeyPressed(new EventHandler<KeyEvent>() {
         public void handle(KeyEvent evt) {
       if (evt.getCode() == KeyCode.ENTER){
       String az = text1.getText();
           web1.setVisible(true);
            String hh = text11.getText();
            Socket socket = new Socket();

    try {
        //open cursor
        text1.setCursor(Cursor.WAIT);
        que.setCursor(Cursor.WAIT);
        writ.setCursor(Cursor.WAIT);
        ancpa.setCursor(Cursor.WAIT);
        web1.setCursor(Cursor.WAIT);
        web2.setCursor(Cursor.WAIT);
        web3.setCursor(Cursor.WAIT);
        web4.setCursor(Cursor.WAIT);
        web5.setCursor(Cursor.WAIT);
        web6.setCursor(Cursor.WAIT);
        web7.setCursor(Cursor.WAIT);
        web8.setCursor(Cursor.WAIT);
        web9.setCursor(Cursor.WAIT);
        String urlString = text1.getText().trim();
        WebEngine myWebEngine = web1.getEngine();
        myWebEngine.load(urlString);    
        Stage stage = new Stage();
          Parent root = FXMLLoader.load(getClass().getResource("Choose.fxml"));
          Scene scene = new Scene(root);
         stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
              @Override public void handle(WindowEvent t) { } });
        //close cursor
        ancpa.setCursor(Cursor.DEFAULT);
        web1.setCursor(Cursor.DEFAULT);
        web2.setCursor(Cursor.DEFAULT);
        web3.setCursor(Cursor.DEFAULT);
        web4.setCursor(Cursor.DEFAULT);
        web5.setCursor(Cursor.DEFAULT);
        web6.setCursor(Cursor.DEFAULT);
        web7.setCursor(Cursor.DEFAULT);
        web8.setCursor(Cursor.DEFAULT);
        web9.setCursor(Cursor.DEFAULT);
    }
   catch (IOException e){
       final  Stage stg = new Stage();           
        stg.initModality(Modality.APPLICATION_MODAL);
        stg.initOwner(stg);
        stg.setTitle("Cannot connect to the internet /n Please Verify your connection internet");
        labelno.setText("Cannot connect to the internet...");
        //close chooser
        Stage stage = new Stage();
         stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
              @Override public void handle(WindowEvent t) { } });

       //set cursor
         ancpa.setCursor(Cursor.DEFAULT);
        web1.setCursor(Cursor.DEFAULT);
        web2.setCursor(Cursor.DEFAULT);
        web3.setCursor(Cursor.DEFAULT);
        web4.setCursor(Cursor.DEFAULT);
        web5.setCursor(Cursor.DEFAULT);
        web6.setCursor(Cursor.DEFAULT);
        web7.setCursor(Cursor.DEFAULT);
        web8.setCursor(Cursor.DEFAULT);
        web9.setCursor(Cursor.DEFAULT);
   } finally{
       try{ socket.close(); } catch (Exception e){ }
       }

          } 
         }
         }

 });

}
您完成的代码应该如下所示:

String urlString = text1.getText().trim();
WebEngine myWebEngine = web1.getEngine();
myWebEngine.load(urlString);
@FXML
private void onpressed (ActionEvent ee) {
     text1.setOnKeyPressed(new EventHandler<KeyEvent>() {
         public void handle(KeyEvent evt) {
       if (evt.getCode() == KeyCode.ENTER){
       String az = text1.getText();
           web1.setVisible(true);
            String hh = text11.getText();
            Socket socket = new Socket();

    try {
        //open cursor
        text1.setCursor(Cursor.WAIT);
        que.setCursor(Cursor.WAIT);
        writ.setCursor(Cursor.WAIT);
        ancpa.setCursor(Cursor.WAIT);
        web1.setCursor(Cursor.WAIT);
        web2.setCursor(Cursor.WAIT);
        web3.setCursor(Cursor.WAIT);
        web4.setCursor(Cursor.WAIT);
        web5.setCursor(Cursor.WAIT);
        web6.setCursor(Cursor.WAIT);
        web7.setCursor(Cursor.WAIT);
        web8.setCursor(Cursor.WAIT);
        web9.setCursor(Cursor.WAIT);
        String urlString = text1.getText().trim();
        WebEngine myWebEngine = web1.getEngine();
        myWebEngine.load(urlString);    
        Stage stage = new Stage();
          Parent root = FXMLLoader.load(getClass().getResource("Choose.fxml"));
          Scene scene = new Scene(root);
         stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
              @Override public void handle(WindowEvent t) { } });
        //close cursor
        ancpa.setCursor(Cursor.DEFAULT);
        web1.setCursor(Cursor.DEFAULT);
        web2.setCursor(Cursor.DEFAULT);
        web3.setCursor(Cursor.DEFAULT);
        web4.setCursor(Cursor.DEFAULT);
        web5.setCursor(Cursor.DEFAULT);
        web6.setCursor(Cursor.DEFAULT);
        web7.setCursor(Cursor.DEFAULT);
        web8.setCursor(Cursor.DEFAULT);
        web9.setCursor(Cursor.DEFAULT);
    }
   catch (IOException e){
       final  Stage stg = new Stage();           
        stg.initModality(Modality.APPLICATION_MODAL);
        stg.initOwner(stg);
        stg.setTitle("Cannot connect to the internet /n Please Verify your connection internet");
        labelno.setText("Cannot connect to the internet...");
        //close chooser
        Stage stage = new Stage();
         stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
              @Override public void handle(WindowEvent t) { } });

       //set cursor
         ancpa.setCursor(Cursor.DEFAULT);
        web1.setCursor(Cursor.DEFAULT);
        web2.setCursor(Cursor.DEFAULT);
        web3.setCursor(Cursor.DEFAULT);
        web4.setCursor(Cursor.DEFAULT);
        web5.setCursor(Cursor.DEFAULT);
        web6.setCursor(Cursor.DEFAULT);
        web7.setCursor(Cursor.DEFAULT);
        web8.setCursor(Cursor.DEFAULT);
        web9.setCursor(Cursor.DEFAULT);
   } finally{
       try{ socket.close(); } catch (Exception e){ }
       }

          } 
         }
         }

 });

}
@FXML
已按下私有void(ActionEvent ee){
text1.setOnKeyPressed(新的EventHandler(){
公共无效句柄(KeyEvent evt){
if(evt.getCode()==KeyCode.ENTER){
字符串az=text1.getText();
web1.setVisible(true);
字符串hh=text11.getText();
套接字=新套接字();
试一试{
//打开光标
text1.setCursor(Cursor.WAIT);
que.setCursor(Cursor.WAIT);
write.setCursor(Cursor.WAIT);
ancpa.setCursor(Cursor.WAIT);
web1.setCursor(Cursor.WAIT);
web2.setCursor(Cursor.WAIT);
web3.setCursor(Cursor.WAIT);
web4.setCursor(Cursor.WAIT);
web5.setCursor(Cursor.WAIT);
web6.setCursor(Cursor.WAIT);
web7.setCursor(Cursor.WAIT);
web8.setCursor(Cursor.WAIT);
web9.setCursor(Cursor.WAIT);
字符串urlString=text1.getText().trim();
WebEngine myWebEngine=web1.getEngine();
myWebEngine.load(urlString);
阶段=新阶段();
父根=fxmloader.load(getClass().getResource(“Choose.fxml”);
场景=新场景(根);
stage.setOnCloseRequest(新的EventHandler(){
@重写公共无效句柄(WindowEvent t{});
//关闭光标
ancpa.setCursor(Cursor.DEFAULT);
web1.setCursor(Cursor.DEFAULT);
web2.setCursor(Cursor.DEFAULT);
web3.setCursor(Cursor.DEFAULT);
web4.setCursor(Cursor.DEFAULT);
web5.setCursor(Cursor.DEFAULT);
web6.setCursor(Cursor.DEFAULT);
web7.setCursor(Cursor.DEFAULT);
web8.setCursor(Cursor.DEFAULT);
web9.setCursor(Cursor.DEFAULT);
}
捕获(IOE异常){
最后阶段stg=新阶段();
stg.initmodel(model.APPLICATION_model);
stg.业主(stg);
stg.setTitle(“无法连接到internet/n请验证您的internet连接”);
labelno.setText(“无法连接到internet…”);
//闭合选择器
阶段=新阶段();
stage.setOnCloseRequest(新的EventHandler(){
@凌驾