如何使其作为Java小程序而不是Java应用程序运行?
我不知道如何让它作为小程序运行,请帮助 我一直在尝试很多事情,但都没有奏效。我相信我必须使如何使其作为Java小程序而不是Java应用程序运行?,java,swing,applet,awt,Java,Swing,Applet,Awt,我不知道如何让它作为小程序运行,请帮助 我一直在尝试很多事情,但都没有奏效。我相信我必须使类扩展Applet,但我不确定这样做之后要更改什么 编辑:别担心,我已经解决了问题!:D import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HowMany { private JFrame mainJFrame; private JLabel
类扩展Applet
,但我不确定这样做之后要更改什么
编辑:别担心,我已经解决了问题!: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);
}
}
}
}
要使java小程序运行,您需要在web服务器上运行它 此链接显示了有关如何制作小程序的oracles示例,您应该能够轻松地按照这些示例进行操作: 您还可以在该页面上看到,如果您试图将其作为本地小程序运行,并且将java安全性设置为高或非常高,那么它将无法工作。需要记住的东西。“我相信我必须让
类扩展Applet
”第一步就出了问题。如果你想继续这种疯狂行为,请使用JApplet
。顺便问一下,你是否意识到Chrom、FF和soon(?)IE将无法启动小程序?部署Java桌面应用程序的最佳方法是使用(…从网页上的链接,而不是制作小程序)。System.exit(0)代码>在小程序中永远不起作用。。