Applet不显示,但也没有错误

时间:2014-03-26 15:37:56

标签: java swing applet awt

我正在尝试为我的课程项目创建这个程序。编译器表示进程已完成,但在我尝试运行它时没有显示任何内容。

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

public class Project extends Applet implements ActionListener {

    Label sp = new Label("Receipt Calculator");
    Panel pl = new Panel();
    Label cndy = new Label("Candy:");
    TextField cndyi = new TextField(5);
    Label bred = new Label("Bread:");
    TextField bredi = new TextField(5);
    Label sop = new Label("Soap:");
    TextField sopi = new TextField(5);
    Label mlk = new Label("Milk:");
    TextField mlki = new TextField(5);
    Label ham = new Label("Ham:");
    TextField hami = new TextField(5);
    Label srdns = new Label("Sardines:");
    TextField srdnsi = new TextField(5);
    Label cfee = new Label("Coffee:");
    TextField cfeei = new TextField(5);
    Label ndls = new Label("Noodles:");
    TextField ndlsi = new TextField(5);
    Label salt = new Label("Salt:");
    TextField salti = new TextField(5);
    Label btrs = new Label("Batteries:");
    TextField btrsi = new TextField(5);
    Button co = new Button("Compute Price");
    Panel pnl = new Panel();
    Label st = new Label("");
    Label tx = new Label("");
    Label t = new Label("");

    public void init() {
        setLayout(new GridLayout(2, 2));
        setBackground(Color.blue);

        add(sp);
        add(pl);

        pl.setLayout(new GridLayout(11, 2));

        pl.add(cndy);
        pl.add(cndyi);
        pl.add(bred);
        pl.add(bredi);
        pl.add(sop);
        pl.add(sopi);
        pl.add(mlk);
        pl.add(mlki);
        pl.add(ham);
        pl.add(hami);
        pl.add(srdns);
        pl.add(srdnsi);
        pl.add(cfee);
        pl.add(cfeei);
        pl.add(ndls);
        pl.add(ndlsi);
        pl.add(salt);
        pl.add(salti);
        pl.add(btrs);
        pl.add(btrsi);
        add(co);
        co.addActionListener(this);
        add(pnl);
        pnl.setLayout(new GridLayout(3, 2));
        pnl.add(st);
        pnl.add(tx);
        pnl.add(t);
    }

    public void actionPerformed(ActionEvent z) {
        int a, b, c, d, e, f, g, h, i, j;
        double nst, ntx, nt;
        a = Integer.parseInt(cndyi.getText());
        b = Integer.parseInt(bredi.getText());
        c = Integer.parseInt(sopi.getText());
        d = Integer.parseInt(mlki.getText());
        e = Integer.parseInt(hami.getText());
        f = Integer.parseInt(srdnsi.getText());
        g = Integer.parseInt(cfeei.getText());
        h = Integer.parseInt(ndlsi.getText());
        i = Integer.parseInt(salti.getText());
        j = Integer.parseInt(btrsi.getText());

        nst = (a * 31.50) + (b * 35) + (c * 25) + 
              (d * 38.85) + (e * 43.15) + (f * 13) + 
              (g * 39) + (h * 7) + (i * 10) + (j * 30);
        ntx = nst + (nst * .12);

        nt = nst + ntx;
        st.setText("Sub-total = " + nst);
        tx.setText("Sub-total = " + ntx);
        t.setText("Sub-total = " + nt);
    }

    public static void main(String[] args) {
        new Project();
    }
}

4 个答案:

答案 0 :(得分:0)

尝试将所有面板放在框架中。尝试使用本教程。 http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html这是用于显示所有内容并使其可见的窗口。

答案 1 :(得分:0)

这对我来说似乎是一个功课问题。问题是你没什么可跑的。

public static void main(String[] args) {
    new Project();
}

所有这一切都是创建一个新对象,但之后程序终止:你需要一个循环。

试用本教程:Building Your First Java Applet

答案 2 :(得分:0)

  

尝试运行时没有显示

那是因为你没有要求任何东西。 new Project()只会创建一个Project对象,因为您没有定义默认构造函数,并且您没有显式调用任何其他方法,所以执行会立即退出。进行以下更改

new Project().init();

您需要将Panel放在JFrame中以使其可见。在init()方法

中尝试以下内容

JFrame frame = new JFrame(); frame.add(pl); frame.pack(); frame.setVisible(true);

答案 3 :(得分:0)

您的代码运行正常。我明白了:

enter image description here

我的猜测是你试图将它作为Java应用程序运行,而不是作为Java Applet运行。您的班级中确实有main()方法,这可能会导致这种混淆。 main()可以删除。对于applet,init()是入口点,如果它作为应用程序运行,则为main()

右键单击该类,然后选择Run As> Java Applet。例如:

enter image description here

相关问题