java 能不能在JButton中加菜单的?

来源:百度知道 编辑:UC知道 时间:2024/05/27 21:02:29
我是刚刚学java的.
我想实现这样的一个功能,就是在一个"设置"的按钮下,当按这个"设置"按钮时,就会有出现相应的菜单供选择.高手请告诉我,能不能这样实现的?我就是想在JButton中加进菜单.如果能的话,麻烦告诉我怎么样做!谢谢...
我很急着要知道怎么做!
谢谢了,谢谢了...
要我是鼠标左键单击一个按钮,然后出现菜单...就像QQ聊天窗口上带有向下的三角型按钮一样,一按,下面就会出现相应的菜单...
谢谢了...

要告诉我具体怎么做啊...
如何隐藏啊,能给例子吗?

我这里有一个例子,说的是鼠标右键单击弹出菜单的,不知道是不是你要的这种类型(来源《Thinking in Java, 3rd》稍作了一下修改)
//:Popup.java
//Creating popup menus with Swing
//<applet code=Popup width=300 height=200></applet>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Popup extends JApplet{
private JPopupMenu popup=new JPopupMenu();
private JTextField t=new JTextField(10);
public void init(){
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(t);
ActionListener al=new ActionListener(){
public void actionPerformed(ActionEvent e){
t.setText(((JMenuItem)e.getSource()).getText());
}
};
JMenuItem m=new JMenuItem("Hither");
m.addActionListener(al);
popup.add(m);
m=new JMenuItem("You");
m.addActionListener(al);
popup.add(m);
m=new JMenuItem("Afar");
m.addActionListener(al);