刚学GUI.实验中遇到问题.

sy9988111 2008-05-14 09:29:15
做了个鼠标画线的实验,可是上面的菜单栏显示不出来,为什么
代码如下:
package event;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Try extends JPanel
{
private int x=-1,y=-1;


public Try()
{
this.setLayout(new BorderLayout(5,5));
JPanel p1 = new JPanel(new FlowLayout());
JPanel p2 = new JPanel(new FlowLayout());


p1.addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent event)
{
x=event.getX();
y=event.getY();
Graphics g=getGraphics();
paint(g);
}
}

);

this.add(p1,"Center");
JMenu m1 = new JMenu("编辑");
JMenu m2 = new JMenu("查看");
JMenu m3 = new JMenu("帮助");

JMenuBar bar = new JMenuBar();
bar.add(m1);
bar.add(m2);
bar.add(m3);
p2.add(bar);
this.add(p2,"North");
}

public static void main(String args[])
{
Try a = new Try();
JFrame application = new JFrame();

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(a);
application.setSize(400,300);
application.setVisible(true);


}
public void paint(Graphics g)
{
g.fillOval(x,y+40,5,5);

}


}
...全文
31 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
a_nuo 2008-05-14
  • 打赏
  • 举报
回复
需要调用父类的paint方法
你最好找一本好的书看看Swing的编程
a_nuo 2008-05-14
  • 打赏
  • 举报
回复
package event;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Try extends JPanel
{
private int x=-1,y=-1;


public Try()
{
this.setLayout(new BorderLayout(5,5));
JPanel p1 = new JPanel(new FlowLayout());
JPanel p2 = new JPanel(new FlowLayout());


p1.addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent event)
{
x=event.getX();
y=event.getY();
Graphics g=getGraphics();
paint(g);
}
}

);

this.add(p1,"Center");
JMenu m1 = new JMenu("编辑");
JMenu m2 = new JMenu("查看");
JMenu m3 = new JMenu("帮助");

JMenuBar bar = new JMenuBar();
bar.add(m1);
bar.add(m2);
bar.add(m3);
p2.add(bar);
this.add(p2,"North");
}

public static void main(String args[])
{
Try a = new Try();
JFrame application = new JFrame();

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(a);
application.setSize(400,300);
application.setVisible(true);


}
public void paint(Graphics g)
{
super.paint(g);//增加此句
g.fillOval(x,y+40,5,5);

}


}

62,614

社区成员

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

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