开发者

Complications converting Swing application to an Applet

I finally got this calculator application in Netbeans to compile correctly and run client-side with no errors but for some reason I cannot get it to work as an applet. I have spent a lot of time trying to figure this out and researching but to no avail. Any experts around to take a quick look and see what the problem is?

I'd really appreciate any suggestions, thanks.

package eventhandler;

import javax.swing.JApplet;

public class CalculatorApplet extends JApplet {


    @Override
    public void init() {

    }

    @SuppressWarnings("unchecked")

    private void initComponents() {

        firstText = new javax.swing.JTextField();
        secondText = new javax.swing.JTextField();
        postLabel = new javax.swing.JLabel();
        postButton = new javax.swing.JButton();
        comboBox = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        postButton.setText("Solve");
        postButton.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                postName(evt);
            }
        });

        comboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "+", "-", "*", "/" }));

        jButton1.setText("Clear");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearFields(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(47, 47, 47)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, secondText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, firstText)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                .add(21, 21, 21)
                                .add(comboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(postLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 51, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(postButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 91, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jButton1)))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(firstText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(secondText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(comboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(postButton)
                    .add(jButton1))
                .add(5, 5, 5)
                .add(postLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                .addContainerGap())
    开发者_StackOverflow中文版    );

        //pack();
    }// </editor-fold>

    private void postName(java.awt.event.ActionEvent evt) {
        String ft = firstText.getText();
        String lt = secondText.getText(); 
        String total;
        double parse1, parse2;

        Object selectedCombo = comboBox.getSelectedItem();

        if (selectedCombo == "+") {
            parse1 = Double.parseDouble(ft);
            parse2 = Double.parseDouble(lt);
            total = String.valueOf(parse1 + parse2);
            postLabel.setText(total);

        } else if (selectedCombo == "-") {
            parse1 = Double.parseDouble(ft);
            parse2 = Double.parseDouble(lt);
            total = String.valueOf(parse1 - parse2);
            postLabel.setText(total);

        } else if (selectedCombo == "*") {
            parse1 = Double.parseDouble(ft);
            parse2 = Double.parseDouble(lt);
            total = String.valueOf(parse1 * parse2);
            postLabel.setText(total);

        } else if (selectedCombo == "/") {
            parse1 = Double.parseDouble(ft);
            parse2 = Double.parseDouble(lt);
            total = String.valueOf(parse1 / parse2);
            postLabel.setText(total);
        }

    }

    private void clearFields(java.awt.event.ActionEvent evt) {
        firstText.setText(null);
        secondText.setText(null);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new EventGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JComboBox comboBox;
    private javax.swing.JTextField firstText;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton postButton;
    private javax.swing.JLabel postLabel;
    private javax.swing.JTextField secondText;
    // End of variables declaration

    private void setDefaultCloseOperation(int EXIT_ON_CLOSE) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}


When using a applet people generally add the GUI code in the init() method. Your init() method is empty. Simple example:

//<applet code="AppletBasic.class" width="500" height="300"></applet>
// The above line makes it easy to test the applet from the command line by using:
// appletviewer AppletBasic.java

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

public class AppletBasic extends JApplet
{
    /**
     * Create the GUI. For thread safety, this method should
     * be invoked from the event-dispatching thread.
     */
    private void createGUI()
    {
        JLabel appletLabel = new JLabel( "I'm a Swing Applet" );
        appletLabel.setHorizontalAlignment( JLabel.CENTER );
        appletLabel.setFont(new Font("Serif", Font.PLAIN, 36));
        add( appletLabel );
    }

    public void init()
    {
        try
        {
            SwingUtilities.invokeAndWait(new Runnable()
            {
                public void run()
                {
                    createGUI();
                }
            });
        }
        catch (Exception e)
        {
            System.err.println("createGUI didn't successfully complete: " + e);
        }
    }

    public static void main(String[] args)
    {
        JApplet applet = new AppletBasic();
        applet.init();

        JFrame frame = new JFrame("Applet in Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( applet );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

        applet.start();
    }
}


As I can notice

  • A) You use GroupLayout but it is not a common J2SE < v6 object; As I can see you use a native NB lib and maybe that means you have an older JDK installed; if so you need to have GroupLayout in your applet jar as an imported lib; Anyway I recommend you just to have your JDK and JRE upgraded to 6
  • B) I couldn't notice any this.getContentPane().add() etc for your applet :( but still class extends JApplet

  • C) And, of course, you should place initGUI() the init() method because in applets it is as the same as the main method in desktop apps :)

Anyway, it is just a quick look... so if you have some thrown exception it would be more helpful you to add it in your question. That will help to analyze the problem in a more deep way :)

Good luck

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜