Hi everyone,i really hope you can assist with the following problem that i have been having,how do i add components to JApplets,what are the methods tha i should use for the task?

Recommended Answers

All 3 Replies

It's basicly the same as adding to a framed application, without a few methods such as setDefaultCloseOperation() and setSize().

One more thing, use this instead of the usual constructor:

public void init()
{
}

also, you don't need a main method, as you will be calling it from an html file.

here is a really simple example, i hope this helps

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

public class GUIOneB extends JApplet implements ActionListener
{
   JButton jOptionConfirm, jOptionInput, jOptionMessage;
   Container c;

   public void init()
   {
      c = getContentPane();
      c.setBackground(Color.yellow);
      c.setLayout(new FlowLayout());

      jOptionConfirm = new JButton("showConfirmDialog");
      jOptionInput = new JButton("showOptionInputDialog");
      jOptionMessage = new JButton("showOptionMessageDialog");

      jOptionConfirm.addActionListener(this);
      jOptionInput.addActionListener(this);
      jOptionMessage.addActionListener(this);

      c.add(jOptionConfirm);
      c.add(jOptionInput);
      c.add(jOptionMessage);
   }

   public void actionPerformed(ActionEvent e)
   {
      String returnValue;

      if(e.getSource() == jOptionConfirm)
         JOptionPane.showConfirmDialog(this, "Confirm Something");
      else if(e.getSource() == jOptionInput)
         returnValue = JOptionPane.showInputDialog("Enter Something.");
      else if(e.getSource() == jOptionMessage)
         JOptionPane.showMessageDialog(this, "Hi from a message dialog.");
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.