Hello everyone,
I am writing a program that is for a programming class..This program has to display syntax when the appropriate button is pushed..it also has a menu bar that must be functional. It must also run as an applet and as an application.
The above is complete and my program functions as expected with a few minor problems...This is interesting...When I run it from Eclipse3.0 as an applet and use the exit function from the menu bar it exits with no problem..but, when I run it from the command line as an applet it will not exit properly. Also when I run it as an applet all the Syntax Buttons work without generating exceptions. If I run it as an application all the Syntax Buttons generate exceptions..???But yet it still works as expected...I am posting the code for anyone who may be able to help me..I am not asking for you to do it for me as you will see I have tried multiple ways of fixing it..I have commented out things that did not work.any help would be greatly appreciated.
ZEEPLE
/**
* Zeeple
*
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JApplet;
//import java.applet.Applet;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JTextArea;
public class MenuTest extends JApplet implements ActionListener{
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private JPanel pnPanel0;
private JPanel pnPanel2;
private JButton btBut0;
private JButton btBut1;
private JButton btBut2;
private JPanel pnPanel3;
private JButton btBut3;
private JButton btBut4;
private JButton btBut5;
private JTextArea outputArea;
private JLabel displayLabel;
private ButtonGroup fontGroup, colorGroup;
// private int style;
// set up GUI
public MenuTest()
{
// super( "Using JMenus" );
// set up File menu and its menu items
JMenu fileMenu = new JMenu( "File" );
fileMenu.setMnemonic( 'F' );
// set up About... menu item
JMenuItem helpItem = new JMenuItem( "Help..." );
helpItem.setMnemonic( 'H' );
fileMenu.add( helpItem );
helpItem.addActionListener(
new ActionListener() { // anonymous inner class
// display message dialog when user selects About...
public void actionPerformed( ActionEvent event )
{
JOptionPane.showMessageDialog( MenuTest.this,
"This is my final project..Ver1.0..CSC211",
"Help", JOptionPane.PLAIN_MESSAGE );
}
} // end anonymous inner class
); // end call to addActionListener
// set up Exit menu item
JMenuItem exitItem = new JMenuItem( "Exit" );
exitItem.setMnemonic( 'x' );
fileMenu.add( exitItem );
exitItem.addActionListener(
new ActionListener() { // anonymous inner class
// terminate application when user clicks exitItem
public void actionPerformed( ActionEvent event )
{
System.exit( 0 );
}
} // end anonymous inner class
); // end call to addActionListener
// create menu bar and attach it to MenuTest window
JMenuBar bar = new JMenuBar();
setJMenuBar( bar );
bar.add( fileMenu );
pnPanel0 = new JPanel();
GridBagLayout gbPanel0 = new GridBagLayout();
GridBagConstraints gbcPanel0 = new GridBagConstraints();
pnPanel0.setLayout( gbPanel0 );
pnPanel2 = new JPanel();
pnPanel2.setBorder( BorderFactory.createTitledBorder( "Selection" ) );
pnPanel2.setBackground(Color.BLUE);
GridBagLayout gbPanel2 = new GridBagLayout();
GridBagConstraints gbcPanel2 = new GridBagConstraints();
pnPanel2.setLayout( gbPanel2 );
btBut0 = new JButton( "IF" );
btBut0.setForeground(Color.BLUE);
btBut0.addActionListener(this);
/* new ActionListener(){
public void actionPerformed(ActionEvent event){
// if(event.getSource() == btBut0)
String output="";
output += "if (condition) {" + "\nstatements;" + "\n};";
outputArea.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 18));
outputArea.setBackground(Color.BLUE) ;
outputArea.setText( output );
showStatus("You clicked the If button..");
}
}
);
*/
gbcPanel2.gridx = 2;
gbcPanel2.gridy = 1;
gbcPanel2.gridwidth = 13;
gbcPanel2.gridheight = 4;
gbcPanel2.fill = GridBagConstraints.BOTH;
gbcPanel2.weightx = 1;
gbcPanel2.weighty = 0;
gbcPanel2.anchor = GridBagConstraints.NORTH;
gbPanel2.setConstraints( btBut0, gbcPanel2 );
pnPanel2.add( btBut0 );
btBut1 = new JButton( "IF...ELSE" );
btBut1.setForeground(Color.BLUE);
btBut1.addActionListener(this);
gbcPanel2.gridx = 2;
gbcPanel2.gridy = 7;
gbcPanel2.gridwidth = 13;
gbcPanel2.gridheight = 4;
gbcPanel2.fill = GridBagConstraints.BOTH;
gbcPanel2.weightx = 1;
gbcPanel2.weighty = 0;
gbcPanel2.anchor = GridBagConstraints.NORTH;
gbPanel2.setConstraints( btBut1, gbcPanel2 );
pnPanel2.add( btBut1 );
btBut2 = new JButton( "SWITCH" );
btBut2.setForeground(Color.BLUE);
btBut2.addActionListener(this);
gbcPanel2.gridx = 2;
gbcPanel2.gridy = 13;
gbcPanel2.gridwidth = 13;
gbcPanel2.gridheight = 4;
gbcPanel2.fill = GridBagConstraints.BOTH;
gbcPanel2.weightx = 1;
gbcPanel2.weighty = 0;
gbcPanel2.anchor = GridBagConstraints.NORTH;
gbPanel2.setConstraints( btBut2, gbcPanel2 );
pnPanel2.add( btBut2 );
gbcPanel0.gridx = 1;
gbcPanel0.gridy = 4;
gbcPanel0.gridwidth = 17;
gbcPanel0.gridheight = 19;
gbcPanel0.fill = GridBagConstraints.BOTH;
gbcPanel0.weightx = 1;
gbcPanel0.weighty = 0;
gbcPanel0.anchor = GridBagConstraints.NORTH;
gbPanel0.setConstraints( pnPanel2, gbcPanel0 );
pnPanel0.add( pnPanel2 );
pnPanel3 = new JPanel();
pnPanel3.setBorder( BorderFactory.createTitledBorder( "Repetition" ) );
pnPanel3.setBackground(Color.RED);
GridBagLayout gbPanel3 = new GridBagLayout();
GridBagConstraints gbcPanel3 = new GridBagConstraints();
pnPanel3.setLayout( gbPanel3 );
btBut3 = new JButton( "WHILE" );
btBut3.setForeground(Color.RED);
btBut3.addActionListener(this);
gbcPanel3.gridx = 2;
gbcPanel3.gridy = 1;
gbcPanel3.gridwidth = 14;
gbcPanel3.gridheight = 4;
gbcPanel3.fill = GridBagConstraints.BOTH;
gbcPanel3.weightx = 1;
gbcPanel3.weighty = 0;
gbcPanel3.anchor = GridBagConstraints.NORTH;
gbPanel3.setConstraints( btBut3, gbcPanel3 );
pnPanel3.add( btBut3 );
btBut4 = new JButton( "DO..WHILE" );
btBut4.setForeground(Color.RED);
btBut4.addActionListener(this);
gbcPanel3.gridx = 2;
gbcPanel3.gridy = 7;
gbcPanel3.gridwidth = 14;
gbcPanel3.gridheight = 4;
gbcPanel3.fill = GridBagConstraints.BOTH;
gbcPanel3.weightx = 1;
gbcPanel3.weighty = 0;
gbcPanel3.anchor = GridBagConstraints.NORTH;
gbPanel3.setConstraints( btBut4, gbcPanel3 );
pnPanel3.add( btBut4 );
btBut5 = new JButton( "FOR" );
btBut5.setForeground(Color.RED);
btBut5.addActionListener(this);
gbcPanel3.gridx = 2;
gbcPanel3.gridy = 13;
gbcPanel3.gridwidth = 14;
gbcPanel3.gridheight = 4;
gbcPanel3.fill = GridBagConstraints.BOTH;
gbcPanel3.weightx = 1;
gbcPanel3.weighty = 0;
gbcPanel3.anchor = GridBagConstraints.NORTH;
gbPanel3.setConstraints( btBut5, gbcPanel3 );
pnPanel3.add( btBut5 );
gbcPanel0.gridx = 21;
gbcPanel0.gridy = 4;
gbcPanel0.gridwidth = 18;
gbcPanel0.gridheight = 19;
gbcPanel0.fill = GridBagConstraints.BOTH;
gbcPanel0.weightx = 1;
gbcPanel0.weighty = 0;
gbcPanel0.anchor = GridBagConstraints.NORTH;
gbPanel0.setConstraints( pnPanel3, gbcPanel0 );
pnPanel0.add( pnPanel3 );
outputArea = new JTextArea(2,10);
gbcPanel0.gridx = 1;
gbcPanel0.gridy = 25;
gbcPanel0.gridwidth = 38;
gbcPanel0.gridheight = 14;
gbcPanel0.fill = GridBagConstraints.BOTH;
gbcPanel0.weightx = 1;
gbcPanel0.weighty = 1;
gbcPanel0.anchor = GridBagConstraints.NORTH;
gbPanel0.setConstraints( outputArea, gbcPanel0 );
pnPanel0.add( outputArea );
setContentPane( pnPanel0 );
// pack();
setVisible( true );
} // end constructor GridBagDemo
public void actionPerformed(ActionEvent event){
if(event.getSource() == btBut0)
{String output="";
output += "if (condition) {" + "\nstatements;" + "\n};";
outputArea.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 18));
outputArea.setBackground(Color.BLUE) ;
outputArea.setText( output );
showStatus("You clicked the If button..");
}
else if(event.getSource() == btBut4)
{ String output="";
output += "do {" + "\nstatements;"+ "\n} while (condition);";
outputArea.setFont(new Font("Serif", Font.BOLD, 18));
outputArea.setBackground(Color.RED) ;
outputArea.setText( output );
showStatus("You clicked the Do while button..");
} //end if
else if(event.getSource() == btBut1)
{String output="";
output += "if (condition) {" + "\nstatements;" + "\n} else {" + "\nstatements;" + "\n};";
outputArea.setFont(new Font("SansSerif", Font.PLAIN, 14));
outputArea.setBackground(Color.BLUE) ;
outputArea.setText( output );
showStatus("You clicked the If..else button..");
}//end if
else if(event.getSource() == btBut3)
{String output="";
output += "while (condition) {"+ "\nstatements;" + "\n};";
outputArea.setFont(new Font("Monospaced", Font.ITALIC, 12));
outputArea.setBackground(Color.RED) ;
outputArea.setText( output );
showStatus("You clicked the While button..");
} //end if
else if(event.getSource() == btBut2)
{ String output="";
output += "switch (condition) {" + "\ncase ABC:" + "\nstatements:" + "\n/*falls through" +
"\ncase DEF:" + "\nstatements;" + "\nbreak;" + "\ncase XYZ:" +
"\nstatements;" + "\nbreak;" + "\ndefault:" + "\nstaments;" + "\nbreak;" + "\n};";
outputArea.setFont(new Font("Serif", Font.ITALIC, 16));
outputArea.setBackground(Color.GREEN) ;
outputArea.setText( output );
showStatus("You clicked the Switch button..");
} //end if
else if(event.getSource() == btBut5)
{String output="";
output += "for (initialization; condition;" + "\nupdate)" + " {" + "\nstatements;" +
"\n};";
outputArea.setFont(new Font("Aerial", Font.BOLD + Font.ITALIC, 14));
outputArea.setBackground(Color.RED) ;
outputArea.setText( output );
showStatus("You clicked the For button..");
}//end if
}//end method action performed
public static void main( String args[] )
{Frame f = new Frame(); //Create a new frame object
MenuTest menuTest = new MenuTest(); //Create an instance of DrawHouse
f.add( menuTest );
f.setSize(230,440);
f.setVisible(true);
//An "anonymous" inner class used to close the window
f.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}///end window closing method
}// end window adapter method
);// end parameter argument
menuTest.init(); //Call the applet's init method
} //End of main
} // end class MenuTest
:cry: :cry: :-|