java插件菜单

请问哪位写过没,都找了很久都没有找到,要得急,希望各位帮帮忙,要在页面中右击鼠标弹出菜单来。
问题补充:
能不能加些注释,还有jsp怎样调用呢?小妹不懂希望能更加详细一点,非常感谢!

package test1;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Applet1 extends Applet {
boolean isStandalone = false;
PopupMenu popupMenu1 = new PopupMenu();
MenuItem menuItem1 = new MenuItem();
MenuItem menuItem2 = new MenuItem();
MenuItem menuItem3 = new MenuItem();
Button button1 = new Button();
/**Get a parameter value*/
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
/**Construct the applet*/
public Applet1() {
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
menuItem1.setLabel("1");
menuItem2.setLabel("2");
menuItem3.setLabel("3");
button1.setLabel("button1");
file://这是button1的监听事件,
button1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(MouseEvent e) {
button1_mousePressed(e);
}
});
popupMenu1.add(menuItem1);
popupMenu1.add(menuItem2);
popupMenu1.add(menuItem3);
this.add(button1, null);
add(popupMenu1);
}
/**Get Applet information*/
public String getAppletInfo() {
return "Applet Information";
}
/**Get parameter info*/
public String[][] getParameterInfo() {
return null;
}
void button1_mousePressed(MouseEvent e) {
/*通过InputEvent.BUTTON3_MASK对右键的判断
要判断是左键用InputEvent.BUTTON1_MASK,
*/
int mods=e.getModifiers();
if((mods&InputEvent.BUTTON3_MASK)!=0)
{
popupMenu1.show(button1,e.getX(),e.getY());
button
}
}
}
 
wangxuliangboy (高级程序员) 2008-11-06
Java代码 复制代码
  1.   
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
*
* @author Five
*
*/
public class PopupTest extends JFrame {
   private JRadioButtonMenuItem items[];
   private final Color colorValues[] =
      { Color.BLUE, Color.YELLOW, Color.RED };
   private JPopupMenu popupMenu;

   // set up GUI
   public PopupTest()
   {
      super( "Using JPopupMenus" );

      ItemHandler handler = new ItemHandler();
      String colors[] = { "Blue", "Yellow", "Red" };

      // set up popup menu and its items
      ButtonGroup colorGroup = new ButtonGroup();
      popupMenu = new JPopupMenu();
      items = new JRadioButtonMenuItem[ 3 ];

      // construct each menu item and add to popup menu; also
      // enable event handling for each menu item
      for ( int count = 0; count < items.length; count++ ) {
         items[ count ] = new JRadioButtonMenuItem( colors[ count ] );
         popupMenu.add( items[ count ] );
         colorGroup.add( items[ count ] );
         items[ count ].addActionListener( handler );
      }

      getContentPane().setBackground( Color.WHITE );

      // declare a MouseListener for the window that displays
      // a JPopupMenu when the popup trigger event occurs
      addMouseListener(

         new MouseAdapter() {  // anonymous inner class

            // handle mouse press event
            public void mousePressed( MouseEvent event )
            {
               checkForTriggerEvent( event );
            }

            // handle mouse release event
            public void mouseReleased( MouseEvent event )
            {
               checkForTriggerEvent( event );
            }

            // determine whether event should trigger popup menu
            private void checkForTriggerEvent( MouseEvent event )
            {
               if ( event.isPopupTrigger() )
                  popupMenu.show(
                     event.getComponent(), event.getX(), event.getY() ); 
            }

         } // end anonymous inner clas

      ); // end call to addMouseListener

      setSize( 300, 200 );
      setVisible( true );

   } // end constructor PopupTest

   public static void main( String args[] )
   {
      PopupTest application = new PopupTest();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }

   // private inner class to handle menu item events
   private class ItemHandler implements ActionListener {

      // process menu item selections
      public void actionPerformed( ActionEvent event )
      {
         // determine which menu item was selected
         for ( int i = 0; i < items.length; i++ )
            if ( event.getSource() == items[ i ] ) {
               getContentPane().setBackground( colorValues[ i ] );
               return;
            }
      }

   } // end private inner class ItemHandler

} // end class PopupTest


 
iampurse (高级程序员) 2008-11-06
通过<a>标签的oncontextmenu事件实现

可参考
http://www.wangchao.net.cn/bbsdetail_38014.html
fishbottle (资深程序员) 2008-11-06
1. 在a标签中加入oncontextmenu事件调用popupmenu(),并且返回false.返回false指不会调用默认的菜单

2.   var oPopup = window.createPopup();通过html隐藏对象window创建一个popup

3. 配置oPopup属性和innerHTML
Html代码 复制代码
  1. oPopBody.style.backgroundColor = "buttonface";   
  2.    oPopBody.style.border = "solid black 1px";   
  3.   
  4.   
  5.   
  6.    oPopBody.innerHTML = ""  
  7.    +"<table cellpadding=0 cellspacing=0 border=0 width=100style='font-size:12px;'>"   
  8.    +"<tr><td valign=middle height=20 onmouseover=/"this.bgColor='highlight';this.style.color='highlighttext';/" "   
  9.    +"onmouseout=/"this.bgColor='';this.style.color=''/" onclick=/"parent.window.open('/news/view.asp?id="+id+"');/">&nbsp;&nbsp;查看该新闻 </td></tr>"   
  10.    +"<tr><td bgcolor='threedshadow' height=1></td></tr>"   
  11.    +"<tr><td valign=middle height=20 onmouseover=/"this.bgColor='highlight';this.style.color='highlighttext';/" "   
  12.    +"onmouseout=/"this.bgColor='';this.style.color=''/" onclick=/"parent.document.frames['main'].location="   
  13.    +"'newsimgdel.asp?id="+id+"'/">&nbsp;&nbsp;删除 </td></tr><table>";  

oPopBody.style.backgroundColor = "buttonface";
   oPopBody.style.border = "solid black 1px";



   oPopBody.innerHTML = ""
   +"<table cellpadding=0 cellspacing=0 border=0 width=100% style='font-size:12px;'>"
   +"<tr><td valign=middle height=20 οnmοuseοver=/"this.bgColor='highlight';this.style.color='highlighttext';/" "
   +"οnmοuseοut=/"this.bgColor='';this.style.color=''/" οnclick=/"parent.window.open('/news/view.asp?id="+id+"');/">&nbsp;&nbsp;查看该新闻 </td></tr>"
   +"<tr><td bgcolor='threedshadow' height=1></td></tr>"
   +"<tr><td valign=middle height=20 οnmοuseοver=/"this.bgColor='highlight';this.style.color='highlighttext';/" "
   +"οnmοuseοut=/"this.bgColor='';this.style.color=''/" οnclick=/"parent.document.frames['main'].location="
   +"'newsimgdel.asp?id="+id+"'/">&nbsp;&nbsp;删除 </td></tr><table>";


innerHTML里是一个Table
4. 显示popup
oPopup.show(event.x, event.y, 90, 41, document.body);
5.return false;
 
fishbottle (资深程序员) 2008-11-07
另外右击弹击事件是javascript事件。
和java没有很大的联系。

直接将javascript代码加入jsp中就可以用了。
 
以前我在project里写的,可以参考

import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.beans.*;

/**
* 扩展的JDesktopPane,在父窗体菜单(如果父窗体有菜单条)上自动添加windows菜单,并实现对内部窗体的切换、平铺、层叠管理
* @version 1.0
* @see JDesktopPane
*/
public class MDIPane extends JDesktopPane implements VetoableChangeListener{
JFrame parentFrame = null;
JMenuBar parentMenuBar = null;
JMenu jMenuWindow = new JMenu();
JMenuItem jMenuWindowTile = new JMenuItem();
JMenuItem jMenuWindowCascade = new JMenuItem();
ArrayList frameItems = new ArrayList();
ArrayList frameHandles = new ArrayList();
ButtonGroup frameItemGroup = new ButtonGroup();

/**
* @param parentFrame,父窗体
*/
public MDIPane(JFrame parentFrame) {
try {
this.parentFrame = parentFrame;
this.parentMenuBar = parentFrame.getJMenuBar();
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

/**
* 接受一个传入的JInternalFrame,如果此JInternalFrame已经在MDIPane中,把它置为当前活动窗口
* 如果JInternalFrame不在MDIPane中,则把它加入MDIPane
*/
public void showFrame(JInternalFrame frame) {
if (!frameIsContained(frame)) {
this.add(frame);
addFrameHandleMenuItem(frame);
}
frame.addVetoableChangeListener(this);
displayFrame(frame);
frame.setSelected(true);
}

private void displayFrame(JInternalFrame frame) {
try {
if (frame.isIcon())
frame.setIcon(false);
if (frame.isMaximum());
frame.setMaximum(false);
} catch (PropertyVetoException ex) {
System.out.println(ex);
}
frame.setLocation(0, 0);
frame.setSize(this.getSize());
//frame.revalidate();
frame.setVisible(true);
}

private void addFrameHandleMenuItem(JInternalFrame frame) {
String frameTitle = frame.getTitle();
JCheckBoxMenuItem frameItem = new JCheckBoxMenuItem(frameTitle);
frameItemGroup.add(frameItem);
frameItem.setSelected(true);
frameItems.add(frameItem);
frameHandles.add(frame);
jMenuWindow.add(frameItem);
frameItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
showSelectedFrame(e);
}
});
}

private void showSelectedFrame(ActionEvent e) {
JCheckBoxMenuItem frameItem = (JCheckBoxMenuItem)e.getSource();
for (int i = 0; i < frameItems.size(); i++) {
if (frameItem == (JCheckBoxMenuItem)frameItems.get(i)) {
JInternalFrame frame = (JInternalFrame)frameHandles.get(i);
try {
frame.setSelected(true);
displayFrame(frame);
} catch (PropertyVetoException ex) {
System.out.println(ex);
}
break;
}
}
}

public void vetoableChange(PropertyChangeEvent e) {
JInternalFrame frame = (JInternalFrame)e.getSource();
String name = e.getPropertyName();
Object value = e.getNewValue();
if (name.equals("selected") && value.equals(Boolean.TRUE)) {
for (int i = 0; i < frameHandles.size(); i++) {
if (frame == (JInternalFrame)frameHandles.get(i)) {
JCheckBoxMenuItem frameItem = (JCheckBoxMenuItem)frameItems.get(i);
frameItem.setSelected(true);
break;
}
}
}
if (name.equals("closed") && value.equals(Boolean.TRUE)) {
for (int i = 0; i < frameHandles.size(); i++) {
if (frame == (JInternalFrame)frameHandles.get(i)) {
jMenuWindow.remove((JCheckBoxMenuItem)frameItems.get(i));
frameItemGroup.remove((JCheckBoxMenuItem)frameItems.get(i));
frameHandles.remove(i);
frameItems.remove(i);
break;
}
}
}
}

private boolean frameIsContained(JInternalFrame frame) {
JInternalFrame[] frames = this.getAllFrames();
for (int i = 0; i < frames.length; i++) {
if (frame == frames[i]) {
return true;
}
}
return false;
}

private void tileWindows() {
JInternalFrame[] frames = this.getAllFrames();
if (frames.length == 0)
return;
int frameCount = frames.length;
int rows = (int)Math.sqrt(frameCount);
int cols = frameCount / rows;
int extra = frameCount % rows;
int width = this.getWidth() / cols;
int height = this.getHeight() / rows;
int r = 0;
int c = 0;

for (int i = 0; i < frames.length; i++) {
try {
frames[i].setMaximum(false);
frames[i].setIcon(false);
frames[i].reshape(c * width, r * height, width, height);
r++;
if (r == rows) {
r = 0;
c++;
if (c == cols - extra) {
rows++;
height = this.getHeight() / rows;
}
}
}
catch (PropertyVetoException ex) {
System.out.println(ex);
}
}
}

private void cascadeWindows() {
JInternalFrame[] frames = this.getAllFrames();
if (frames.length == 0)
return;
int x = 0;
int y = 0;
int width = this.getWidth() / 2;
int height = this.getHeight() / 2;
int frameDistance = frames[0].getHeight() - frames[0].getRootPane().getHeight();

for (int i = 0; i < frames.length; i++) {
try {
frames[i].setMaximum(false);
frames[i].setIcon(false);
frames[i].reshape(x, y, width, height);
x += frameDistance;
y += frameDistance;
if (x + width > this.getWidth())
x = 0;
if (y + height > this.getHeight())
y = 0;
} catch (PropertyVetoException ex) {
System.out.println(ex);
}
}
}

private void jbInit() throws Exception {
jMenuWindowTile.setText("Tile");
jMenuWindowTile.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuWindowTile_actionPerformed(e);
}
});
jMenuWindowCascade.setText("Cascade");
jMenuWindowCascade.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuWindowCascade_actionPerformed(e);
}
});
jMenuWindow.setText("Window");
jMenuWindow.add(jMenuWindowTile);
jMenuWindow.add(jMenuWindowCascade);
jMenuWindow.addSeparator();
if (parentMenuBar != null)
int menuCount = parentMenuBar.getComponentCount();
//将WinodwMenu加在parentFrame菜单倒数第二项
parentMenuBar.add(jMenuWindow, (menuCount > 0) ? menuCount - 1: 0);
}

void jMenuWindowTile_actionPerformed(ActionEvent e) {
tileWindows();
}

void jMenuWindowCascade_actionPerformed(ActionEvent e) {
cascadeWindows();
}
}
 
 
eclipse插件开发java编辑器扩展菜单不展示
最近写了一个java编辑器右键菜单的扩展,在eclipse里面调试都是ok得,功能正常,导出插件jar包,放到eclipse的plugins目录后,重启eclipse,扩展的右键菜单没有展示出来。但是在调试过程中都是可以展示的。比较怀疑plugin.xml的问题,请高手指点一下。plugin.xml如下:
        <viewerContribution
            targetID="#CompilationUnitEditorContext"
            id="a.b.editormenu">
         <action
               class="tt.popup.actions.EditorMenu"
               enablesFor="*"
               icon="icon/add2.gif"
               id="a.b.c.editorAction"
               label="submen"
               menubarPath="TT.menu3/group1">
         </action>
         <menu
               id="TT.menu3"
               label="submenu"
               path="additions">
            <separator
                  name="group1">
            </separator>
         </menu>
      </viewerContribution>

extends是继承父类,只要那个类不是声明为final或者那个类定义为abstract的就能继承,JAVA中不支持多重继承,但是可以用接口来实现,这样就要用到implements,继承只能继承一个类,但implements可以实现多个接口,用逗号分开就行了
比如
class A extends B implements C,D,E

一个类通过关键字implements声明自己使用一个或者多个接口。在类的声明中,通过关键字extends来创建一个类的子类。
class 子类名 extends 父类名 implenments 接口名
{...
}

基本上就这样用了。 

 

回答者: cshty - 一级  2006-1-8 18:11

 
 

class 子类名 extends 父类名 implenments 接口名 

[转载]JAVA中extends 与implements有啥区别?

(2010-03-22 10:53:53)
标签:

转载

分类:Java


JAVA中extends 与implements有啥区别?
1. 在类的声明中,通过关键字extends来创建一个类的子类。一个类通过关键字implements声明自己使用一个或者多个接口。
extends 是继承某个类, 继承之后可以使用父类的方法, 也可以重写父类的方法; implements 是实现多个接口, 接口的方法一般为空的, 必须重写才能使用
2.extends是继承父类,只要那个类不是声明为final或者那个类定义为abstract的就能继承,JAVA中不支持多重继承,但是可以用接口来实现,这样就要用到implements,继承只能继承一个类,但implements可以实现多个接口,用逗号分开就行了
比如
class A extends B implements C,D,E

===========================================================
implements
学了好久,今天终于明白了implements,其实很简单,看下面几个例子就ok啦~~
接口的一些概念
public inerface Runner
{
   int ID = 1;
   void run ();
}


interface Animal extends Runner
{
   void breathe ();
}

 

class Fish implements Animal
{
   public void run ()
 {
    System.out.println("fish is swimming");
 }

public void breather()
 {
    System.out.println("fish is bubbing");   
 }
}

abstract LandAnimal implements Animal
{
  
   public void breather ()
 {
    System.out.println("LandAnimal is breathing");
 }
}
 
class Student extends Person implements Runner
{
    ......
    public void run ()
     {
          System.out.println("the student is running");
     }
    ......
}

 

interface Flyer
{
   void fly ();
}
 
class Bird implements Runner , Flyer
{
   public void run ()
    {
        System.out.println("the bird is running");
    }
   public void fly ()
    {
        System.out.println("the bird is flying");
    }
}
 

class TestFish
{
   public static void main (String args[])
    {
       Fish f = new Fish();
       int j = 0;
       j = Runner.ID;
       j = f.ID;
    }

}
接口实现的注意点:

a.实现一个接口就是要实现该接口的所有的方法(抽象类除外)。
b.接口中的方法都是抽象的。
c.多个无关的类可以实现同一个接口,一个类可以实现多个无关的接口。
 ===========================================================
extends与implements的不同

  extends是继承父类,只要那个类不是声明为final或者那个类定义为abstract的就能继承,JAVA中不支持多重继承,但是可以用接口来实现,这样就要用到implements,继承只能继承一个类,但implements可以实现多个接口,用逗号分开就行了
比如
class A extends B implements C,D,E

//
    一个类通过关键字implements声明自己使用一个或者多个接口。在类的声明中,通过关键字extends来创建一个类的子类。
class 子类名 extends 父类名 implenments 接口名
{...

}
 
===========================================================

A a = new B(); 结果a是一个A类的实例,只能访问A中的方法,那么又和A a = new A();有什么区别呢?
==========================================================
class B extends A
继承过后通常会定义一些父类没有的成员或者方法。
A a = new B();
这样是可以的,上传。
a是一个父类对象的实例,因而不能访问子类定义的新成员或方法。
==========================================================
假如这样定义:
class A{
int i;
void f(){}
}
class B extends A{
int j;
void f(){}//重写
void g(){}
}
然后:
B b = new B();
b就是子类对象的实例,不仅能够访问自己的属性和方法,也能够访问父类的属性和方法。诸如b.i,b.j,b.f(),b.g()都是合法的。此时b.f()是访问的B中的f()
A a = new B();
a虽然是用的B的构造函数,但经过upcast,成为父类对象的实例,不能访问子类的属性和方法。a.i,a.f()是合法的,而a.j,a.g()非法。此时访问a.f()是访问B中的f()
==========================================================
A a = new B(); 这条语句,实际上有三个过程:
(1) A a;
将a声明为父类对象,只是一个引用,未分配空间
(2) B temp = new B();
通过B类的构造函数建立了一个B类对象的实例,也就是初始化
(3) a = (A)temp;
将子类对象temp转换未父类对象并赋给a,这就是上传(upcast),是安全的。
经过以上3个过程,a就彻底成为了一个A类的实例。
子类往往比父类有更多的属性和方法,上传只是舍弃,是安全的;而下传(downcast)有时会增加,通常是不安全的。
===========================================================
a.f()对应的应该是B类的方法f()
调用构造函数建立实例过后,对应方法的入口已经确定了。
如此以来,a虽被上传为A类,但其中重写的方法f()仍然是B的方法f()。也就是说,每个对象知道自己应该调用哪个方法。
A a1 = new B();
A a2 = new C();
a1,a2两个虽然都是A类对象,但各自的f()不同。这正是1楼说的多态性的体现。

这类问题在《Java编程思想》上都讲的很清楚

===========================================================

java中能够定义接口 接口是一种变相的抽象类 接口的定义可以实现java中的多继承问题 java中类与类之间只能单继承 关键字 是extends 而对于类来说可以实现接口  关键字就用implements  表示该类实现接口 接口与接口是也可以用extends来继承

JAVA接口的实现。接口是一种特殊的抽象类,如果说抽象类定义了“是什么”,那么接口定义的就是“做什么”。因为是抽象的,所以需要实现类去实现,实现接口的类就需要在定义的时候用“implement”注明继承的哪个类

RCP中hello world模板 和带intro的模板的 区别!

 
 
今天先用 hello world模板 加了一个view视图
在creatPartControl里面加上相应代码
XML里面添加相应代码
最后在Perspective.java里面添加addView

启动运行正常

但是在 带intro的模板里面执行相同的步骤以后

启动运行,相对于代码添加前没有任何变化;

在TestView.java和Perspective.java
添加System.out.println("test");

发现这2段test代码都没有运行

崩溃了 谁能告诉偶是怎么回事呀!
 

zizianzi 离线
级别: CEC高级程序员
显示用户信息 
 
1  发表于: 2006-08-17   
 
 
 
 
今天将PERSPECTIVE_ID = "TestRCP.TestPerspective";
结果还是和昨天一样,没有得到自己的view和edit.

ApplicationWorkbenchAdvisor代码段如下:

package testRCP.intro;

import org.eclipse.ui.application.IWorkbenchConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {

   private static final String PERSPECTIVE_ID = "TestRCP.TestPerspective";

  public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
    return new ApplicationWorkbenchWindowAdvisor(configurer);
  }
 
  public void initialize(IWorkbenchConfigurer configurer) {
    super.initialize(configurer);
    configurer.setSaveAndRestore(true);
  }

   public String getInitialWindowPerspectiveId() {

       return PERSPECTIVE_ID;
   }
}


TestPerspective 代码段如下:

package testRCP;

import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;

import testRCP.intro.TestView;
import testRCP.intro.TestEdit;

public class TestPerspective implements IPerspectiveFactory {

   public void createInitialLayout(IPageLayout layout) {
       
       layout.addStandaloneView(TestView.ID ,true,
               IPageLayout.BOTTOM, 0.30f, layout.getEditorArea());
       
       System.out.println("test");
       
       layout.addStandaloneView(TestEdit.ID ,true,
               IPageLayout.RIGHT, 0.30f, layout.getEditorArea());
   }
}

 

 

那也就是说intro模板的代码都在*.intro目录下,那么应该是不会影响自己添加的view和edit的

看例子上,是直接添加上去的,我是对着例子添加的--论坛置顶的"RCP程序设计".

XML代码也察看了,没有问题.

期待答案!
今天使用view视图又可以实现自己的视图了,算了 就用view视图好了!估计是在用intro视图的时候,哪个地方出了点小问题

有一个地方就是:hello和view视图的文件都在项目文件名的节点下面

intro视图的大部分文件在intro节点下面!

不知道是不是这么说,也许一些名词用错了,但意思就是那样.
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值