一个头痛的问题,各位帮个忙,谢谢!!

myjava2006 2005-11-17 11:36:32
我想通过点击按钮来绘直线,怎么不行呀??
程序如下::
import java.awt.*;
import java.awt.event.*;
public class button1 {
public static void main(String[] args) {
Frame th=new Frame("按钮事件");
th.add(new mybutton());
th.setLayout(new BorderLayout());
Button b1=new Button("开始");
Button b2=new Button("结束");
th.add(b1,"North");
th.add(b2,"South");
actioner aa=new actioner();
b1.addActionListener(aa);
th.addWindowListener(new myclose());
th.setSize(400,400);
th.setVisible(true);
}
}
class myclose extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
class actioner implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("开始")){
Frame rh=new Frame();
drawlinep p = new drawlinep() ;
p.init();
rh.add(p);
}
else {
System.exit(0);
}
}
}
class drawlinep extends Panel
{
int orgx;
int orgy;
int endx;
int endy;

public void paint(Graphics g)
{
g.setColor(Color.BLUE);
g.drawLine(orgx,orgy,endx,endy);
}
public void init()
{
this.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
endx = e.getX();
endy = e.getY();
//Graphics g = getGraphics();
//g.drawLine(orgx,orgy,endx,endy);
repaint();
}

});
this.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
orgx = e.getX();
orgy = e.getY();
}
public void mouseReleased(MouseEvent e)
{
endx = e.getX();
endy = e.getY();
Graphics g = getGraphics();
g.setColor(Color.RED);
g.setFont(new Font("隶书",Font.ITALIC+Font.BOLD,10));
g.drawString(new String(orgx + ", " + orgy),orgx,orgy);
g.drawString(new String(endx + ", " + endy),endx,endy);
g.drawLine(orgx,orgy,endx,endy);

}

});
}

}

...全文
40 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧