JApplet

Like awt’s Applet swing also provides the class JApplet to create the applet that may contains the swing components. JApplet has some enhanced features. The JApplet class is the subclass of Applet. JApplet support for adding menus and menu items. Default layout out of JApplet is border layout while in Applet default layout is FlowLayout.

Painting in JApplet is not as like Applet class. Paint code can not be placed directly to paint something.
//
// An example of the JApplet class.  For use with the applet.html file.
//

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
/*
 *<applet code="JAppletDemo" width=300 height=250>
</applet>
 */
public class JAppletDemo extends JApplet {
  public void init() {
    JPanel p = new JPanel();
    p.setBorder(BorderFactory.createTitledBorder("Login"));
    p.setBackground(new Color(0xCCFFCC));
    p.setLayout(new GridLayout(2, 2, 2, 2));
    p.add(new JLabel("Username"));
    p.add(new JTextField(10));
    p.add(new JLabel("Password"));
    p.add(new JPasswordField(10));
    Container content = getContentPane();
    content.setLayout(new FlowLayout());
    content.add(p);
  }
}

















//
// An example of the JApplet class.  For use with the applet.html file.
//


import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
/*
 *<applet code="JAppletDemo2" width=400 height=300>
</applet>
 */
public class JAppletDemo2 extends JApplet {
  public void init() {
    JPanel p = new JPanel();
    p.setBorder(BorderFactory.createTitledBorder("Login"));
    p.setBackground(new Color(0xCCFFCC));
    p.setLayout(new GridLayout(2, 2, 2, 2));
    p.add(new JLabel("Username"));
    p.add(new JTextField(10));
    p.add(new JLabel("Password"));
    p.add(new JPasswordField(10));
    Container content = getContentPane();
    content.setLayout(new FlowLayout());
    content.add(p);
    
    //Asdding menues
        JMenu file=new JMenu("File");
        file.setMnemonic(KeyEvent.VK_F);
        
        
        JMenu edit=new JMenu("Edit");
        JMenu view=new JMenu("View");
        JMenu help=new JMenu("Help");
        
        file.add(new JMenuItem("Open"));
        file.add(new JMenuItem("New"));
        file.addSeparator(  );
        //add separator
        file.add(new JMenuItem("Save"));
        file.add(new JMenuItem("Save As"));
        JMenuItem exit=new JMenuItem("Exit");
        
        file.add(exit);
        
        edit.add(new JMenuItem("Copy"));
        edit.add(new JMenuItem("Peste"));
        edit.add(new JMenuItem("Cut"));
        edit.add(new JMenuItem("Find"));
        edit.add(new JMenuItem("Replace"));
        
        view.add(new JMenuItem("wer"));
        view.add(new JMenuItem("sgfsd"));
        view.add(new JMenuItem("sdfgds"));
        
        JMenuBar menubar=new JMenuBar();
        menubar.add(file);
        menubar.add(edit);
        menubar.add(view);
        menubar.add(help);
        setJMenuBar(menubar);
  }

}



No comments:

Popular Posts