为applet添加了主类,方法没有用

时间:2012-07-21 12:40:24

标签: java applet

这是一个类,我没有在项目中包含所有其他类。 我需要初始化方法“public boolean action(Event e,Object arg)”但我不知道如何初始化进行参数的方法。

* To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package finalpkg;

import java.awt.*;

import javax.swing.*;

public class SimplexTool extends JApplet {
    private static final long serialVersionUID = 1L;
    public static void main (String[] arg){
        SimplexTool applet = new SimplexTool();
        final JFrame home = new JFrame("Start");
        home.setContentPane(applet.getContentPane());
        home.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        home.pack();
        home.setSize(200, 350);
        home.setLocation(450,200);
        home.setVisible(true);
                applet.init();
        applet.start();
    }

    int numConstraints;
    int numVariables;

    revisedSimplex LP;
    problemDimensionWindow DimensionWindow;
    enterDataFrame dataFrame;

    numberCrunchingFrame activeCruncher, oldCruncher;
    About aboutWindow;
    float coefficients[] = new float[33];
    int SolveStatus;

    Button start = new Button("New Problem");
    Button end   = new Button("Quit");
    Button about = new Button("About");
    Button help  = new Button("Help");

    @Override
    public void init()
    {
        super.init();
        this.setBackground(Color.gray);
        this.setLayout(new GridLayout(0,1,0,0));

        start.setBackground(Color.lightGray);
        end.setBackground(Color.lightGray);
        about.setBackground(Color.lightGray);
        help.setBackground(Color.lightGray);

        this.add(start);
        this.add(about);
        this.add(help);
        this.add(end);

        aboutWindow = new About(this);
    } /* end init procedure */

    @SuppressWarnings("deprecation")
    @Override
    public boolean action(Event e, Object arg)
    {
        if (e.target instanceof Button) {

            if (e.target == start) {
                if (dataFrame != null)      dataFrame.dispose();
                if (activeCruncher != null) activeCruncher.dispose();
                if (LP != null) LP.reset(numVariables, numConstraints);

                DimensionWindow = new problemDimensionWindow(this,"Enter the Sizes");
                DimensionWindow.pack();
                DimensionWindow.show();
                start.disable();
                return true;   /* handled this event */
            }

            if (e.target == end) {
                if (DimensionWindow != null) DimensionWindow.dispose();
                if (dataFrame != null)       dataFrame.dispose();
                if (activeCruncher != null)  activeCruncher.dispose();

                if (aboutWindow != null) aboutWindow.dispose();
                if (!start.isEnabled())  start.enable();
                if (!about.isEnabled())  about.enable();
                return true;   /* handled this event */
            }

            if (e.target == about) {
                about.disable();
                if (!aboutWindow.isShowing()) aboutWindow.show();
                return true;   /* handled this event */
            }
        }   /* end if instanceof Button */

        return false;    /* return false if we do not handle event */
    } /* end action procedure */

    @SuppressWarnings("deprecation")
    public void update(problemDimensionWindow InputWindow)
    {
        try {
            numVariables =
                Integer.valueOf(InputWindow.numVariables.getText()).intValue();
            numConstraints =
                Integer.valueOf(InputWindow.numConstraints.getText()).intValue();

            if (numVariables >= 2 && numConstraints >= 2 &&
                numVariables <= 7 && numConstraints <= 7) {
                InputWindow.dispose();
                LP = new revisedSimplex(numVariables, numConstraints);
                dataFrame = new enterDataFrame(this, "Enter Your Profit maximisation Linear Program!",
                numVariables, numConstraints);
                dataFrame.Messages.setText("Press Preprocess to Begin.");
                dataFrame.Messages.getAlignmentX();
                dataFrame.pack();
                dataFrame.show();
                start.enable();
            }
            else {
                if (numVariables > 7 || numVariables < 2)
                    InputWindow.reminderVar.setBackground(Color.red);
                else
                    InputWindow.reminderVar.setBackground(Color.lightGray);

                if (numConstraints > 7 || numConstraints < 2)
                    InputWindow.reminderCon.setBackground(Color.red);
                else
                    InputWindow.reminderCon.setBackground(Color.lightGray);
            }
        }
        catch (NumberFormatException e) {}
    } /* end update procedure */

    @SuppressWarnings("deprecation")
    public void update(numberCrunchingFrame LPWindow, boolean OneStepOnly)
    {
        if (OneStepOnly) {
            SolveStatus = LP.iterateOneStep();
            activeCruncher.updatePanelsForOneStep();

            if (LP.CurrentStep == 0)
                LPWindow.iterate.enable();
        }
        else {  /* perform one complete iteration */
            activeCruncher.Messages.setText("click view results to proceed");
            SolveStatus = LP.iterate();
            activeCruncher.updateAllPanels();
        }

        /* Check return status and take action if necessary */

        switch (SolveStatus) {
            case 3:   /*LP.Unbounded: */
                activeCruncher.Messages.setText("The problem is unbounded.");
                LPWindow.iterate.disable();
                LPWindow.step.disable();
                start.enable();
            break;

            case 1: /*LP.Optimal:*/
                if (LP.ArtificialAdded == false) {
                    activeCruncher.Messages.setText
                        ("This is the maximum Profit!!!");
                    dataFrame.Messages.setText("");
                    LPWindow.updateAllPanels();
                    LPWindow.iterate.disable();
                    LPWindow.step.disable();
                    start.enable();
                } else {  /* artificial variables were added (in phase I) */
                    /* if it passes this if test, then the problem is feasible */

                    /*** ADD CODE HERE TO TEST WHEN ARTIFICIALS ARE STILL IN BASIS ***/

                    if (LP.calculateObjective() == 0) {

                        if (LP.NumArtificials > 0) {
                            System.out.print  ("Even though 0 objective, there are");
                            System.out.println(" still artificials in basis.");
                        }

                        System.out.println("Calling getRidOfArtificials");
                        LP.getRidOfArtificials();
                        System.out.println("Called  getRidOfArtificials");

                        activeCruncher.Messages.setText
                            ("Artificial Variables Eliminated\nGetting New Window");

                        oldCruncher    = activeCruncher;
                        activeCruncher = new numberCrunchingFrame(this, "Final Stage",LP);
                        oldCruncher.dispose();

                        activeCruncher.Messages.setText
                            ("Continuing With Original Problem, click view results to proceed");
                        dataFrame.Messages.setText("In Final Stage");
                    } else {
                        activeCruncher.Messages.setText("The Problem is infeasible.");
                        LPWindow.iterate.disable();
                        LPWindow.step.disable();
                        start.enable();
                    }
                }
            break;

        } /* end switch (SolveStatus) */
    }   /* end update procedure */

    public boolean ReadDataAndPreprocess(int numVariables, int numConstraints)
    {
        /* Specify the objective function */

        float rhs;

        LP.reset(numVariables, numConstraints);  /* reset LP class for reading */

        for (int i = 0; i < numVariables; i++) {
            if (dataFrame.objective[i].isNumber())
                coefficients[i] = dataFrame.objective[i].getFloat();
            else {
                /* error in reading data, notify user */
                dataFrame.Messages.setText("Error: Objective x"+(i+1));
                return false;
            }
        }

        if (dataFrame.minmax.getSelectedIndex() == 0)
            LP.specifyObjective(coefficients, true);  /* minimize */
        else
            LP.specifyObjective(coefficients, false); /* maximize */

        /* Let the SimplexTool Class know about the constraints */

        for (int i = 0; i < numConstraints; i++) {
            /*LP.showInfo();*/
            for(int j = 0; j < numVariables; j++) {
                if (dataFrame.constraint[i][j].isNumber())
                    coefficients[j] = dataFrame.constraint[i][j].getFloat();
                else {
                    dataFrame.Messages.setText
                        ("Error: Constraint "+(i+1)+" in x"+(j+1));
                    return false;
                }
            } /* end for j */

            if (dataFrame.rhs[i].isNumber())
                rhs  = dataFrame.rhs[i].getFloat();
            else {
                dataFrame.Messages.setText("Error: Constraint "+(i+1)+ "RHS");
                return false;
            }

            LP.addConstraint(coefficients, rhs,
            dataFrame.rowType[i].getSelectedIndex());
        } /* end for i loop */

        /* Now that we have the data, perform preprocessing */

        LP.preprocess(numVariables, numConstraints);

        dataFrame.setEditable(false);
        dataFrame.Messages.setText("Starting to compute the optimal profit");

        /*if (LP.artificialPresent) {*/
        if (LP.NumArtificials > 0) {
            activeCruncher = new numberCrunchingFrame(this, "P",LP);
            dataFrame.Messages.setText("Starting ");
        }
        else {
            activeCruncher = new numberCrunchingFrame(this, "Results",LP);
            dataFrame.Messages.setText("Starting to find the results");
        }

        return true;  /* successful completion */
    } /* End ReadDataAndPreprocess */

    public void constrain(Container container, Component component,
        int grid_x, int grid_y, int grid_width,
        int grid_height,
        int fill, int anchor, double weight_x, double weight_y,
        int top, int left, int bottom, int right)
    {
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = grid_x; c.gridy = grid_y;
        c.gridwidth = grid_width; c.gridheight = grid_height;
        c.fill = fill; c.anchor = anchor;
        c.weightx = weight_x; c.weighty = weight_y;
        if (top+bottom+left+right > 0)
        c.insets = new Insets(top, left, bottom, right);

        ((GridBagLayout)container.getLayout()).setConstraints(component, c);
        container.add(component);
    } /* end constrain procedure */

    public void constrain(Container container, Component component,
            int grid_x, int grid_y, int grid_width,
            int grid_height) {
        constrain(container, component, grid_x, grid_y,
              grid_width, grid_height, GridBagConstraints.NONE,
              GridBagConstraints.NORTHWEST, 0.0, 0.0, 0, 0, 0, 0);
    } /* end constrain procedure */

    public void constrain(Container container, Component component,
                  int grid_x, int grid_y, int grid_width, int grid_height,
                  int top, int left, int bottom, int right)
    {
        constrain(container, component, grid_x, grid_y,
           grid_width, grid_height, GridBagConstraints.NONE,
           GridBagConstraints.NORTHWEST,
           0.0, 0.0, top, left, bottom, right);
    } /* end constrain procedure */
}  /* end SimplexTool class */

2 个答案:

答案 0 :(得分:0)

Applets 没有main()方法。

答案 1 :(得分:0)

您可能误解了 Applet类架构/框架。

阅读教程 - Lesson: Java Applets

  

Java applet是一种特殊的Java程序,支持浏览器   使用Java技术可以从互联网上下载并运行。小程序   通常嵌入在网页中并在a的上下文中运行   浏览器。 applet必须是java.applet.Applet类的子类。   Applet类提供applet之间的标准接口   和浏览器环境。

运行/启动applet:

  1. 创建.html文档
  2. 添加<applet code="finalpkg.SimplexTool" width="200" height="200"></applet>代码
  3. 在网络浏览器中打开此html文档。
  4. PS: 实际上,您可以通过扩展GUI Application将您的计划转换为JFrame