猿问

在java程序中的paint画图画不了,请问为什么repaint不识别?

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(); }
}

回首忆惘然
浏览 158回答 2
2回答

肥皂起泡泡

几个问题1.没有继承frame2.没有repaint3.整个结构很混乱可以试试一个大类从frame继承,然后把监听的东西弄成内部类、匿名类

一只甜甜圈

最上面的类后面 加一句 extends JFrame
随时随地看视频慕课网APP
我要回答