巧用JPopupMenu(一)

JPopupMenu是对于Swing开发者是在熟悉不过的组件了,不过大部分人只将其作为右击弹出菜单使用,其实JPopupMenu还可以有很多用途,可以说只要是需要弹出式效果的都可以用其实现。例如我们用开发工具输入import java.util.当输入“.”时会自动弹出提示框选项,以下的小程序展示了这样的效果:
 
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
public class PopupTip extends JFrame {
    String[] messages = new String[] {
                        "getNetworkFromTWaver",
                        "getTreeFromTWaver",
                        "getTableFromTWaver",
                        "getPropertySheetFromTWaver",
                        "getTreeTableFromTWaver",
                        "getChartFromTWaver",
                        "getRPCFromTWaver",
                        "getGUIFrameworkFromTWaver",
                        "getFantasticIdeaFromTWaver",
    };
   
    JLabel label = new JLabel("TWaver make everything easy!");
    JList list = new JList(messages);
    JComponent tip = new JScrollPane(list);
    JTextArea text = new JTextArea();
    JPopupMenu popup = new JPopupMenu();
   
    public PopupTip() {
     super("
www.servasoft.com ");
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(new JScrollPane(text), BorderLayout.CENTER);
        this.tip.setPreferredSize(new Dimension(400, 100));
        this.label.setForeground(Color.BLUE);
        this.label.setHorizontalAlignment(SwingConstants.CENTER);
     this.popup.setLayout(new BorderLayout());
        this.popup.add(label, BorderLayout.NORTH);
        this.popup.add(tip, BorderLayout.CENTER);
       
        this.text.setBackground(Color.BLACK);

 
       this.text.setForeground(Color.GREEN);
        this.text.setCaretColor(Color.WHITE);
       
        this.text.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
             if(popup.isShowing()){
              popup.setVisible(false);
             }else if (e.getKeyCode() == KeyEvent.VK_PERIOD){
                    Point point = text.getCaret().getMagicCaretPosition();
                    if(point != null){
                     popup.show(text, point.x, point.y);
                    }
                    text.requestFocus();
             }
            }
        });
    }
    public static void main(String[] args)  {
        PopupTip ui = new PopupTip();
        ui.setSize(400, 200);
        ui.show();
    }
}
 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给你提供一些思路,但是要注意的是,由于我是语言模型,并不能实际编写代码。你需要自己动手实现。 首先,你需要选择一个 Java GUI 库,比如 Swing 或 JavaFX。这里以 Swing 为例。 1. 创建一个 JFrame 对象,设置窗口的标题和大小。 2. 创建一个 JTextArea 对象,用于显示和编辑文本内容。 3. 在 JTextArea 上添加一个 JPopupMenu,用于实现复制、粘贴、剪切等操作。 4. 在 JPopupMenu 上添加 JMenuItem,分别对应复制、粘贴、剪切操作。 5. 为 JMenuItem 添加 ActionListener,实现对应功能。 下面是具体代码实现的示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class NotePad extends JFrame implements ActionListener { private JTextArea textArea; public NotePad() { super("记事本"); setSize(500, 500); textArea = new JTextArea(); add(new JScrollPane(textArea), BorderLayout.CENTER); JPopupMenu popupMenu = new JPopupMenu(); JMenuItem copyItem = new JMenuItem("复制"); JMenuItem pasteItem = new JMenuItem("粘贴"); JMenuItem cutItem = new JMenuItem("剪切"); copyItem.addActionListener(this); pasteItem.addActionListener(this); cutItem.addActionListener(this); popupMenu.add(copyItem); popupMenu.add(pasteItem); popupMenu.add(cutItem); textArea.setComponentPopupMenu(popupMenu); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("复制")) { textArea.copy(); } else if (e.getActionCommand().equals("粘贴")) { textArea.paste(); } else if (e.getActionCommand().equals("剪切")) { textArea.cut(); } } public static void main(String[] args) { NotePad notepad = new NotePad(); notepad.setVisible(true); } } ``` 这样,一个简单的记事本程序就完成了。你可以在此基础上进一步添加其他功能,比如打开、保存文件,设置字体和颜色等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值