1

我正在 Eclipse 中创建动态 web 项目。现在以下是我的java类

package com;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class FirstProject extends JPanel {
    private Map<Color, Integer> bars = new LinkedHashMap<Color, Integer>();

    /**
     * Add new bar to chart
     * 
     * @param color
     *            color to display bar
     * @param value
     *            size of bar
     */
    public void addBar(Color color, int value) {
        bars.put(color, value);
        repaint();
    }

    protected void paintComponent(Graphics g) {

        int max = Integer.MIN_VALUE;
        for (Integer value : bars.values()) {
            max = Math.max(max, value);
        }

        int width = (getWidth() / bars.size()) - 2;
        int x = 1;
        for (Color color : bars.keySet()) {
            int value = bars.get(color);
            int height = (int) ((getHeight() - 5) * ((double) value / max));
            g.setColor(color);
            g.fillRect(x, getHeight() - height, width, height);
            g.setColor(Color.black);
            g.drawRect(x, getHeight() - height, width, height);
            x += (width + 2);
        }
    }

    public Dimension getPreferredSize() {
        return new Dimension(bars.size() * 10 + 2, 50);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Bar Chart");
        FirstProject chart = new FirstProject();
        chart.addBar(Color.red, 100);
        chart.addBar(Color.green, 8);
        chart.addBar(Color.blue, 54);
        chart.addBar(Color.black, 23);
        frame.getContentPane().add(chart);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

以下是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>com.FirstProject</display-name>
  <welcome-file-list>
    <welcome-file>com.FirstProject</welcome-file>

  </welcome-file-list>
</web-app>.

现在,当我运行应用程序时,本地主机会显示浏览器上的整个代码。

如何获得所需的输出(在浏览器上显示的条形图)?我错过了什么还是我没有遵循正确的模式?

4

5 回答 5

4

If you want to display your output,Then you have to use the JSP, Servlet, NOT a JPANEL. JPanel cannot display data into browser.

You can use the JApplet and the display the code into browser.

You got my point?

于 2013-03-26T04:31:36.873 回答
0

使用JavaFX显示网页。

于 2013-10-04T15:01:34.633 回答
0

您正在使用 JPanel.. JPanel 是为桌面应用程序开发的.. 您必须使用 Jsp 或 servlet 才能在基于 Web 的应用程序中工作.. 您必须使用 Html 标签在 Web 浏览器中显示您的设计..

于 2013-03-26T04:56:34.880 回答
0

我认为这不是您问题的解决方案,但这可能会帮助您解决问题。

Swing 和 JSP+Servlet 之间的显着差异

JSP,Servlets:它们是服务器端技术,用于生成 HTML 页面

Swing用于桌面应用程序(可与 JApplets 一起使用)

于 2013-03-26T04:42:51.133 回答
0

正如每个人都指定的 Swing 是桌面应用程序,Jframe 不能用于 Web 应用程序。

您可以使用 jfreechart 在 Web 浏览器上仅显示静态图像。

在使用 swing 的桌面应用程序中,您可以自定义鼠标列表、键盘列表、工具提示等图表。

Jsp 只能显示静态图像文件。

于 2013-10-04T14:27:57.793 回答