java 如何調用另外一個包的代碼


1.新建java project,新建一個包,如baseTest,在該包下新建類,代碼如下:

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.新建包含,main的主類,代碼如下:

package testjar;
import baseTest.DrawLineFrame;
public class testjar {

public static void main(String[] args) {

//baseTest.DrawLineFrame.main(args);//方法1:通過jar包引用函數
DrawLineFrame.main(args);//方法2:通過源碼包直接引入
}

}

3.測試,運行。


免責聲明!

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



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