标题:为什么JMenuItem被Canvas(画布)覆盖?是什么原因呢?
只看楼主
xiang092106
Rank: 1
等 级:新手上路
帖 子:53
专家分:0
注 册:2007-5-16
 问题点数:0 回复次数:2 
为什么JMenuItem被Canvas(画布)覆盖?是什么原因呢?
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Apaint extends JFrame implements ActionListener{
int i=7;
JButton[] gr;
JMenu paper=new JMenu("文件(F)");
JMenu edit=new JMenu("编辑(E)");
JMenu format=new JMenu("格式(O)");
JMenu paint=new JMenu("画图(P)");
JMenu help=new JMenu("帮助(H)");

private JLabel label=new JLabel("请在下面画图");

Container fr;
JMenuBar menubar=new JMenuBar();
JMenuItem naw =new JMenuItem("新建(N)",'N');
JMenuItem open=new JMenuItem("打开(O)",'O');
JMenuItem type=new JMenuItem("保存(S)",'S');
JMenuItem exit=new JMenuItem("退出(X)",'X');
JMenuItem withdrawm=new JMenuItem("撤消(U)",'U');
JMenuItem cut=new JMenuItem("剪切(T)",'T');
JMenuItem paste=new JMenuItem("复制(C)",'C');
JMenuItem delet=new JMenuItem("删除(D)",'D');
JMenuItem Autook=new JMenuItem("自动换行(W)",'W');
JMenuItem font=new JMenuItem("字体(F)",'F');
JMenuItem helpTopics=new JMenuItem("帮助主题(H)",'H');
JMenuItem about=new JMenuItem("关于..(A)",'A');
JMenuItem tools=new JMenuItem("工具(P)",'P');
BorderLayout layout1;
public Apaint(){
super("祥祥的画图板..");

super.setJMenuBar(menubar);
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
paper.setMnemonic('F');
edit.setMnemonic('E');
format.setMnemonic('O');
paint.setMnemonic('P');
help.setMnemonic('H');
menubar.add (paper);
menubar.add(edit);
menubar.add(format);
menubar.add(paint);
menubar.add(help);

paper.add(naw );naw.setEnabled(false);
paper.add(open);open.setEnabled(false);
paper.add(type);type .setEnabled(false);
paper.add(exit);exit.addActionListener(new exit());

edit.add(withdrawm);withdrawm.setEnabled(false);
edit.add(cut);cut.setEnabled(false);
edit.add(paste);paste.setEnabled(false); edit.add(delet);delet.setEnabled(false);

format.add(Autook);Autook.setEnabled(false);
format.add(font);font.setEnabled(false);

paint.add(tools);tools.setEnabled(false);

help.add(helpTopics); helpTopics .setEnabled(false);
help.add(about);about.addActionListener(new message());
JPanel panel =new JPanel(new FlowLayout());

gr=new JButton[i];
gr[0]=new JButton("○");
gr[3]=new JButton("◇");
gr[2]=new JButton("□");
gr[1]=new JButton("—");
gr[4]=new JButton("~");
gr[5]=new JButton("☆");
gr[6]=new JButton("←");
for(i=3;i<7;i++){
panel.add(gr[i]);
gr[i].setEnabled(false);
}
for(i=0;i<3;i++){
panel.add(gr[i]);
gr[i].addMouseListener(new MouseAdapter(){
private int lastx,lasty;
public void mousePressed (MouseEvent e){
Point first=e.getPoint();
lastx=first.x;
lasty=first.y;
}
public void mouseDragged(MouseEvent e){

Point current=e.getPoint();
Graphics page=getGraphics();
for(int i=0;i<5;i++){
if(e.getSource()==gr[i]){
if(i==0){ page.drawOval(lastx,lasty,current.x,current.y);}
if(i==2){ page.drawLine(lastx,lasty,current.x,current.y);}
if(i==3){ page.drawRect(lastx,lasty,current.x,current.y);}

}
lastx=current.x;
lasty=current.y;
}
}});
}
fr=getContentPane();
fr.add("Center",new canvas() );
fr.add("South",panel);

fr.add("North",label);
}
private class canvas extends Canvas {
private int lastx,lasty;
public void canvas(){
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
Point first=e.getPoint();
lastx=first.x;
lasty=first.y; }});
addMouseMotionListener(new MouseMotionAdapter(){ public void mouseDragged(MouseEvent e){
Point current=e.getPoint();
lastx=current.x;
lasty=current.y;
}});
setBackground(Color.blue);
setSize(30,30);
}
}
public void naw() {}
public void open(){}
public void type(){}
public void exit(){}
public void withdrawm(){}
public void cut(){}
public void paste(){}
public void delet(){}
public void Autook(){}
public void helpTopes(){}
public void about(){}
public void tools(){}
public void paints(Graphics g){ }
public void actionPerformed(ActionEvent e){

if(e.getSource()==naw) naw ();
if (e.getSource()==open) open();
if(e.getSource()==type) type();

if(e.getSource()==withdrawm) {


}
if(e.getSource()==cut) cut();
if(e.getSource()==paste) paste();
if(e.getSource()==delet) delet();
if(e.getSource()==Autook) Autook();
if(e.getSource()==helpTopics) helpTopes();

if(e.getSource()==tools) tools();
}
class message implements ActionListener{
public void actionPerformed(ActionEvent e){
JFrame frame =new JFrame("关于画图板的信息..");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setSize(100,100);
frame.getContentPane().setLayout(new GridLayout(2,1));
frame.setVisible(true);
JLabel label1=new JLabel("作者信息:");
JLabel label2=new JLabel("滁州学院-石满祥");
frame.getContentPane().add(label1);
frame.getContentPane().add(label2);
}
}
class exit implements ActionListener{
public void actionPerformed(ActionEvent w){
System.exit(0);
}
}
public static void main(String[] args){
Apaint cp=new Apaint();
cp.setVisible(true);
cp.pack();
}
}
搜索更多相关主题的帖子: Canvas JMenuItem 画布 
2007-06-13 23:05
Eastsun
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:802
专家分:0
注 册:2006-12-14
得分:0 
因为Canvas是重量级组件,JMenuItem是轻量级组件,当重量级组件与轻量级组件混用的时候,重量级组件会覆盖轻量级组件.
解决方法:用JPanel来代替Canvas

My BlogClick Me
2007-06-14 02:03
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
得分:0 

写程序的时候,最好不要混用.最好是用swing里面的组件


可惜不是你,陪我到最后
2007-06-14 09:14



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-147284-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.078421 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved