当前位置: 技术问答>java相关
菜单为什么不消失?
来源: 互联网 发布时间:2015-05-09
本文导语: 我在JAVA编写程序中, 1。一个FRAME中有菜单条和一个JPANEL,写出的下拉菜单和弹出式菜单在选择后不能消失,留在了JPANEL,不符合WINDOWS的习惯。应该怎么写,才能符合WINDOWS习惯呢? 2。我要写一个程序,菜单上有DRAW...
我在JAVA编写程序中,
1。一个FRAME中有菜单条和一个JPANEL,写出的下拉菜单和弹出式菜单在选择后不能消失,留在了JPANEL,不符合WINDOWS的习惯。应该怎么写,才能符合WINDOWS习惯呢?
2。我要写一个程序,菜单上有DRAW选项,DRAW下有LINE,CIRCLE,ANGLE三个子菜单,我用鼠标控制画图,那么请问,我要用选择的菜单来控制鼠标要画出什么样的图来,那我
class mypanel extends JPanel
{
.........
public void mouseclicked (mouseEvent evt)
{
if (menucommnad.equease("Line"))
{
}
if (menucommand.equest("Circle"))
{
}
.........
}
}
class myframe extends JFrame
{
.......
public void actionperfion()
{
String arg=getactioncommand();
if (arg.equease("Line"))
{
menucommand=arg;
}
if (arg.equease("Circle"))
{
menucommand=arg;
}
..............
}
public String menucommand;
}
上面的程序是个大概的意思,我就想问,为什么menucommand不能到另一个类里去引用,如果想引用,该怎么办?
1。一个FRAME中有菜单条和一个JPANEL,写出的下拉菜单和弹出式菜单在选择后不能消失,留在了JPANEL,不符合WINDOWS的习惯。应该怎么写,才能符合WINDOWS习惯呢?
2。我要写一个程序,菜单上有DRAW选项,DRAW下有LINE,CIRCLE,ANGLE三个子菜单,我用鼠标控制画图,那么请问,我要用选择的菜单来控制鼠标要画出什么样的图来,那我
class mypanel extends JPanel
{
.........
public void mouseclicked (mouseEvent evt)
{
if (menucommnad.equease("Line"))
{
}
if (menucommand.equest("Circle"))
{
}
.........
}
}
class myframe extends JFrame
{
.......
public void actionperfion()
{
String arg=getactioncommand();
if (arg.equease("Line"))
{
menucommand=arg;
}
if (arg.equease("Circle"))
{
menucommand=arg;
}
..............
}
public String menucommand;
}
上面的程序是个大概的意思,我就想问,为什么menucommand不能到另一个类里去引用,如果想引用,该怎么办?
|
如果一定要重载paintComponents,那么在绘制之前必须用背景色填充整个组件区域,否则就会出现以前的区域不刷新的情况:
public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.setColor(Color.white);
g.fillRect(0,0,this.getWidth(),this.getHeight());
.......
顺便说一句,看不出你这个程序有什么用,如果想保留原有区域的画面,最好创建一个off-screen的Image对象,你所有的操作都对这个对象进行,每次重绘只要画这个Image就可以了,网上有好多关于双缓冲的例子,随便找一个看看就明白了。改了一下,不是很完美,呵呵,因为我现在没有时间
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//class makemenu extends JMenu
class makemenu
{
public makemenu(JMenuBar a)
{
//JMenu filemenu=new JMenu("File");
//a.add(filemenu);
//JMenu editmenu=new JMenu("Edit");
//a.add(editmenu);
}
public void makeTopMenu(JMenuBar a,Object b)
{
//JMenu m=null;
//m=(JMenu)b;
JMenu d=new JMenu((String)b);
d.setActionCommand((String)b);
a.add(d);
//JMenu editmenu=new JMenu("Edit");
//a.add(editmenu);
}
public void makeSubMenu(JMenu a,Object b)
{
JMenuItem d=new JMenuItem((String)b);
a.add(d);
}
}
class testPanel extends JPanel
implements MouseMotionListener
{
public testPanel()
{ setBackground(Color.white);
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
addMouseListener(new MouseAdapter()
{
//确定鼠标事件
public void mouseClicked(MouseEvent evt)
{
//System.out.println(menucommand);
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
if (x1==0 && y1==0)
{
x1 =evt.getX();
y1 =evt.getY();
}
else
{
x2=evt.getX();
y2=evt.getY();
}
System.out.println(x1);
System.out.println(y1);
System.out.println(x2);
System.out.println(y2);
if (x1!=0 && x2!=0 && y1!=0 && y2!=0)
{
if (x1!=x2 || y1!=y2)
{
System.out.println("draw");
//System.out.println(x1);
//System.out.println(y1);
//System.out.println(x2);
//System.out.println(y2);
Graphics g=getGraphics();
g.setColor(Color.blue);
g.drawLine(x1,y1,x2,y2);
g.dispose();
}
}
//重复画线
if ((evt.getModifiers() & InputEvent.BUTTON1_MASK)!=0)
{
System.out.println("left");
if (x2!=0 || y2!=0)
{
x1=x2;
y1=y2;
}
}
else
{
x1=0;
x2=0;
y1=0;
y2=0;
}
}
}
);
addMouseMotionListener(this);
}
public void repaint()
{
if(testform.img!=null)
getGraphics().drawImage(testform.img,0,0,this);
}
public void mouseDragged(MouseEvent evt)
{
}
public void mouseMoved(MouseEvent evt)
{
//setCursor(Cursor.CROSSHAIR_CURSOR);
if(testform.img==null)
testform.img=createImage(this.size().width,this.size().height);
int x3=evt.getX();
int y3=evt.getY();
if (x1!=0 && y1!=0)
{
testform.gv=testform.img.getGraphics();
testform.gv.setColor(Color.blue);
testform.gv.drawLine(x1,y1,x3,y3);
if ((x3!=x4 || y3!=y4) && (x4!=0 || y4!=0))
{
testform.gv.setColor(Color.black);
testform.gv.drawLine(x1,y1,x4,y4);
}
x4=x3;
y4=y3;
repaint();
}
}
public int x1=0;
public int x2=0;
public int y1=0;
public int y2=0;
public int x4=0;
public int y4=0;
public Graphics g;
}
class form extends JFrame
implements ActionListener
{
public form()
{
int wd,hd;
Toolkit tk=Toolkit.getDefaultToolkit();
setTitle("实验表单");
setLocation(0,0);
setSize(600,500);
setResizable(false);
Dimension d=tk.getScreenSize();
wd=d.width;
hd=d.height;
setLocation((wd-600)/2,(hd-500)/2);
addWindowListener
(new WindowAdapter()
{
public void WindowClosting(WindowEvent e)
{
System.exit(0);
}
}
);
//菜单执行
JMenuBar mbar=new JMenuBar();
setJMenuBar(mbar);
//增加主菜单
makemenu makemenu=new makemenu(mbar);
makemenu.makeTopMenu(mbar,"File");
makemenu.makeTopMenu(mbar,"Edit");
makemenu.makeTopMenu(mbar,"Search");
makemenu.makeTopMenu(mbar,"Window");
//makemenu.makeTopMenu(mbar,"Draw");
JMenu Draw=new JMenu("Draw");
mbar.add(Draw);
makemenu.makeTopMenu(mbar,"Help");
//增加子菜单
JMenuItem drawLine=new JMenuItem("Line");
Draw.add(drawLine);
drawLine.addActionListener(this);
JMenuItem drawCircle=new JMenuItem("Circle");
Draw.add(drawCircle);
drawCircle.addActionListener(this);
JMenuItem drawAngle=new JMenuItem("Angle");
Draw.add(drawAngle);
drawAngle.addActionListener(this);
//弹出式菜单
//JPopupMenu popup=new JPopupMenu();
popup=new JPopupMenu();
JMenuItem copymenu=new JMenuItem("Copy");
JMenuItem cutmenu=new JMenuItem("Cut");
JMenuItem pastermenu=new JMenuItem("Paster");
JMenuItem propertylemenu=new JMenuItem("Propertyle");
popup.add(copymenu);
popup.add(cutmenu);
popup.add(pastermenu);
popup.add(propertylemenu);
//调用弹出式菜单
addMouseListener(new MouseAdapter()
{ public void mouseReleased(MouseEvent evt)
{ if (evt.isPopupTrigger())
//popup.show(evt.getComponent(),evt.getX(), evt.getY());
popup.show(evt.getComponent(),evt.getX(),evt.getY());
}
});
//客户区的画图
Container containPanel=getContentPane();
JPanel p1=new testPanel();
setBackground(Color.white);
setCursor(Cursor.HAND_CURSOR);
containPanel.add(p1);
}
public void actionPerformed(ActionEvent evt)
{
String arg=evt.getActionCommand();
System.out.println(arg);
if (arg .equals("Line"))
{
menucommand=arg;
}
if (arg.equals("Circle"))
{
menucommand=arg;
}
if (arg.equals("Angle"))
{
menucommand=arg;
}
if(arg.equals("Exit"))
System.exit(0);
repaint();
}
private JPopupMenu popup;
public String menucommand;
}
public class testform
{
static Image img=null;
static Graphics gv=null;
public static void main(String[] args)
{
Frame frame=new form();
//frame.setBackground(Color.white);
frame.show();
}
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.setColor(Color.white);
g.fillRect(0,0,this.getWidth(),this.getHeight());
.......
顺便说一句,看不出你这个程序有什么用,如果想保留原有区域的画面,最好创建一个off-screen的Image对象,你所有的操作都对这个对象进行,每次重绘只要画这个Image就可以了,网上有好多关于双缓冲的例子,随便找一个看看就明白了。改了一下,不是很完美,呵呵,因为我现在没有时间
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//class makemenu extends JMenu
class makemenu
{
public makemenu(JMenuBar a)
{
//JMenu filemenu=new JMenu("File");
//a.add(filemenu);
//JMenu editmenu=new JMenu("Edit");
//a.add(editmenu);
}
public void makeTopMenu(JMenuBar a,Object b)
{
//JMenu m=null;
//m=(JMenu)b;
JMenu d=new JMenu((String)b);
d.setActionCommand((String)b);
a.add(d);
//JMenu editmenu=new JMenu("Edit");
//a.add(editmenu);
}
public void makeSubMenu(JMenu a,Object b)
{
JMenuItem d=new JMenuItem((String)b);
a.add(d);
}
}
class testPanel extends JPanel
implements MouseMotionListener
{
public testPanel()
{ setBackground(Color.white);
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
addMouseListener(new MouseAdapter()
{
//确定鼠标事件
public void mouseClicked(MouseEvent evt)
{
//System.out.println(menucommand);
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
if (x1==0 && y1==0)
{
x1 =evt.getX();
y1 =evt.getY();
}
else
{
x2=evt.getX();
y2=evt.getY();
}
System.out.println(x1);
System.out.println(y1);
System.out.println(x2);
System.out.println(y2);
if (x1!=0 && x2!=0 && y1!=0 && y2!=0)
{
if (x1!=x2 || y1!=y2)
{
System.out.println("draw");
//System.out.println(x1);
//System.out.println(y1);
//System.out.println(x2);
//System.out.println(y2);
Graphics g=getGraphics();
g.setColor(Color.blue);
g.drawLine(x1,y1,x2,y2);
g.dispose();
}
}
//重复画线
if ((evt.getModifiers() & InputEvent.BUTTON1_MASK)!=0)
{
System.out.println("left");
if (x2!=0 || y2!=0)
{
x1=x2;
y1=y2;
}
}
else
{
x1=0;
x2=0;
y1=0;
y2=0;
}
}
}
);
addMouseMotionListener(this);
}
public void repaint()
{
if(testform.img!=null)
getGraphics().drawImage(testform.img,0,0,this);
}
public void mouseDragged(MouseEvent evt)
{
}
public void mouseMoved(MouseEvent evt)
{
//setCursor(Cursor.CROSSHAIR_CURSOR);
if(testform.img==null)
testform.img=createImage(this.size().width,this.size().height);
int x3=evt.getX();
int y3=evt.getY();
if (x1!=0 && y1!=0)
{
testform.gv=testform.img.getGraphics();
testform.gv.setColor(Color.blue);
testform.gv.drawLine(x1,y1,x3,y3);
if ((x3!=x4 || y3!=y4) && (x4!=0 || y4!=0))
{
testform.gv.setColor(Color.black);
testform.gv.drawLine(x1,y1,x4,y4);
}
x4=x3;
y4=y3;
repaint();
}
}
public int x1=0;
public int x2=0;
public int y1=0;
public int y2=0;
public int x4=0;
public int y4=0;
public Graphics g;
}
class form extends JFrame
implements ActionListener
{
public form()
{
int wd,hd;
Toolkit tk=Toolkit.getDefaultToolkit();
setTitle("实验表单");
setLocation(0,0);
setSize(600,500);
setResizable(false);
Dimension d=tk.getScreenSize();
wd=d.width;
hd=d.height;
setLocation((wd-600)/2,(hd-500)/2);
addWindowListener
(new WindowAdapter()
{
public void WindowClosting(WindowEvent e)
{
System.exit(0);
}
}
);
//菜单执行
JMenuBar mbar=new JMenuBar();
setJMenuBar(mbar);
//增加主菜单
makemenu makemenu=new makemenu(mbar);
makemenu.makeTopMenu(mbar,"File");
makemenu.makeTopMenu(mbar,"Edit");
makemenu.makeTopMenu(mbar,"Search");
makemenu.makeTopMenu(mbar,"Window");
//makemenu.makeTopMenu(mbar,"Draw");
JMenu Draw=new JMenu("Draw");
mbar.add(Draw);
makemenu.makeTopMenu(mbar,"Help");
//增加子菜单
JMenuItem drawLine=new JMenuItem("Line");
Draw.add(drawLine);
drawLine.addActionListener(this);
JMenuItem drawCircle=new JMenuItem("Circle");
Draw.add(drawCircle);
drawCircle.addActionListener(this);
JMenuItem drawAngle=new JMenuItem("Angle");
Draw.add(drawAngle);
drawAngle.addActionListener(this);
//弹出式菜单
//JPopupMenu popup=new JPopupMenu();
popup=new JPopupMenu();
JMenuItem copymenu=new JMenuItem("Copy");
JMenuItem cutmenu=new JMenuItem("Cut");
JMenuItem pastermenu=new JMenuItem("Paster");
JMenuItem propertylemenu=new JMenuItem("Propertyle");
popup.add(copymenu);
popup.add(cutmenu);
popup.add(pastermenu);
popup.add(propertylemenu);
//调用弹出式菜单
addMouseListener(new MouseAdapter()
{ public void mouseReleased(MouseEvent evt)
{ if (evt.isPopupTrigger())
//popup.show(evt.getComponent(),evt.getX(), evt.getY());
popup.show(evt.getComponent(),evt.getX(),evt.getY());
}
});
//客户区的画图
Container containPanel=getContentPane();
JPanel p1=new testPanel();
setBackground(Color.white);
setCursor(Cursor.HAND_CURSOR);
containPanel.add(p1);
}
public void actionPerformed(ActionEvent evt)
{
String arg=evt.getActionCommand();
System.out.println(arg);
if (arg .equals("Line"))
{
menucommand=arg;
}
if (arg.equals("Circle"))
{
menucommand=arg;
}
if (arg.equals("Angle"))
{
menucommand=arg;
}
if(arg.equals("Exit"))
System.exit(0);
repaint();
}
private JPopupMenu popup;
public String menucommand;
}
public class testform
{
static Image img=null;
static Graphics gv=null;
public static void main(String[] args)
{
Frame frame=new form();
//frame.setBackground(Color.white);
frame.show();
}
}
|
JPanel的背景色setBackground(Color.white)移到了构造函数中。程序框架
真的应该改了。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//class makemenu extends JMenu
class makemenu
{
public makemenu(JMenuBar a)
{
//JMenu filemenu=new JMenu("File");
//a.add(filemenu);
//JMenu editmenu=new JMenu("Edit");
//a.add(editmenu);
}
public void makeTopMenu(JMenuBar a,Object b)
{
//JMenu m=null;
//m=(JMenu)b;
JMenu d=new JMenu((String)b);
d.setActionCommand((String)b);
a.add(d);
//JMenu editmenu=new JMenu("Edit");
//a.add(editmenu);
}
public void makeSubMenu(JMenu a,Object b)
{
JMenuItem d=new JMenuItem((String)b);
a.add(d);
}
}
class testPanel extends JPanel
implements MouseMotionListener
{
public testPanel()
{
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
setBackground(Color.white);
addMouseListener(new MouseAdapter()
{
//确定鼠标事件
public void mouseClicked(MouseEvent evt)
{
//System.out.println(menucommand);
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
if (x1==0 && y1==0)
{
x1 =evt.getX();
y1 =evt.getY();
}
else
{
x2=evt.getX();
y2=evt.getY();
}
System.out.println(x1);
System.out.println(y1);
System.out.println(x2);
System.out.println(y2);
if (x1!=0 && x2!=0 && y1!=0 && y2!=0)
{
if (x1!=x2 || y1!=y2)
{
System.out.println("draw");
//System.out.println(x1);
//System.out.println(y1);
//System.out.println(x2);
//System.out.println(y2);
Graphics g=getGraphics();
g.setColor(Color.blue);
g.drawLine(x1,y1,x2,y2);
g.dispose();
}
}
//重复画线
if ((evt.getModifiers() & InputEvent.BUTTON1_MASK)!=0)
{
System.out.println("left");
if (x2!=0 || y2!=0)
{
x1=x2;
y1=y2;
}
}
else
{
x1=0;
x2=0;
y1=0;
y2=0;
}
}
}
);
addMouseMotionListener(this);
}
/*public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.setColor(Color.blue);
Font f=new Font("SansSerif",Font.BOLD+Font.ITALIC,18);
g.setFont(f);
g.drawString("我的实验地!",300,100);
g.drawLine(100,100,400,100);
g.drawLine(400,100,400,400);
g.drawLine(400,400,100,400);
g.drawLine(100,400,100,100);
Polygon p=new Polygon();
p.addPoint(100,100);
p.addPoint(150,170);
p.addPoint(50,170);
g.drawPolygon(p);
g.drawArc(400,300,200,200,0,360);
g.translate(200,200);
this.validate();
}*/
public void mouseDragged(MouseEvent evt)
{
}
public void mouseMoved(MouseEvent evt)
{
//setCursor(Cursor.CROSSHAIR_CURSOR);
int x3=evt.getX();
int y3=evt.getY();
if (x1!=0 && y1!=0)
{
Graphics n=getGraphics();
n.setColor(Color.blue);
n.drawLine(x1,y1,x3,y3);
if ((x3!=x4 || y3!=y4) && (x4!=0 || y4!=0))
{
n.setColor(Color.black);
n.drawLine(x1,y1,x4,y4);
}
x4=x3;
y4=y3;
n.dispose();
}
}
public int x1=0;
public int x2=0;
public int y1=0;
public int y2=0;
public int x4=0;
public int y4=0;
public Graphics g;
}
class form extends JFrame
implements ActionListener
{
JPanel p1 = null;
public form()
{
int wd,hd;
Toolkit tk=Toolkit.getDefaultToolkit();
setTitle("实验表单");
setLocation(0,0);
setSize(600,500);
setResizable(false);
Dimension d=tk.getScreenSize();
wd=d.width;
hd=d.height;
setLocation((wd-600)/2,(hd-500)/2);
addWindowListener
(new WindowAdapter()
{
public void WindowClosting(WindowEvent e)
{
System.exit(0);
}
}
);
//菜单执行
JMenuBar mbar=new JMenuBar();
setJMenuBar(mbar);
//增加主菜单
makemenu makemenu=new makemenu(mbar);
makemenu.makeTopMenu(mbar,"File");
makemenu.makeTopMenu(mbar,"Edit");
makemenu.makeTopMenu(mbar,"Search");
makemenu.makeTopMenu(mbar,"Window");
//makemenu.makeTopMenu(mbar,"Draw");
JMenu Draw=new JMenu("Draw");
mbar.add(Draw);
makemenu.makeTopMenu(mbar,"Help");
//增加子菜单
JMenuItem drawLine=new JMenuItem("Line");
Draw.add(drawLine);
drawLine.addActionListener(this);
JMenuItem drawCircle=new JMenuItem("Circle");
Draw.add(drawCircle);
drawCircle.addActionListener(this);
JMenuItem drawAngle=new JMenuItem("Angle");
Draw.add(drawAngle);
drawAngle.addActionListener(this);
//弹出式菜单
//JPopupMenu popup=new JPopupMenu();
popup=new JPopupMenu();
JMenuItem copymenu=new JMenuItem("Copy");
JMenuItem cutmenu=new JMenuItem("Cut");
JMenuItem pastermenu=new JMenuItem("Paster");
JMenuItem propertylemenu=new JMenuItem("Propertyle");
popup.add(copymenu);
popup.add(cutmenu);
popup.add(pastermenu);
popup.add(propertylemenu);
//调用弹出式菜单
addMouseListener(new MouseAdapter()
{ public void mouseReleased(MouseEvent evt)
{ if (evt.isPopupTrigger())
//popup.show(evt.getComponent(),evt.getX(), evt.getY());
popup.show(evt.getComponent(),evt.getX(),evt.getY());
}
});
//客户区的画图
Container containPanel=getContentPane();
p1=new testPanel();
setBackground(Color.white);
setCursor(Cursor.HAND_CURSOR);
containPanel.add(p1);
}
public void actionPerformed(ActionEvent evt)
{
String arg=evt.getActionCommand();
System.out.println(arg);
if (arg .equals("Line"))
{
menucommand=arg;
}
if (arg.equals("Circle"))
{
menucommand=arg;
}
if (arg.equals("Angle"))
{
menucommand=arg;
}
if(arg.equals("Exit"))
System.exit(0);
repaint();
}
private JPopupMenu popup;
public String menucommand;
}
public class testform
{
public static void main(String[] args)
{
Frame frame=new form();
//frame.setBackground(Color.white);
frame.show();
}
}
真的应该改了。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//class makemenu extends JMenu
class makemenu
{
public makemenu(JMenuBar a)
{
//JMenu filemenu=new JMenu("File");
//a.add(filemenu);
//JMenu editmenu=new JMenu("Edit");
//a.add(editmenu);
}
public void makeTopMenu(JMenuBar a,Object b)
{
//JMenu m=null;
//m=(JMenu)b;
JMenu d=new JMenu((String)b);
d.setActionCommand((String)b);
a.add(d);
//JMenu editmenu=new JMenu("Edit");
//a.add(editmenu);
}
public void makeSubMenu(JMenu a,Object b)
{
JMenuItem d=new JMenuItem((String)b);
a.add(d);
}
}
class testPanel extends JPanel
implements MouseMotionListener
{
public testPanel()
{
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
setBackground(Color.white);
addMouseListener(new MouseAdapter()
{
//确定鼠标事件
public void mouseClicked(MouseEvent evt)
{
//System.out.println(menucommand);
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
if (x1==0 && y1==0)
{
x1 =evt.getX();
y1 =evt.getY();
}
else
{
x2=evt.getX();
y2=evt.getY();
}
System.out.println(x1);
System.out.println(y1);
System.out.println(x2);
System.out.println(y2);
if (x1!=0 && x2!=0 && y1!=0 && y2!=0)
{
if (x1!=x2 || y1!=y2)
{
System.out.println("draw");
//System.out.println(x1);
//System.out.println(y1);
//System.out.println(x2);
//System.out.println(y2);
Graphics g=getGraphics();
g.setColor(Color.blue);
g.drawLine(x1,y1,x2,y2);
g.dispose();
}
}
//重复画线
if ((evt.getModifiers() & InputEvent.BUTTON1_MASK)!=0)
{
System.out.println("left");
if (x2!=0 || y2!=0)
{
x1=x2;
y1=y2;
}
}
else
{
x1=0;
x2=0;
y1=0;
y2=0;
}
}
}
);
addMouseMotionListener(this);
}
/*public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.setColor(Color.blue);
Font f=new Font("SansSerif",Font.BOLD+Font.ITALIC,18);
g.setFont(f);
g.drawString("我的实验地!",300,100);
g.drawLine(100,100,400,100);
g.drawLine(400,100,400,400);
g.drawLine(400,400,100,400);
g.drawLine(100,400,100,100);
Polygon p=new Polygon();
p.addPoint(100,100);
p.addPoint(150,170);
p.addPoint(50,170);
g.drawPolygon(p);
g.drawArc(400,300,200,200,0,360);
g.translate(200,200);
this.validate();
}*/
public void mouseDragged(MouseEvent evt)
{
}
public void mouseMoved(MouseEvent evt)
{
//setCursor(Cursor.CROSSHAIR_CURSOR);
int x3=evt.getX();
int y3=evt.getY();
if (x1!=0 && y1!=0)
{
Graphics n=getGraphics();
n.setColor(Color.blue);
n.drawLine(x1,y1,x3,y3);
if ((x3!=x4 || y3!=y4) && (x4!=0 || y4!=0))
{
n.setColor(Color.black);
n.drawLine(x1,y1,x4,y4);
}
x4=x3;
y4=y3;
n.dispose();
}
}
public int x1=0;
public int x2=0;
public int y1=0;
public int y2=0;
public int x4=0;
public int y4=0;
public Graphics g;
}
class form extends JFrame
implements ActionListener
{
JPanel p1 = null;
public form()
{
int wd,hd;
Toolkit tk=Toolkit.getDefaultToolkit();
setTitle("实验表单");
setLocation(0,0);
setSize(600,500);
setResizable(false);
Dimension d=tk.getScreenSize();
wd=d.width;
hd=d.height;
setLocation((wd-600)/2,(hd-500)/2);
addWindowListener
(new WindowAdapter()
{
public void WindowClosting(WindowEvent e)
{
System.exit(0);
}
}
);
//菜单执行
JMenuBar mbar=new JMenuBar();
setJMenuBar(mbar);
//增加主菜单
makemenu makemenu=new makemenu(mbar);
makemenu.makeTopMenu(mbar,"File");
makemenu.makeTopMenu(mbar,"Edit");
makemenu.makeTopMenu(mbar,"Search");
makemenu.makeTopMenu(mbar,"Window");
//makemenu.makeTopMenu(mbar,"Draw");
JMenu Draw=new JMenu("Draw");
mbar.add(Draw);
makemenu.makeTopMenu(mbar,"Help");
//增加子菜单
JMenuItem drawLine=new JMenuItem("Line");
Draw.add(drawLine);
drawLine.addActionListener(this);
JMenuItem drawCircle=new JMenuItem("Circle");
Draw.add(drawCircle);
drawCircle.addActionListener(this);
JMenuItem drawAngle=new JMenuItem("Angle");
Draw.add(drawAngle);
drawAngle.addActionListener(this);
//弹出式菜单
//JPopupMenu popup=new JPopupMenu();
popup=new JPopupMenu();
JMenuItem copymenu=new JMenuItem("Copy");
JMenuItem cutmenu=new JMenuItem("Cut");
JMenuItem pastermenu=new JMenuItem("Paster");
JMenuItem propertylemenu=new JMenuItem("Propertyle");
popup.add(copymenu);
popup.add(cutmenu);
popup.add(pastermenu);
popup.add(propertylemenu);
//调用弹出式菜单
addMouseListener(new MouseAdapter()
{ public void mouseReleased(MouseEvent evt)
{ if (evt.isPopupTrigger())
//popup.show(evt.getComponent(),evt.getX(), evt.getY());
popup.show(evt.getComponent(),evt.getX(),evt.getY());
}
});
//客户区的画图
Container containPanel=getContentPane();
p1=new testPanel();
setBackground(Color.white);
setCursor(Cursor.HAND_CURSOR);
containPanel.add(p1);
}
public void actionPerformed(ActionEvent evt)
{
String arg=evt.getActionCommand();
System.out.println(arg);
if (arg .equals("Line"))
{
menucommand=arg;
}
if (arg.equals("Circle"))
{
menucommand=arg;
}
if (arg.equals("Angle"))
{
menucommand=arg;
}
if(arg.equals("Exit"))
System.exit(0);
repaint();
}
private JPopupMenu popup;
public String menucommand;
}
public class testform
{
public static void main(String[] args)
{
Frame frame=new form();
//frame.setBackground(Color.white);
frame.show();
}
}
|
你的程序有点乱。重新部署吧。
封掉testPanel的paintComponent(Graphics g)整个方法你就知道怎么改了。
封掉testPanel的paintComponent(Graphics g)整个方法你就知道怎么改了。