【问题标题】:How to set a web page while clicking on a button ? with JavaFX单击按钮时如何设置网页?使用 JavaFX
【发布时间】:2015-01-20 21:30:41
【问题描述】:

我正在尝试从TextField 获取URL 示例:http://www.google.com 并且我有一个WebView,通过单击“Enter 键”可以看到它,但问题是当我运行应用程序它没有显示任何内容请注意我正在使用FXML File。这是我的代码:

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

          } 
         }
         }

 });

}

所以请任何人为我解释这段代码的问题在哪里,我会非常感激:)

【问题讨论】:

  • 编辑您的问题以包含mcve - 确保它既是最小的又是可执行的,以便在有人运行它时复制问题。
  • 我还注意到您的所有问题都没有被接受的答案,所以如果您对有人在投票上下箭头右下方发布的答案感到满意,请单击复选标记以接受答案
  • @APro 我是否按照您的喜好回答了您的问题?
  • @sazzy4o 现在我正在尝试您的代码,如果问题解决了,我会提出“接受这个问题”,但无论如何感谢您的帮助:)
  • @APro 如果它不起作用,请发布错误并尝试修复它

标签: netbeans webview javafx


【解决方案1】:

这是一个简单的示例应用程序,当您在文本字段中按 Enter 时,它会转到您输入的网页:

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,您可能需要删除 @Override
我希望这会有所帮助

【讨论】:

    【解决方案2】:

    我不确定你是否想要 'if(text1.getText().equals("1")){' if 语句只有在有人输入字符“1”时才为真,但你如何设置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){ }
           }
    
              } 
             }
             }
    
     });
    
    }
    

    我希望这会有所帮助。如果您有任何问题,请尽管提问。

    【讨论】:

    • 我发现了这个代码在我的电脑上不起作用的问题对不起:(我认为问题出在“setOnKeyPressed”我认为它是“setOnKeyTyped”没有?
    • @APro 你得到什么错误或者你没有得到任何错误?
    • 我真的很想要 "if(text1.getText().equals("1")){" 因为这个 KeyListener 应该在 TextField 中并且这个 TextField 应该包含 "1" 然后按输入打开网页
    • 如果您希望文本字段中的数字不是数字“1”,您可能需要使用 .contains("1") 而不是 .equals("1") 例如如果有人输入“1and1.ca”,if 语句将返回 true,但如果您使用 .equals("1"),它将返回 false,我的另一个回答是否也是 5 小时前的回答您的问题?我认为这是一个更好的例子
    • 是的,谢谢它帮助了我,非常感谢你解决了我的错误;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    相关资源
    最近更新 更多