2016-04-29 91 views
0

我创建了一个java代码(jframe),但我试图将它转换为applet,以便它可以出现在html网页上。由于我对Java编码不太熟悉,因此遇到了困难。将J帧转换为JApplet

这是迄今为止代码:

/** 
    * Implements a program with a pie chart 
* that shows interactive polling results for three candidates 
*/ 

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

    public class Poll extends JApplet { 

     public void init() { 
public Poll() 
    { 

super("Vote for Pat, Ismail, or Clair"); 
Container c = getContentPane(); 
c.setBackground(Color.WHITE); 
PollDisplayPanel chart = new PollDisplayPanel("Pat", "Ismail", "Clair"); 
PollControlPanel controls = new PollControlPanel(chart); 
c.add(chart, BorderLayout.CENTER); 
c.add(controls, BorderLayout.SOUTH); 

Poll w = new Poll(); 
} 



     } 
     } 

这是原来的JFrame代码

/** 
* Implements a program with a pie chart 
* that shows interactive polling results for three candidates 
*/ 

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

    public class Poll extends JFrame 
{ 
public Poll() 
    { 
    super("Vote for Pat, Ismail, or Clair"); 

Container c = getContentPane(); 
c.setBackground(Color.WHITE); 
PollDisplayPanel chart = new PollDisplayPanel("Pat", "Ismail", "Clair"); 
PollControlPanel controls = new PollControlPanel(chart); 
c.add(chart, BorderLayout.CENTER); 
c.add(controls, BorderLayout.SOUTH); 
    } 

    public static void main(String[] args) 
    { 
Poll w = new Poll();} 
    w.setBounds(300, 300, 400, 400); 
w.setDefaultCloseOperation(EXIT_ON_CLOSE); 
w.setVisible(true); 
    } 
    } 
+0

顺便说一下,在哪里'PollDisplayPanel'和其他类? – Arvind

+0

编码是人类为人类发明的。随之而来的是格式化的责任。因为上帝不懂代码,所以我们不能把这个留给上帝。 – randominstanceOfLivingThing

+1

1)请参阅[不支持Java插件的支持](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/)和[转移到无插件Web] (https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free)。 2)部署Java桌面应用程序的最佳方式是使用[Java Web Start](http://stackoverflow.com/tags/java-web-start/info)。 –

回答

1

不能直接定义内的​​另一个方法。在这里你已经做了相同的小程序。在init()内部,您正在添加构造函数Poll()

public void init() { 

public Poll() { 
    super("Vote for Pat, Ismail, or Clair"); 

Container c = getContentPane(); 
    c.setBackground(Color.WHITE); 
    PollDisplayPanel chart = new PollDisplayPanel("Pat", "Ismail", "Clair"); 
    PollControlPanel controls = new PollControlPanel(chart); 
    c.add(chart, BorderLayout.CENTER); 
    c.add(controls, BorderLayout.SOUTH); 

 Poll w = new Poll(); 
    } 

} 
+0

我认为**备用视图**更清晰。请选择它们并相应地编辑答案。当然,如果您更喜欢原始版本,只需[查看编辑历史记录](http://stackoverflow.com/posts/36927656/revisions)并回滚。 –