- Java code
import java.awt.*;import java.awt.event.*;public class DrawLine extends Frame{ public DrawLine() { addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); this.addMouseListener(new MouseAdapter() { int orgX; int orgY; public void mousePressed(MouseEvent e) { orgX=e.getX(); orgY=e.getY(); } public void mouseReleased(MouseEvent e) { Graphics g=getGraphics();//为什么等号右边要这么写。 g.setColor(Color.RED); g.setFont(new Font(null,Font.ITALIC|Font.BOLD,30)); //g.setFont(Font.BOLD)为什么此处这么写会报错?为什么不能这么写? g.drawString(orgX+","+orgY,orgX,orgY); g.drawString(e.getX()+","+e.getY(),e.getX(),e.getY()); g.drawLine(orgX,orgY,e.getX(),e.getY()); } }); } public static void main(String []args) { DrawLine dl=new DrawLine(); dl.setSize(400,400); dl.setTitle("窗体"); dl.setVisible(true); }}
------解决方案--------------------
Graphics g=getGraphics();//为什么等号右边要这么写。
相当于 Graphics g=this.getGraphics(); //getGraphics()方法是从父类继承而来的,获得组建的图形上下文
------解决方案--------------------
g.setFont(Font.BOLD) setFont方法需要一个Font对象参数,而Font.BOLD是个int类型的,LZ自己要学会自己查看javadoc文档