java简单的画线程序
JFrame里有一个JLabel对象l想每在JFrame里添加组件时实现画一条线把组件和l连上...
JFrame里有一个JLabel对象l想每在JFrame里添加组件时实现画一条线把组件和l连上
展开
2013-09-13
展开全部
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;i<lbArray.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。赶快采纳吧。
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;i<lbArray.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。赶快采纳吧。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |