此代码在打包在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 );
}
}
这是我将此代码打包到JAR文件中的步骤 - 右键单击Project→Export→Java→JAR文件 →选择选项导出生成的类文件和资源, 导出Java源文件和资源。 我得到一个警告:带有编译警告的导出:Project 01 / src / Calculator.java 然后文件Calculator.jar在打包完成后无法运行。
您能否告诉我如何解决此错误?
答案 0 :(得分:3)
您需要导出为可运行的jar文件,如下所示:
右键单击Project→Export→Java→Runnable JAR文件
确保在运行配置中选择Calculator,以便它知道哪个类具有main方法。