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

來源:互聯網
上載者:User

標籤:exit   如何   dea   port   lin   import   version   draw   stat   

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.測試,運行。

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.