java 仿vim工具(JVIM)

本文介绍了如何使用Java Swing库创建一个类似Vim编辑器的界面,包括Vi KeyEvent处理键盘输入、文本区域的设置和基本操作。重点在于展示了如何通过KeyListener实现字符输入并实时更新显示。
摘要由CSDN通过智能技术生成
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout.*;
import java.io.File;
import java.time.*;
import java.time.format.*;
import java.util.*;
import java.util.List;

/*
* this my editor like vim
*/
class JVim {
    public int k;
    public int j;
    public String s;
    public String s1;
    public String s2;

    //public JPanel pnl;
    public JLabel lb;
    public JScrollPane sp;
    public JTextArea area;
    public JFrame frame;
    public JTextField cmdline;
    public final Font fnt = new Font("courier", Font.BOLD, 19);

    //___________________________________

    //constructor
    public JVim() {
        create();
        setUp();
        //	area.setEnabled(false);
        sp.requestFocusInWindow();
        sp.requestFocus();
    }

    public void create() {
        frame = new JFrame();
        //JPanel pnl;
        lb = new JLabel("JLabel");
        area = new JTextArea();
        sp = new JScrollPane(area);
        cmdline = new JTextField();
    }

    public void setUp() {
        area.setRows(20);
        //len=(area.getKeyListeners()).length;
        area.addKeyListener(new ViKeyEvent());
        sp.addKeyListener(new ViKeyEvent());
        area.setColumns(40);
        //area.setLineWrap(true);
        //area.setWrapStyleWord(true);
        area.setTabSize(2);
        area.setFont(fnt);
        area.setText("hollo swing!\n");
        //Font font=new Font("courier",Font.BOLD,20);
        cmdline.setFont(fnt);
        cmdline.requestFocus();
        cmdline.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    area.append(cmdline.getText() + "\n");
                    cmdline.setText("");
                }
            });
        //BorderLayout layout = new BorderLayout();
        frame.setSize(200, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(sp, BorderLayout.CENTER);
        frame.add(cmdline, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }

    //___________________________________
    //this is for the KeyListener inner class
    //key event for sp
    public class ViKeyEvent extends KeyAdapter {
        public void keyTyped(KeyEvent e) {
            char c = e.getKeyChar();
            String s = String.valueOf(c);

            //"\uA1F5" is a white block
            //"\uA1F6" is a black block
            String w = "\uA1F5";
            String b = "\uA1F6";
            s = s + w;
            System.out.println(s);
            area.append(s);
        }
    }

				/*
    public static void main(String[] Args) {
    
				}
				*/
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值