2015-01-20 32 views
-1

我想要得到一個URLTextField exapmle:http://www.google.com,我有一個WebView,它將通過單擊「回車鍵」可見,但問題是當我運行應用程序它沒有顯示任何東西請注意,我用FXML File。這是我的代碼已經traied:如何在單擊按鈕時設置網頁?與JavaFX

@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){ } 
     } 

      } 
     } 
     } 

}); 

} 

所以,請能爲我身體的任何地方解釋是這個代碼的問題,我會很感激:)

+0

編輯您的問題包括[MCVE](http://stackoverflow.com /幫幫我/ mcve) - 確保它既小巧又可執行,以便在有人運行時複製問題。 – jewelsea 2015-01-20 21:37:09

+0

我也注意到,你所有的問題都沒有被接受的答案,所以如果你對答案感到滿意,那麼有人已經在右下角發表了投票上下箭頭點擊選中標記以接受答案 – sazzy4o 2015-01-21 00:54:09

+0

@APro我回答了你的問題問題到你的喜好? – sazzy4o 2015-01-21 15:28:19

回答

0

我真的不確定你是否想'if(text1.getText()。equals(「1」)){'if語句只會是真的在字符「1」中的ne類型,但是如何設置web引擎是通過從文本字段(text1)獲取文本並讓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){ } 
     } 

      } 
     } 
     } 

}); 

} 

我希望這有助於。如果你有問題,就問吧。

+0

我發現這個問題代碼它沒有工作在我的電腦對不起(我認爲問題是在「setOnKeyPressed」ithink它的「setOnKeyTyped」沒有? – APro 2015-01-21 15:59:19

+0

@APro你有什麼錯誤或你沒有得到任何錯誤? – sazzy4o 2015-01-21 16:00:44

+0

你會介意給我你的電子郵件向您展示問題? – APro 2015-01-21 16:04:48

2

下面是一個簡單的示例應用程序,進入該網頁,你鍵入當您在文本字段中輸入:

package application; 

import javafx.application.Application; 
import javafx.event.EventHandler; 
import javafx.scene.Scene; 
import javafx.scene.control.TextField; 
import javafx.scene.input.KeyEvent; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.web.WebEngine; 
import javafx.scene.web.WebView; 
import javafx.stage.Stage; 

public class Main extends Application { 




@Override 
public void start(Stage stage) throws Exception { 
    AnchorPane pane = new AnchorPane(); 
    Scene scene = new Scene(pane); 

    final TextField text1 = new TextField(); 
    WebView web = new WebView(); 
    final WebEngine webEngine= web.getEngine(); 
    text1.setOnKeyPressed(new EventHandler<KeyEvent>() { 
     public void handle(KeyEvent ke) { 
      if (ke.getCode().toString().equalsIgnoreCase("ENTER")) { 
          String urlString = text1.getText().trim(); 
          webEngine.load(urlString); 

    } 
    } 
    }); 
    pane.getChildren().addAll(web,text1); 

    stage.setScene(scene); 
    stage.sizeToScene(); 
    stage.show(); 
} 
public static void main(String[] args) { 
    Application.launch("application.Main"); 
} 
} 

你可以嘗試在https://www.google.com打字,它應該帶你去
如果您排除HTTP或HTTPS它不應該工作
根據您的JRE可能需要刪除@覆蓋
我希望這有助於

相關問題