简体   繁体   English

如何使它作为Java小程序而不是Java应用程序运行?

[英]How do I make this run as a Java applet instead of a Java application?

I can't figure out how to get this to run as an applet, please help I've been trying a bunch of things but nothing is working. 我不知道如何使它作为applet运行,请帮助我尝试了很多事情,但没有任何效果。 I believe I have to make the class extend Applet but I'm not sure what to change after doing that 我相信我必须让class extend Applet但是我不确定这样做之后要更改什么

EDIT: Nevermind I have solved the problem! 编辑:没关系,我已经解决了问题! :D :D

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

public class HowMany {

   private JFrame mainJFrame;
   private JLabel headerJLabel;
   private JLabel statusJLabel;
   private JPanel controlJPanel;

   public HowMany() {
      prepareGUI();
   }

   public static void main(String[] args){ /* The main part of the code */
      HowMany HowMany = new HowMany();  
      HowMany.showEventDemo();       
   }

   private void prepareGUI() {
      mainJFrame = new JFrame("Click the Button!"); /*Sets the applet title */
      mainJFrame.setSize(400,400); /* Sets the size of the applet */
      mainJFrame.setLayout(new GridLayout(3, 1));

      headerJLabel = new JLabel("",JLabel.CENTER ); //Centers the header JLabel
      statusJLabel = new JLabel("",JLabel.CENTER); //Centers the status JLabel    

      statusJLabel.setSize(350,100); //Sets the size of the status JLabel
      mainJFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      controlJPanel = new JPanel();
      controlJPanel.setLayout(new FlowLayout());

      mainJFrame.add(headerJLabel); //adds all the stuff to the applet
      mainJFrame.add(controlJPanel);
      mainJFrame.add(statusJLabel);
      mainJFrame.setVisible(true);  
   }

   private void showEventDemo() {
      headerJLabel.setText("Click the Button to add to the bottom text!");     /* Tells you to click */
      /* Click button */
      JButton clickButton = new JButton("Click Me!");

      clickButton.setActionCommand("Click Me!");

      clickButton.addActionListener(new ButtonClickListener()); 
      /* Adds the buttons to the JFrame */
      controlJPanel.add(clickButton);     

      mainJFrame.setVisible(true);  
   }

   private class ButtonClickListener implements ActionListener {
      int x = 0; //Sets the starting value of the # of times the button was clicked to 0
      public void actionPerformed(ActionEvent e) {
         String command = e.getActionCommand();  
         /* Displays how many times the button was clicked */
         if( command.equals( "Click Me!" ))  {
             x = x + 1;
             String m = Integer.toString(x);
            statusJLabel.setText("Times clicked: " + m);
         }
      }     
   }
}

To make a java applet run you are going to need to run it on a web server. 要运行Java applet,您需要在Web服务器上运行它。

This link shows oracles examples on how to make an applet which you should easily be able to follow to work with your example: 该链接显示了有关如何制作applet的oracle示例,您应该可以轻松地使用它们来处理示例:

https://docs.oracle.com/javase/tutorial/deployment/applet/ https://docs.oracle.com/javase/tutorial/deployment/applet/

Also you can see on that page that if you are trying to run it as a local applet and have your java security set to high or very high it won't work. 您还可以在该页面上看到,如果您尝试将其作为本地小程序运行,并且将Java安全性设置为高或非常高,则将无法使用。 Something to keep in mind. 要记住的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM