Total members 11895 |It is currently Fri Dec 27, 2024 4:41 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Here a code to make JFrame on applet,This program demonstrates the basics of creating a frame user interface with a menubar. It also shows how to add a menubar and dropdown menus to the applet, which wasn't possible in the basic AWT heavyweight component.
java code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
* This program demonstrates the basics of creating a frame
* user interface with a menubar. It also shows how to
* add a menubar and dropdown menus to the applet, which wasn't
* possible in the basic AWT heavyweight component.
**/
public class FrameApplet extends JApplet
implements ActionListener
{
JFrame fFrame;
JMenuItem fMenuClose ;
JMenuItem fMenuOpen;

/** Build an applet interface with a menubar. A
* a drop down menu includes Open/Close items
* for opening and closing an instance of ParticleFrame.
**/
public void init () {
JMenuBar mb = new JMenuBar ();
JMenu m = new JMenu ("File");
fMenuOpen= new JMenuItem ("Open");
m.add (fMenuOpen);
fMenuOpen.addActionListener (this);

fMenuClose = new JMenuItem ("Close");
m.add (fMenuClose);
fMenuClose.addActionListener (this);
mb.add (m);

setJMenuBar (mb);

fFrame = new ParticleFrame (this);
fFrame.setVisible (true);
fMenuOpen.setEnabled (false);
fMenuClose.setEnabled (true);

} // init

/** Get the menu events here. Open an instance of ParticleFrame
* or close the one currently displayed.
**/
public void actionPerformed (ActionEvent e) {
String command = e.getActionCommand ();
if (command.equals ("Close")) {
close ();
} else { // Open
if (fFrame == null) {
fFrame = new ParticleFrame (this);
fFrame.setVisible (true);
fMenuOpen.setEnabled (false);
fMenuClose.setEnabled (true);
}
}
} // actionPerformed

/** Close the frame. **/
void close () {
fFrame.dispose ();
fFrame = null;
fMenuOpen.setEnabled (true);
fMenuClose.setEnabled (false);
} // close

} // class FrameApplet


/** A JFrame subclass that displays a menu bar
* and a JComboBox.
**/
class ParticleFrame extends JFrame
implements ActionListener, ItemListener
{
JLabel fLabelA;
JLabel fLabelB;

FrameApplet fApplet;

ParticleFrame (FrameApplet applet) {
super ("Frame Test");

fApplet = applet;
Container content_pane = getContentPane ();

content_pane.setLayout (new GridLayout (1,3));

JPanel choice_panel = new JPanel ();
choice_panel.add (new JLabel ("Quark", JLabel.RIGHT) );

JComboBox c = new JComboBox ();
c.addItem ("Up");
c.addItem ("Down");
c.addItem ("Strange");
c.addItem ("Charm");
c.addItem ("Top");
c.addItem ("Bottom");
c.addItemListener (this);
choice_panel.add (c);

content_pane.add (choice_panel);

fLabelA =new JLabel ("Quark: Up");
content_pane.add (fLabelA);
fLabelB =new JLabel ("Lepton: Electron");
content_pane.add (fLabelB);

// Use the helper method makeMenuItem
// for making the menu items and registering
// their listener.
JMenu m = new JMenu ("Lepton");
m.add (makeMenuItem ("electron"));
m.add (makeMenuItem ("muon"));
m.add (makeMenuItem ("tau"));

JMenu sm = new JMenu ("Neutrino");
sm.add (makeMenuItem ("e Neutrino"));
sm.add (makeMenuItem ("mu Neutrino"));
sm.add (makeMenuItem ("tau Neutrino"));

m.add (sm);
m.add (makeMenuItem ("Quit"));
JMenuBar mb = new JMenuBar ();
mb.add (m);

setJMenuBar (mb);
setSize (200,200);
pack ();
setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
} // ctor

/** Get the combobox item events here. **/
public void itemStateChanged (ItemEvent e) {
String command = e.getItem ().toString ();
if (command.equals ("Quit") )
dispose ();
else
fLabelA.setText ("Quark: " + command);
} // itemStateChanged

/** Get the menu events here. **/
public void actionPerformed (ActionEvent e) {
String command = e.getActionCommand ();
if (command.equals ("Quit")) {
fApplet.close ();
} else {
fLabelB.setText ("Lepton: " + command);
}
} // actionPerformed

/** This "helper method" makes a menu item and then
* registers this applet as a listener to it.
**/
private JMenuItem makeMenuItem (String name) {
JMenuItem m = new JMenuItem (name);
m.addActionListener (this);
return m;
} // makeMenuItem

} // class ParticleFrame




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : JFrame on applet
 JFrame properties for an Applet to be embedded in website?     -  
 Remove default window icon from JFrame     -  
 load class to applet- load frame class to applet     -  
 add applet to jsp     -  
 First Applet     -  
 Java applet     -  
 Snake applet     -  
 applet in netbeans     -  
 Applet File Permission     -  
 how to connect applet with excel     -  



Topic Tags

Java Swing, Java Applet
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com