java - 我的第一个 gwt 应用程序不显示按钮

标签 java gwt

我正在开始使用 gwt,我不明白为什么我的应用程序无法显示任何按钮,即使在我的代码中我已指示要创建一个按钮。

我的入口点类是

package com.france.webapp.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

public class MyEntryPoint implements EntryPoint {

public MyEntryPoint(){
}

    @Override
    public void onModuleLoad() {
    Label label = new Label("Hello GWT !!!");
    Button button = new Button("Say something");

    button.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        Window.alert("Hello, again");
      }
    });

    RootPanel.get().add(label);
    RootPanel.get().add(button);
  }
}

我在 Windows 上使用最新版本的 jdk 和 eclipse juno。我的应用程序运行并显示 html 页面,但没有按钮。我已经使用gwt开发插件在最新版本的chrome和firefox 20上对其进行了测试。

我是否必须编译该项目,或者“运行方式”->“Web 应用程序”选项足以显示按钮吗?我的 gwt.xml 看起来不错,我正在按照 http://www.vogella.com/articles/GWT/article.html 上的教程进行操作。

这是我的 MyModule.gwt.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.5.1//EN" "http://google-web-toolkit.googlecode.com/svn/tags/2.5.1/distro-source/core/src/gwt-module.dtd">
<module>
    <inherits name="com.google.gwt.user.User" />
    <source path="client" />
    <entry-point class="com.france.webapp.client.MyEntryPoint"></entry-point>
</module>

我的 html.html 文件

<!doctype html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>html</title>
    <script type="text/javascript" language="javascript" src=".nocache.js"></script>
  </head>

  <body>
    <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>

  </body>
</html>

我查看了 eclipse 控制台,它是这样的

[WARN] 404 - GET /.nocache.js (127.0.0.1) 1397 bytes
   Request headers
      Host: 127.0.0.1:8888
      Connection: keep-alive
      Accept: */*
      User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
      Accept-Encoding: gzip,deflate,sdch
      Accept-Language: en-US,en;q=0.8
      Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
      Referer: http://127.0.0.1:8888/html.html?gwt.codesvr=127.0.0.1:9997

最佳答案

这可能是您的 html 文件有问题。我成功运行了您的代码,请尝试将其作为项目中 war 文件夹下的 html 文件的内容:

<!doctype html>
<!-- The DOCTYPE declaration above will set the     -->
<!-- browser's rendering engine into                -->
<!-- "Standards Mode". Replacing this declaration   -->
<!-- with a "Quirks Mode" doctype is not supported. -->

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    <!--                                                               -->
    <!-- Consider inlining CSS to reduce the number of requested files -->
    <!--                                                               -->
    <link type="text/css" rel="stylesheet" href="MyEntryPoint.css">

    <!--                                           -->
    <!-- Any title is fine                         -->
    <!--                                           -->
    <title>Web Application Starter Project</title>

    <!--                                           -->
    <!-- This script loads your compiled module.   -->
    <!-- If you add any GWT meta tags, they must   -->
    <!-- be added before this line.                -->
    <!--                                           -->
    <script type="text/javascript" language="javascript" src="myentrypoint/myentrypoint.nocache.js"></script>
  </head>
  <body>
  </body>
</html>

并确保它位于您的项目 xxx.gwt.xml 中的 com.france.webapp 包下:

<entry-point class='com.france.webapp.client.MyEntryPoint'/>

通过 Eclipse/jetty 运行应用程序时,加载小部件确实需要几秒钟,有时需要 10 秒钟或更长时间。

关于java - 我的第一个 gwt 应用程序不显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16486999/

相关文章:

java - GWT+Java : Globals, 单例和头痛

gwt - 如何继续学习 GWT

java - ListFragment 和 Fragment

java - 使用jquery在java中创建 session

java - 在 Java Swing 中,当我将 JScrollPane 添加到我的面板时,什么也没有出现

java - 如何读取 Java 中的 SNI 扩展?

java - Hibernate中的查询异常

java - Java中不使用java.net从URL获取域名

gwt - CAS 和 GWT - 将 GWT 保留在 CAS 重定向上

java - 在 GWT 中使用嵌入式 Neo4j 时出现 AccessControlException