gpt4 book ai didi

java - 以 HTML 显示小程序

转载 作者:行者123 更新时间:2023-12-01 16:11:17 25 4
gpt4 key购买 nike

我在编写 Java 小程序并将其与 html 文件链接时遇到问题

java applet 是关于绘制饼图 3 个值的销售、成员(member)资格和添加。 java小程序的代码:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

/**
* Class AppDemo - write a description of the class here
*
* @author (your name)
* @version (a version number)
*/
public class AppDemo extends JApplet
{
public void init()
{
Container appC = getContentPane();

MyPanel myp = new MyPanel() ;
myp.setBorder(new EtchedBorder() ) ;
myp.setBackground(Color.red);
appC.add(myp);
}
}


class MyPanel extends JPanel {
public void paint(Graphics g)
{
g.setFont( new Font("Verdana", Font.BOLD , 18) ) ;
g.setColor(Color.green);
g.drawString("HELLO WORLD", 20, 20);
g.fillArc( 20, 50, 200, 200 , 0 , 90 ) ;
g.setColor( new Color(255, 128, 64) ) ;
g.fillArc( 20, 50, 200, 200 , 90 , 40 ) ;
g.setColor( Color.pink ) ;
g.fillArc( 20, 50, 200, 200 , 130 , 230 ) ;
}
}

现在我想取出第一个切片..仅将以下内容添加到 fillArc 方法的第一个参数(x 坐标)

(int) Math.round(Math.cos(put_first_value_here/360.0*Math.PI)*20)

仅将以下内容添加到 fillArc 方法的第二个参数(y 坐标)

-((int) Math.round(Math.sin(put_first_value_here/360.0*Math.PI)*20))

其中first_value是第一个圆弧的角度

和 html 文件:

<APPLET CODE="AppDemo.class" CODEBASE="." WIDTH=500 HEIGHT=500>
<param name=adds value=1100 />
<param name=memberships value=300/>
<param name=sales value=1000/>
</APPLET>

html 文件结束

他们告诉我使用构造函数来获取值,但我不知道该怎么做,也不明白为什么我应该使用它

提前致谢

最佳答案

如果我理解正确的话,您希望访问 <param.../> 中包含的值来自您的小程序中的标签。小程序可以通过 getParameter(String) 访问这些参数方法。您通常可以在init()内访问这些值。方法:

public class AppDemo extends JApplet
{
public void init()
{
String adds = getParameter("adds");
String memberships = getParameter("memberships");
String sales = getParameter("sales");

// The rest of your init() code...
}
}

关于java - 以 HTML 显示小程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1120099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com