ارجوا مساعدتي في كيفية عند النقر على عنصر من القائمه file يقوم البرنامج بتنفيذ كلاس موجود داخل الحزمه وليكن مثلا اله حاسبه .
البرنامج الاصلي اسمه Example1 به قائمه file عند النقر على العنصر Message1 يقوم البرنامج بتنفيد الكلاس الموجود داخل نفس الحزمه التي بها Example1
انا قمت بعمل البرنامج ولكنه لا ينفذ الاله الحاسبه فارجوا التعديل فيه واعلامي اذا كان هناك خطأ في الكود .
الكود هو:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//-------------------------
class Example1 extends JFrame
{
// Frame f = new Frame("frame");
JButton button=new JButton("خروج");
Example1()
{
Container c = getContentPane ();
Icon icon1=new ImageIcon("33.gif");
JLabel label=new JLabel();
label.setText("Welcome To Java");
label.setIcon(icon1);
JLabel label2=new JLabel();
label2.setText("Welcome To Java");
//-------------------------------------------------------
JMenuBar bar = new JMenuBar(); // JMenuBar
setJMenuBar( bar );
JMenu fileMenu = new JMenu( " file" );//meneu
fileMenu.setMnemonic( 'f' );
JMenuItem mes = new JMenuItem( "Message1" );
mes .setMnemonic( 'M' );
//--------------------------------------------------------
mes.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
if(e.getActionCommand().equals("Message1"))
{
calculator4=new calculator4();
}
}
}
);
fileMenu.add(mes );
//**************************************************************
JMenuItem mes2 = new JMenuItem( "Message2" );
mes2.setMnemonic( 'x' );
mes2.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
//draw
}
}
);
fileMenu.add( mes2 );
//***************************************************************
JMenuItem clac = new JMenuItem( "claculator" );
clac.setMnemonic( 'c' );
clac.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
//no thing
}
}
);
fileMenu.add(clac );
//****************************************************************
JMenuItem exit = new JMenuItem( "exit" );
exit.setMnemonic( 'x' );
exit.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
System.exit( 0 );
}
}
);
//*************************************************************
//************************//
fileMenu.add( exit );
//************************//
bar.add( fileMenu );
//*******************************************************************
c.add ( "North" , bar );
c.add ( "Center" ,label);
c.add ( "East" ,label2);
//c.setLocation(200,100);
setTitle ("java");
setSize ( 800 , 800 );
setVisible ( true );
}
//OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
public static void main ( String[] args )
{
Example1 app = new Example1();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e )
{
System.exit( 0 );
}
}
);
}
//OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
}
-----------------------------------------------------------------------------------
كود الاله الحاسبه هو:
/*Programmer:Shab Muslem
Date Written: 24.03.2004
Design and implement, in Java, a simple working calculator with
a friendly user interface using your choice of the Java layout managers.
This should incorporate a button for each of the numbers 0 to 9,
a button for the = sign and buttons for the +, - , / ,* operators.
A read only display area will also be needed.
The calculator must be fully functional.
Divide by zero must be trapped.
Feel free to add any extra functionality.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class calculator4 extends JFrame
{
private JTextField result; // create a JLabel
private JButton btn0n,btn1n,btn2n, btn3n,btn4n ,btn5n ,btn6n ,btnBin ,btnDecimal;
private JButton btn7n ,btn8n ,btn9n,btnPower,btnC,btnBackspace;
private JButton btnPlus ,btnMin ,btnMul ,btnD, btnEqual,btnDec ,btnsqrt,btnsqrd ,btnNeg;
double opt1, opt2;
String sign;
String num1= "";
private JPanel resultpanel, buttonpanel;
public calculator4()
{
super (" Shab Muslem calculator's ");
Container c = getContentPane ();
resultpanel = new JPanel();
result = new JTextField("0 ");
result.setBackground(Color.yellow);
result.setFont(new Font( "Serif", Font.BOLD,14));
resultpanel.add (result);
c.setLayout(new BorderLayout() );
c.add (resultpanel,BorderLayout.NORTH);
buttonpanel = new JPanel(new GridLayout (6,4) );
c.add (buttonpanel,BorderLayout.CENTER);
btn7n = new JButton("7");
buttonpanel.add (btn7n);
btn8n = new JButton("8");
buttonpanel.add (btn8n);
btn9n = new JButton("9");
buttonpanel.add (btn9n);
btnD = new JButton("/");
btnD.setBackground(Color.white);
buttonpanel.add (btnD);
btn4n = new JButton("4");
buttonpanel.add (btn4n);
btn5n = new JButton("5");
buttonpanel.add (btn5n);
btn6n = new JButton("6");
buttonpanel.add (btn6n);
btnMul = new JButton("*");
btnMul.setBackground(Color.white);
buttonpanel.add (btnMul);
btn1n = new JButton("1");
buttonpanel.add (btn1n);
btn2n = new JButton("2");
buttonpanel.add (btn2n);
btn3n = new JButton("3");
buttonpanel.add (btn3n);
btnMin = new JButton("-");
btnMin.setBackground(Color.white);
buttonpanel.add (btnMin);
btn0n = new JButton("0");
buttonpanel.add (btn0n);
btnNeg = new JButton("+/-");
btnNeg.setBackground(Color.white);
buttonpanel.add (btnNeg);
btnDec = new JButton(".");
btnDec.setBackground(Color.white);
buttonpanel.add (btnDec);
btnPlus = new JButton("+");
btnPlus.setBackground(Color.white);
buttonpanel.add (btnPlus);
btnsqrt = new JButton("square root");
btnsqrt.setBackground(Color.green);
buttonpanel.add (btnsqrt);
btnsqrd = new JButton("sqrd");
btnsqrd.setBackground(Color.green);
buttonpanel.add (btnsqrd);
btnPower = new JButton("power");
btnPower.setBackground(Color.green);
buttonpanel.add (btnPower);
btnEqual = new JButton("=");
btnEqual.setBackground(Color.cyan);
buttonpanel.add (btnEqual);
btnBin = new JButton("Decimal To Binary");
btnBin.setBackground(Color.green);
buttonpanel.add (btnBin);
btnDecimal = new JButton("Binary To Decimal");
btnDecimal.setBackground(Color.green);
buttonpanel.add (btnDecimal);
btnBackspace = new JButton("Backspace");
btnBackspace.setBackground(Color.green);
buttonpanel.add (btnBackspace);
btnC = new JButton("C");
btnC.setBackground(Color.pink);
buttonpanel.add (btnC);
ButtonHandler handler = new ButtonHandler();
btn0n.addActionListener( handler );
btn1n.addActionListener( handler );
btn2n.addActionListener( handler );
btn3n.addActionListener( handler );
btn4n.addActionListener( handler );
btn5n.addActionListener( handler );
btn6n.addActionListener( handler );
btn7n.addActionListener( handler );
btn8n.addActionListener( handler );
btn9n.addActionListener( handler );
btnDec.addActionListener( handler );
btnNeg.addActionListener( handler );
btnC.addActionListener( handler );
OperatorButtonHandler handleOperator = new OperatorButtonHandler();
btnD.addActionListener( handleOperator );
btnMul.addActionListener( handleOperator );
btnMin.addActionListener( handleOperator );
btnPlus.addActionListener( handleOperator );
btnsqrt.addActionListener( handleOperator );
btnsqrd.addActionListener( handleOperator );
btnPower.addActionListener( handleOperator );
btnBackspace.addActionListener( handleOperator );
btnBin.addActionListener( handleOperator );
btnDecimal.addActionListener( handleOperator );
OperatorButtonHandler2 handleOperator2 = new OperatorButtonHandler2();
btnEqual.addActionListener( handleOperator2 );
setSize ( 560,310 );
show();
}
public static void main (String args[])
{
calculator4 app = new calculator4();
app.addWindowListener( new WindowAdapter ()
{public void windoClosing ( WindowEvent e ){System.exit (0);}
} );
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();
if (str.equals("0") )
{
num1=num1+ "0";
result.setText(""+ num1);
}
else if (str.equals("1") )
{
num1=num1+ "1";
result.setText(""+ num1);
}
else if (str.equals("2") )
{
num1=num1+ "2";
result.setText(""+ num1);
}
else if (str.equals("3") )
{
num1=num1+ "3";
result.setText(""+ num1);
}
else if (str.equals("4") )
{
num1=num1+ "4";
result.setText(""+ num1);
}
else if (str.equals("5") )
{
num1=num1+ "5";
result.setText(""+ num1);
}
else if (str.equals("6") )
{
num1=num1+ "6";
result.setText(""+ num1);
}
else if (str.equals("7") )
{
num1=num1+ "7";
result.setText(""+ num1);
}
else if (str.equals("8") )
{
num1=num1+ "8";
result.setText(""+ num1);
}
else if (str.equals("9") )
{
num1=num1+ "9";
result.setText(""+ num1);
}
else if (str.equals(".") )
{
num1=num1+ ".";
result.setText(""+ num1);
}
else if (str.equals("+/-") )
{
num1=num1+ "-";
result.setText(""+ num1);
}
else if (str.equals("power") )
{
num1=num1+ "power";
result.setText(""+ num1);
}
else if (str.equals("C") )
{
num1="";
result.setText("0");
}
}
}
private class OperatorButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
String str1 = ae.getActionCommand();
if (str1.equals("+") )
{
opt1= Double.parseDouble(num1);
result.setText( "+");
sign = "+";
num1="";
}
else if (str1.equals("-") )
{
opt1=Double.parseDouble(num1);
result.setText( "-");
sign = "-";
num1="";
}
else if (str1.equals("*") )
{
opt1=Double.parseDouble(num1);
result.setText( "*");
sign = "*";
num1="";
}
else if (str1.equals("/") )
{
opt1=Double.parseDouble(num1);
result.setText( "/");
sign = "/";
num1="";
}
else if (str1.equals("square root") )
{
opt1=Double.parseDouble(num1);
double a =Math.sqrt( opt1 );
result.setText( ""+a);
num1="";
}
else if (str1.equals("sqrd") )
{
opt1=Double.parseDouble(num1);
double a = opt1*opt1;
result.setText( ""+a);
num1="";
}
else if (str1.equals("power") )
{
opt1=Double.parseDouble(num1);
result.setText( "power");
sign = "power";
num1="";
}
else if (str1.equals("Backspace") )
{
if (num1.length() > 1)
{
num1=num1.substring(0,num1.length()-1);
result.setText( ""+num1);
}
else
{
num1="";
result.setText("0");
}
}
else if (str1.equals("Decimal To Binary") )
{
String m="";
double b =Double.parseDouble(num1);
int a = (int)(B);
int number = a;
if(b < 1)
{
result.setText( "The decimal number "+b+ " smaller than one. Please Re-Enter number larger than one. " );
num1="";
}
else
{
while (a > 0)
{
if ( a%2==0)
{
a = a/2;
m= "0" +m;
}
else
{
a = a/2;
m ="1"+ m;
}
}
result.setText( "The decimal number "+number+ " = " +m+ " in Binary ");
num1="";
}
}
else if (str1.equals("Binary To Decimal") )
{
String theNum1 = num1;
int a =Integer.parseInt(num1);
int i,sum=0,count, m;
boolean isNotB = false;
int lengthNum =num1.length();
while( num1.length() !=0)
{
String ferstNum = num1.substring(0,1);
int Nub =Integer.parseInt(ferstNum);
if(num1.length() !=0)
num1=num1.substring(1,num1.length());
if(Nub !=0 && Nub !=1)
isNotB = true;
}
if(isNotB == true)
result.setText( " This number " +a+ " is not binary number. Please Re-Enter binary number " );
else
{
count = theNum1.length();
for ( i=(count);i>=0;i--)
{
m =a/ (int)(Math.pow(10,i));
if (m%2 ==1)
sum += Math.pow(2,i);
}
result.setText( "The binary number "+a+ " = "+ +sum+ " in Decimal ");
num1="";
}
}
}
}
private class OperatorButtonHandler2 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
String str2 = ae.getActionCommand();
opt2= Double.parseDouble(num1);
if ( sign.equals("+"))
{
double a = opt1 + opt2;
result.setText( " "+ opt1+" " +sign+" "+ opt2+" = "+ a );
num1="";
}
else if ( sign.equals("-"))
{
double a = opt1 - opt2;
result.setText( " "+ opt1+" " +sign+" "+ opt2+" = "+ a );
num1="";
}
else if ( sign.equals("*"))
{
double a = opt1 * opt2;
result.setText( " "+ opt1+" " +sign+" "+ opt2+" = "+ a );
num1="";
}
else if ( sign.equals("power"))
{
double a = Math.pow(opt1, opt2);
result.setText( " "+ opt1+" " +sign+" "+ opt2+" = "+ a );
num1="";
}
else if ( sign.equals("/"))
{
if ( opt2 == 0)
{
result.setText("Cannot divide by zero.");
num1="";
}
else
{
double a = opt1 / opt2;
result.setText( " "+ opt1+" " +sign+" "+ opt2+" = "+ a );
num1="";
}
}
}
}
}-----------------------------------------------------------------
ارجوا الرد اليوم في اسرع وقت ممكن