试图独立运行applet

时间:2013-10-19 23:29:08

标签: java eclipse applet

所以我的任务如下:

Write a Java applet to add two numbers from text fields, and displays the result in a 
non-editable text field. Enable your applet to run standalone with a main method. A 
sample run of the applet is shown in the following figure.

我已经设法在Eclipse中运行applet本身,但是当我将它作为JAR导出时,当我尝试运行它时没有任何反应。我已经多次在我的教科书中重读这个(简短)部分,并且花了最后一小时左右在Google上。所以我在这里,寻求建议。

我的代码:

package NumberTwo;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class AddNumbers extends JApplet {
/**
 * 
 */
private static final long serialVersionUID = 1L;
private JTextField textFieldOne;
private JTextField textFieldTwo;
private JTextField textFieldResult;

private int numberOne, numberTwo;

public static void main(String[] args) {
    JFrame frame = new JFrame("Applet is in the frame");
    AddNumbers applet = new AddNumbers();
    applet.init();
    applet.start();
    frame.add(applet, BorderLayout.CENTER);
    frame.setSize(300, 300);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

/**
 * Create the applet.
 */
public AddNumbers() {
    getContentPane().setLayout(null);

    // Labels

    JLabel labelOne = new JLabel("Number 1");
    labelOne.setBounds(30, 35, 56, 16);
    getContentPane().add(labelOne);

    JLabel labelTwo = new JLabel("Number 2");
    labelTwo.setBounds(130, 35, 56, 16);
    getContentPane().add(labelTwo);

    JLabel labelResult = new JLabel("Result");
    labelResult.setBounds(230, 35, 56, 16);
    getContentPane().add(labelResult);

    JLabel labelAdd = new JLabel("Add");
    labelAdd.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            Addition();
        }
    });
    labelAdd.setBounds(164, 62, 56, 16);
    getContentPane().add(labelAdd);

    // Text Fields

    textFieldOne = new JTextField();
    textFieldOne.setBounds(90, 32, 30, 22);
    getContentPane().add(textFieldOne);
    textFieldOne.setColumns(10);

    textFieldTwo = new JTextField();
    textFieldTwo.setColumns(10);
    textFieldTwo.setBounds(190, 32, 30, 22);
    getContentPane().add(textFieldTwo);

    textFieldResult = new JTextField();
    textFieldResult.setEditable(false);
    textFieldResult.setColumns(10);
    textFieldResult.setBounds(272, 32, 30, 22);
    getContentPane().add(textFieldResult);

}

public void Addition() {
    numberOne = Integer.parseInt(textFieldOne.getText());
    numberTwo = Integer.parseInt(textFieldTwo.getText());
    textFieldResult.setText(String.valueOf(numberOne + numberTwo));
}
}

非常感谢任何见解。

1 个答案:

答案 0 :(得分:0)

经过一些试验和错误,看起来我想出来了 - 显然我在尝试导出时没有足够'下一步'。在这样做之后,我尝试了“选择应用程序入口点的类”并点击浏览,然后选择一个可用的选项。

我也不需要行

applet.init();
applet.start();

所以我也省略了它们。