不知道你说的是不是Swing/awt界面GUI。如果是的话可以这样写:
JPopupMenu jPopupMenu1 = new JPopupMenu();//定义
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JMenuItem jMenuItem3 = new JMenuItem();
JMenuItem jMenuItem4 = new JMenuItem();
public static JTable typetb = new JTable();//typetb里面的内容自己定义
public 构造方法()
{
jMenuItem3.setText("全选");
jMenuItem4.setText("刷新");
jMenuItem3.setText("打印");
jMenuItem4.setText("属性");
jMenuItem1.addActionListener(new
TypeFrame_jMenuItem1_actionAdapter(this));//调用事件
jMenuItem2.addActionListener(new
TypeFrame_jMenuItem2_actionAdapter(this));
jMenuItem4.addActionListener(new
TypeFrame_jMenuItem4_actionAdapter(this));
jMenuItem3.addActionListener(new
TypeFrame_jMenuItem3_actionAdapter(this));
}
public void typetb_mouseClicked(MouseEvent e) {
if (typetb.getSelectedRowCount() == 0) {//选中数的操作
jMenuItem3.setEnabled(false);
jMenuItem4.setEnabled(false);
jMenuItem1.setEnabled(false);
jMenuItem2.setEnabled(false);
} else if (typetb.getSelectedRowCount() == 1) {
jMenuItem3.setEnabled(true);
jMenuItem4.setEnabled(true);
jMenuItem1.setEnabled(true);
jMenuItem2.setEnabled(true);
} else if (typetb.getSelectedRowCount() > 1) {//设置选中数大于一的时候按钮可不可以点击
jMenuItem3.setEnabled(true);
jMenuItem4.setEnabled(false);
jMenuItem1.setEnabled(true);
jMenuItem2.setEnabled(false);
}
if (e.getButton() == MouseEvent.BUTTON3) {
jPopupMenu1.show(typetb, e.getX(), e.getY());
}
}
public void jMenuItem4_actionPerformed(ActionEvent e) {
//处理,其他的同理。
}
class TypeFrame_jMenuItem4_actionAdapter implements ActionListener {
private TypeFrame adaptee;
TypeFrame_jMenuItem4_actionAdapter(TypeFrame
adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem4_actionPerformed(e);
}
}//其他的同理
public class TestAction extends JFrame implements ActionListener{
private JButton b1 = new JButton("请点击");
private JButton b2 = new JButton("Button");
private JLabel l = new JLabel("你点击了0次");
private JLabel l2= new JLabel("");
private JPanel p = new JPanel();
private int count = 0;
public TestAction(){
b1.addActionListener(this); // 这就是对“请点击”按钮加了监听
b2.addActionListener(this);
p.add(l);
p.add(b1);
p.add(l2);
p.add(b2);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.add(p);
this.setBounds(100, 100, 400, 150);
this.setVisible(true);
}