import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.awt.geom.*;class lx11 implements ActionListener,MouseListener{String s="htx"; float x1,y1,x2,y2;JMenuBar bar=new JMenuBar();JMenu menu=new JMenu("请点击选择用鼠标画矩形或写字");JMenuItem htx=new JMenuItem("画矩形");JMenuItem xwz=new JMenuItem("写文字");JFrame f=new JFrame("画图");public lx11(){Container cp=f.getContentPane();f.setSize(200,320);f.show();bar.setOpaque(true);f.setJMenuBar(bar);bar.add(menu);menu.add(htx);menu.add(xwz);htx.addActionListener(this);xwz.addActionListener(this);f.addWindowListener(new WinLis());f.addMouseListener(this);}public void actionPerformed(ActionEvent e){if(e.getSource( )==htx) s="htx";if(e.getSource( )==xwz) s="xwz";}public void mousePressed(MouseEvent e){x1=(float)e.getX();y1=(float)e.getY();}public void mouseReleased(MouseEvent e){x2=(float)e.getX();y2=(float)e.getY();}public void mouseExited(MouseEvent e){}public void mouseEntered(MouseEvent e){}public void mouseClicked(MouseEvent e){}public void paint(Graphics d){Graphics2D g= (Graphics2D)d;f.paint(g);g.setStroke(new BasicStroke(4));if(s=="htx"){ Rectangle2D.Float rec=new Rectangle2D.Float(x1,y1,x2,y2);g.drawString(s,100,100);}}public void update(Graphics g){ paint(g); }class WinLis extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0); }}}public class lx11_8{public static void main(String[ ] args){ new lx11(); }}
添加回答
举报
0/150
提交
取消