方法1:
成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務,包含不限于網(wǎng)站設計制作、成都網(wǎng)站建設、華亭網(wǎng)絡推廣、成都微信小程序、華亭網(wǎng)絡營銷、華亭企業(yè)策劃、華亭品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供華亭建站搭建服務,24小時服務熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com
public?static?void?main(String[]?args)?{
//JPanel??p?=?new?JPanel();?注釋掉這句
JFrame?frame?=?new?JFrame("DrawLine");
frame.add(new?DrawLine());//將p對象換成本類
//因為本類繼承了JPanel重寫paintComponent進行繪制,是繪制到本類的panel上的,
//而不是繪制到new?Panel()對象
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,?200);
frame.setVisible(true);
}
protected?void?paintComponent(Graphics?g)?{
super.paintComponent(g);
g.drawLine(50,?50,?200,?250);
}
方法2:
//這種方式可能?讓你更理解
public?static?void?main(String[]?args)?{
DrawLine?dl?=?new?DrawLine();//新建對象
dl.init();//執(zhí)行初始化
}
private?void?init(){
//JPanel??p?=?new?JPanel();?注釋掉這句
JFrame?frame?=?new?JFrame("DrawLine");
frame.add(this);//將p對象換成本類
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,?200);
frame.setVisible(true);
}
protected?void?paintComponent(Graphics?g)?{
super.paintComponent(g);
g.drawLine(50,?50,?200,?250);
}
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;public class DrawLine extends Frame
implements WindowListener
{
static ArrayList lbArray; DrawLine(){
setBounds(150,150,300,300);
setVisible(true);
Label lb0=new Label("Hello");
addWindowListener(this);
add(lb0);
lb0.setBounds(50,30,40,20);
Label lb1=new Label("World!");
add(lb1);
lb1.setBounds(50,90,40,20); Label lb2=new Label("Java");
add(lb2);
lb2.setBounds(50,150,40,20);
lbArray.add(lb0);
lbArray.add(lb1);
lbArray.add(lb2);
repaint(); }
public void windowClosed(WindowEvent w)
{
System.exit(0);
}
public void windowClosing(WindowEvent w)
{
dispose();
}
public void windowOpened(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void paint(Graphics g)
{
super.paint(g); if(lbArray.size()=1)
{
g.setColor(Color.red);
int x1,y1,x2,y2;
Label lb=(Label)lbArray.get(0);
Rectangle rc=lb.getBounds();
x1=rc.x+rc.width/2;
y1=rc.y+rc.height/2;
for(int i=1;ilbArray.size();i++)
{
lb=(Label)lbArray.get(i);
rc=lb.getBounds();
x2=rc.x+rc.width/2;
y2=rc.y+rc.height/2;
g.drawLine(x1,y1,x2,y2);
x1=x2;
y1=y2;
}
}
} public static void main(String[] args){
lbArray=new ArrayList(10);
new DrawLine();
}
} 終于做了個滿足你要求的東西。要知道我昨天和你說過我并不常用Java。趕快采納吧。
這個簡單
可以調(diào)用方法drawline(int x1, int y1, int x2, int y2)
其中(x1, y1), (x2, y2)分別為直線起點和終點的坐標
特意給你寫了個小例子,希望能幫到你
***************************************
import javax.swing.*;
import java.awt.*;
public class DrawLine extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("DrawLine");
frame.getContentPane().add(new DrawLine());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(50, 50, 200, 250);
}
}
****************************
將程序復制到記事本中并改名為DrawLine.java(注意大小寫)
然后在命令行中用 CD+文件夾 使命令范圍到DrawLine.java的文件夾中
然后執(zhí)行命令javac DrawLine.java
再執(zhí)行命令java DrawLine
你會看到結果
定義一個類繼承JPanel類,重寫 JPanel 的 paint 方法,使用 Graphics 繪制線段,如下代碼:
import?java.awt.BorderLayout;
import?java.awt.Color;
import?java.awt.Graphics;
import?javax.swing.JFrame;
import?javax.swing.JPanel;
class?MyPanel?extends?JPanel?{
@Override
public?void?paint(Graphics?g)?{
super.paint(g);
g.setColor(Color.RED);
for?(int?i?=?0;?i??10;?i++)?{
g.drawLine(10,?10?+?i?*?20,?this.getWidth()?-?10,?10?+?i?*?20);
}
for?(int?i?=?0;?i??10;?i++)?{
g.drawLine(10?+?i?*?20,?10,?10?+?i?*?20,?this.getHeight()?-?10);
}
}
}
public?class?App?extends?JFrame?{
public?App()?{
this.setSize(400,?400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyPanel?panel?=?new?MyPanel();
this.add(panel,?BorderLayout.CENTER);
}
public?static?void?main(String[]?args)?{
new?App().setVisible(true);
}
}
運行結果: