java 如何把源碼導出為jar包,以及如何使用導出的jar包


1.新建java project,編寫源碼如下:

package baseTest;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class DrawLineFrame {
public static void main (String [] args){
JFrame window = new DrawFrame();
window.setTitle("繪制圖形");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(100,100,600,400);
window.setVisible(true);
}
}

//窗口類
class DrawFrame extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;

public DrawFrame(){

add(new DrawComponent());
pack();
}
}

//圖形類
class DrawComponent extends JComponent{
/**
*
*/
private static final long serialVersionUID = 1L;
private static final int DEAFULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 400;
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
//繪制矩形
double leftx = 100;
double topy = 100;
double width = 200;
double height = 150;
Rectangle2D rect = new Rectangle2D.Double(leftx,topy ,width,height);
g2.draw(rect);

//繪制橢圓
Ellipse2D ellipse = new Ellipse2D.Double();
ellipse.setFrame(rect);
g2.draw(ellipse);
//繪制直線
g2.draw(new Line2D.Double(leftx,topy,leftx + width ,topy + height));
//畫正圓
double centerx = rect.getCenterX();
double centery = rect.getCenterY();
double radius = 150;
Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(centerx, centery,centerx+radius,centery+radius);
g2.draw(circle);
}
}

2.運行,run as application,如果沒有什么錯誤,導出jar包。

右鍵點擊項目,export -->java --->jar files-->填寫jar包的保存路徑-->finish

3.新建新的java project,新建一個主main所在的類,

右鍵點擊項目,Build Path-->Config Build Path--->add external jar--->選擇剛才生成的jar包,---->apply-確定

在新工程中編寫如下代碼:

package testjar;
public class testjar {

public static void main(String[] args) {

baseTest.DrawLineFrame.main(args);
}

}

運行一下,就可以了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM