swing JPopupMenu使用和添加事件

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;  
import java.awt.event.MouseEvent;  
import java.util.Vector;  
  

/*
 * 
 * 给弹出菜单做事件,只要每个给菜单项添加事件就行
 */

import javax.swing.*;  
import javax.swing.table.DefaultTableModel;  
public class Jpop extends JFrame implements ActionListener{  
    JTable table;  
    DefaultTableModel tableM;  
    JScrollPane jsp;  
    Vector<String> name = new Vector<String>();  
    Vector<String> data = new Vector<String>();  
    
  
    JPopupMenu jpm; 
    JMenuItem jm1,jm2,jm3;
      
    public static void main(String[] args) {  
        // TODO 自动生成的方法存根  
        new Jpop();  
    }  
    Jpop(){  
        name.add("姓名");  
        name.add("年龄");  
        data.add("张三");  
        data.add("19");  
        tableM = new DefaultTableModel(name,0);  
        tableM.addRow(data);  
        tableM.addRow(data);  
        table = new JTable(tableM);  
        jsp = new JScrollPane(table);  
        
        
        //将菜单条放到弹出菜单中
        jm1 = new JMenuItem("变蓝");
        jm2 = new JMenuItem("变红");
        jm3 = new JMenuItem("变白");
        jm1.setBackground(Color.green);
        jm2.setBackground(Color.green);
        jm3.setBackground(Color.green);
        jpm = new JPopupMenu();
        jpm.add(jm1);
        jpm.add(jm2);
        jpm.add(jm3);
        
        jm1.addActionListener(this);
        jm2.addActionListener(this);
        jm3.addActionListener(this);
        
        
        //右键点击表格事件
        table.addMouseListener(new MouseAdapter() {  
            public void mouseClicked(MouseEvent e){  
                if (e.getButton() == MouseEvent.BUTTON3){  
                    //在table显示  
                    int i = table.rowAtPoint(e.getPoint());  
                    jpm.show(table, e.getX(), e.getY());  
                }  
            }  
        });  
        this.add(jsp);  
        this.setVisible(true);  
        this.setSize(400, 400);  
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);  
    }
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(jm1)){
			jm1.setBackground(Color.blue);
			jm2.setBackground(Color.blue);
			jm3.setBackground(Color.blue);
		}
		if (e.getSource().equals(jm2)){
			jm1.setBackground(Color.red);
			jm2.setBackground(Color.red);
			jm3.setBackground(Color.red);
		}
		if (e.getSource().equals(jm3)){
			jm1.setBackground(Color.white);
			jm2.setBackground(Color.white);
			jm3.setBackground(Color.white);
		}
	}  
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要给Javaswing的bottom添加一个监听使得点击bottom时窗体中出现信息查询的菜单,可以按照以下步骤操作: 1. 首先创建一个JFrame窗体,然后在窗体中添加一个JButton组件作为bottom。 2. 然后给这个JButton组件添加一个ActionListener监听器,这个监听器可以在点击按钮时触发一些操作。 3. 在ActionListener的回调方法中,可以创建一个JPopupMenu对象作为菜单,并将其添加到JFrame窗体中。 4. 然后在JPopupMenu添加一些JMenuItem对象,这些对象可以作为菜单项,用于实现信息查询的功能。 5. 最后,给JMenuItem对象添加一个ActionListener监听器,这个监听器可以在点击菜单项时触发具体的信息查询操作。 示例代码如下: ``` import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MyFrame extends JFrame { public MyFrame() { JButton bottom = new JButton("查询"); bottom.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JPopupMenu menu = new JPopupMenu(); JMenuItem item = new JMenuItem("查询信息"); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 执行信息查询操作 } }); menu.add(item); menu.show(bottom, 0, bottom.getHeight()); } }); getContentPane().add(bottom, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 200); setVisible(true); } public static void main(String[] args) { new MyFrame(); } } ``` 这样点击bottom时就可以弹出查询菜单,并在菜单中选择查询信息菜单项进行信息查询了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值