Java 此代码可以';当它打包在jar文件中时,它不会运行

Java 此代码可以';当它打包在jar文件中时,它不会运行,java,calculator,Java,Calculator,此代码在打包到jar文件中时无法运行,但在Eclipse中构建时运行正常 /* Calculator */ import java.awt.*; import java.awt.event.*; import java.lang.Math; import javax.swing.*; class Calculator extends Frame implements ActionListener { Font font1 = new Font( "Times New Roman",

此代码在打包到jar文件中时无法运行,但在Eclipse中构建时运行正常

/* Calculator */
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
import javax.swing.*;
class Calculator extends Frame implements ActionListener  {
    Font font1 = new Font( "Times New Roman",  Font.PLAIN,  20 );
    JLabel  Label1,  Label2,  Label3;
    JTextField  txt1,  txt2,  txt3;
    // 4 JButton
    JButton  Add = new JButton( "  +  " );
    JButton  Sub = new JButton( "  -  " );
    JButton  Mul = new JButton( "  x  " );
    JButton  Div = new JButton( "  /  " );
    JButton  Power = new JButton( "  ^  " );
    JButton  Sin = new JButton( "  Sin  " );
    JButton  Cos = new JButton( "  Cos  " );
    JButton  Tan = new JButton( "  Tan  " );
    JButton  Arcsin = new JButton( "  Arcsin  " );
    JButton  Arccos = new JButton( "  Arccos  " );
    JButton  Arctan = new JButton( "  Arctan  " );
    JButton  Factorial = new JButton( "  Factorial  " );
    JButton  Combinatory = new JButton( "  Combinatory  " );

    // 2 Panels will contain components
    Panel  p1 = new Panel( new GridLayout( 3, 2 ) );
    Panel  p2 = new Panel( new GridLayout( 4, 4 ) );

/* Calculator( )  */
    Calculator( )  {
        super ( "Calculator" );
        Label1 = new JLabel ( "First number: ", Label.LEFT );
        Label2 = new JLabel ( "Second number: ", Label.LEFT );
        Label3 = new JLabel ( "Result: ", Label.LEFT );
        txt1 = new JTextField( );  txt2 = new JTextField( );   txt3 = new JTextField( ); 
        txt1.setFont( font1 );  txt2.setFont( font1 );   txt3.setFont( font1 );
        txt1.setPreferredSize( new Dimension ( 100, 20 ) );
        txt2.setPreferredSize( new Dimension ( 100, 20 ) );
        txt3.setPreferredSize( new Dimension ( 100, 20 ) );

        Label1.setFont( font1 );  Label2.setFont( font1 );  
        Label3.setFont( font1 );  
        // Adding lables and textbox to panel p1
        p1.setMaximumSize( new Dimension( 3*100,  2*200 ) );
        p2.setMaximumSize( new Dimension( 4*100,  4*100 ) );
        p1.add( Label1 );  p1.add( txt1 );
        p1.add( Label2 );  p1.add( txt2 );
        p1.add( Label3 );  p1.add( txt3 );
        // Adding 4 JButtons to panel p2
        p2.add( Add );  p2.add( Sub );  p2.add( Mul );  p2.add( Div );
        p2.add( Power );  p2.add( Factorial );  p2.add( Combinatory ); 
        p2.add( Sin );  p2.add( Cos );  p2.add( Tan );  
        p2.add( Arcsin );  p2.add( Arccos );  p2.add( Arctan );  

        // set layout of this frame is FlowLayout
        this.setLayout( new FlowLayout( ) );
        // Adding 2 panels to this frame
        this.add( p1 );  this.add( p2 );
        Add.addActionListener( this );  Sub.addActionListener( this );
        Mul.addActionListener( this );  Div.addActionListener( this );
        Power.addActionListener( this );  Factorial.addActionListener( this );
        Sin.addActionListener( this );  Cos.addActionListener( this );
        Tan.addActionListener( this );  Arcsin.addActionListener( this );
        Arccos.addActionListener( this );  Arctan.addActionListener( this );
        Combinatory.addActionListener( this );

      // Managing window closing event
        addWindowListener ( new WindowAdapter( )  {
            public void windowClosing( WindowEvent event )  { System.exit( 0 ); }
        } );  
    /* Add close JButton or we can use:         
        setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE ); in constructor Calculator( )  */
    }
    public long Factorial ( long n )  {
        int i, n1;  n1 = 1;
        for ( i = 1; i <= n; i++ )
            n1 = n1*i;
        return n1; 
    }


/* public void actionPerformed ( ActionEvent e ) */
    public void actionPerformed ( ActionEvent e ) {
        /* Method will be automatic called when ActionListener receive action
            from the listened objects */
        double k3;  double PI = 3.141592654;
        //  Convert inputted content into number data
        double k1 = Double.parseDouble ( txt1.getText( ) );  
        double k2 = Double.parseDouble ( txt2.getText( ) );  
        String s1, s2, s3, s4;  s1 = txt1.getText( );  s2 = txt2.getText( );  
        if ( e.getSource( ) == Add )  {  
            // If event source is JButton Add
            k3 = k1 + k2;  s3 = Double.toString( k3 );
            s4 = s1 + " + " + s2 + " = " + s3;
            txt3.setText( s4  ); 
            /* txt3, s4 = Result */
        } 
        if ( e.getSource( ) == Sub )  {  
            k3 = k1 - k2;  s3 = Double.toString( k3 );
            s4 = s1 + " - " + s2 + " = " + s3;
            txt3.setText( s4  ); 
        } 
        if ( e.getSource(   ) == Mul )  {  
            k3 = k1 * k2;  s3 = Double.toString( k3 );
            s4 = s1 + " * " + s2 + " = " + s3;
            txt3.setText( s4  ); 
        } 
        if ( e.getSource( ) == Div )  {  
            k3 = k1 / k2;  s3 = Double.toString( k3 );
            s4 = s1 + " / " + s2 + " = " + s3;
            txt3.setText( s4  ); 
        } 
        if ( e.getSource( ) == Power )  {  
            k3 = Math.exp( Math.log( k1 ) * k2 );  s3 = Double.toString( k3 );
            s4 = s1 + " ^ " + s2 + " = " + s3;
            txt3.setText( s4  ); 
        } 
        if ( e.getSource( ) == Factorial )  {
            long  n, n1;  n = ( long ) k1;  
            n1 = Factorial( n );
            s3 = Long.toString( n1 );  s1 = Long.toString( n );
            s4 = s1 + "! = " + s3;
            txt3.setText( s4  ); 
        } 
        if ( e.getSource( ) == Sin )  {
            k1 = k1*PI/180;  
            k3 = Math.sin( k1 );  s3 = Double.toString( k3 );
            s4 = "Sin( " + s1 + " ) = " + s3;
            txt3.setText( s4  );  
        } 
        if ( e.getSource( ) == Cos )  { 
            k1 = k1*PI/180;   
            k3 = Math.cos( k1 );  s3 = Double.toString( k3 );
            s4 = "Cos( " + s1 + " ) = " + s3;
            txt3.setText( s4  );  
        } 
        if ( e.getSource( ) == Tan )  { 
            k1 = k1*PI/180;   
            k3 = Math.tan( k1 );  s3 = Double.toString( k3 );
            s4 = "Tan( " + s1 + " ) = " + s3;
            txt3.setText( s4  );  
        } 
        if ( e.getSource( ) == Arcsin )  { 
            k3 = Math.asin( k1 );  k3 = k3*180/PI; 
            s3 = Double.toString( k3 );
            s4 = "Arcsin( " + s1 + " ) = " + s3;
            txt3.setText( s4  );  
        } 
        if ( e.getSource( ) == Arccos )  {  
            k3 = Math.acos( k1 );  k3 = k3*180/PI; 
            s3 = Double.toString( k3 );
            s4 = "Arccos( " + s1 + " ) = " + s3;
            txt3.setText( s4  );  
        } 
        if ( e.getSource( ) == Arctan )  {   
            k3 = Math.atan( k1 );  k3 = k3*180/PI; 
            s3 = Double.toString( k3 );
            s4 = "Arctan( " + s1 + " ) = " + s3;
            txt3.setText( s4  );  
        } 
        if ( e.getSource( ) == Combinatory )  {   
            long n1, n2, n;  n1 = ( long ) k1;  n2 = ( long ) k2; 
            n = Factorial( n2 ) / ( Factorial( n1 ) * Factorial( n2 - n1 )  );
            s1 = Long.toString( n1 );  s2 = Long.toString( n2 );  
            s3 = Long.toString( n ); 
            s4 = "C( " + s1 + ", " + s2 + " ) = " + s3;
            txt3.setText( s4  );  
        } 
    }
    public static void main( String args[] )  {
        Calculator f = new Calculator( );
        f.setSize( 500, 500 );
        f.setVisible( true );
    }
}
/*计算器*/
导入java.awt.*;
导入java.awt.event.*;
导入java.lang.Math;
导入javax.swing.*;
类计算器扩展框架实现ActionListener{
Font font1=新字体(“Times new Roman”,Font.PLAIN,20);
JLabel标签1、标签2、标签3;
JTextField txt1、txt2、txt3;
//4按钮
JButton Add=新JButton(“+”);
JButton Sub=新JButton(“-”);
JButton Mul=新JButton(“x”);
JButton Div=新JButton(“/”);
JButton Power=新JButton(“^”);
JButton Sin=新JButton(“Sin”);
JButton Cos=新JButton(“Cos”);
JButton Tan=新JButton(“Tan”);
JButton arcin=新JButton(“arcin”);
JButton Arccos=新JButton(“Arccos”);
JButton-Arctan=新JButton(“Arctan”);
JButton阶乘=新JButton(“阶乘”);
JButton组合=新JButton(“组合”);
//2个面板将包含组件
面板p1=新面板(新网格布局(3,2));
面板p2=新面板(新网格布局(4,4));
/*计算器()*/
计算器(){
超级(“计算器”);
Label1=新的JLabel(“第一个数字:”,Label.LEFT);
Label2=新的JLabel(“第二个数字:”,Label.LEFT);
Label3=新的JLabel(“结果:”,Label.LEFT);
txt1=新JTextField();txt2=新JTextField();txt3=新JTextField();
txt1.setFont(font1);txt2.setFont(font1);txt3.setFont(font1);
txt1.设置首选尺寸(新尺寸(100,20));
txt2.设置首选尺寸(新尺寸(100,20));
txt3.设置首选尺寸(新尺寸(100,20));
Label1.setFont(font1);Label2.setFont(font1);
Label3.setFont(font1);
//将标签和文本框添加到面板p1
p1.设置最大尺寸(新尺寸(3*100,2*200));
p2.设置最大尺寸(新尺寸(4*100,4*100));
p1.添加(标签1);p1.添加(txt1);
p1.添加(标签2);p1.添加(txt2);
p1.添加(标签3);p1.添加(txt3);
//将4个按钮添加到面板p2
p2.添加(添加);p2.添加(Sub);p2.添加(Mul);p2.添加(Div);
p2.加法(幂);p2.加法(阶乘);p2.加法(组合);
p2.添加(Sin);p2.添加(Cos);p2.添加(Tan);
p2.添加(Arcsin);p2.添加(Arccos);p2.添加(Arctan);
//此框架的设置布局为FlowLayout
这个.setLayout(新的FlowLayout());
//将2个面板添加到此框架
this.add(p1);this.add(p2);
Add.addActionListener(此);Sub.addActionListener(此);
Mul.addActionListener(此);Div.addActionListener(此);
Power.addActionListener(this);Factorial.addActionListener(this);
Sin.addActionListener(this);Cos.addActionListener(this);
Tan.addActionListener(此);arcin.addActionListener(此);
Arccos.addActionListener(此);Arctan.addActionListener(此);
addActionListener(this);
//管理窗口关闭事件
addWindowListener(新的WindowAdapter(){
public void windowClosing(WindowEvent事件){System.exit(0);}
} );  
/*添加close JButton或我们可以使用:
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);在构造函数计算器()中*/
}
公共长阶乘(长n){
int i,n1;n1=1;

对于(i=1;i您需要导出为可运行的jar文件,如下所示:

右键单击项目→ 出口→ JAVA→ 可运行JAR文件


确保在运行配置中选择Calculator,以便它知道哪个类有main方法。

您需要导出为可运行的jar文件,如下所示:

右键单击项目→ 出口→ JAVA→ 可运行JAR文件


确保在运行配置中选择Calculator,以便它知道哪个类有main方法。

您需要导出为可运行的jar文件,如下所示:

右键单击项目→ 出口→ JAVA→ 可运行JAR文件


确保在运行配置中选择Calculator,以便它知道哪个类有main方法。

您需要导出为可运行的jar文件,如下所示:

右键单击项目→ 出口→ JAVA→ 可运行JAR文件



确保在运行配置中选择Calculator,以便它知道哪个类有main方法。

会出现什么错误?清单中是否设置了
main类
?是否确定导出为可运行的jar,而不仅仅是jar?提示:Frame应该是JFrame,可以使用Math.PI。会出现什么错误?是否有Main类
?您确定要作为可运行的jar而不仅仅是jar导出吗?提示:Frame应该是JFrame,可以使用Math.PI。会出现什么错误?清单中是否设置了
Main类
?您确定要作为可运行的jar而不仅仅是jar导出吗?提示:Frame应该是JFrame,oNE可以使用Ma.pI.你的错误是什么?在你的清单中有一个<代码>主类< /代码>吗?你确定你是作为一个可运行的罐子导出的吗?而不仅仅是一个JAR提示:框架应该是一个JFrice,一个可以使用Ma.Pi.考虑通过点击左边的绿色记号来接受这个答案,如果你发现它有用的话,考虑接受C的这个答案。单击左侧的绿色勾号(如果觉得有用)考虑通过单击左侧的绿色勾号接受此答案(如果觉得有用)考虑通过单击左侧的绿色勾号接受此答案(如果觉得有用)