Java swing 简易文本编辑器

1、介绍几个相关的网址

精通Java Swing程序设计
BeautyEye社区


2、主要功能

  • tab页能够关闭(快捷键:ctrl+w,鼠标点击x)
  • JMuneItem和Button快键键的几种绑定
  • 状态栏底部的布局
  • 对话框的使用
  • 图片、字体等自定义和使用

3、使用控件

  • JTabbedPane
  • JMenu
  • JToolBar
  • JSeparator
  • GridLayout
  • FlowLayout
  • Font

4、效果图

全局图

这里写图片描述

login对话框

这里写图片描述
这里写图片描述

编辑窗口

这里写图片描述

beautyeye效果

这里写图片描述


5、部分代码

第6点,有全部代码链接

ItgoTabbedPane
package com.itgo.swing.body;

import javax.swing.*;
import java.awt.*;

public class ItgoTabbedPane {
    private int tabIndex = 0;

    public int getTabIndex() {
        return tabIndex;
    }

    private JTabbedPane tabbedPane;

    public JTabbedPane getTabbedPane() {
        return tabbedPane;
    }

    public ItgoTabbedPane() {
        tabbedPane = new JTabbedPane();
    }

    public void addTextAreaTab(String title) {
        // new tab
        this.addTextAreaTab(title, null, null);
    }

    public void addTextAreaTab(String title, Icon icon, Color bgColor) {
        // textArea
        JTextArea textArea = new JTextArea();
        textArea.setBackground(bgColor);
        textArea.setFont(new Font("SimSun", Font.PLAIN, 16));
        // new tab
        new ItgoTabPane(title, textArea, tabIndex++, icon, this);
    }

    public void addTab(String title, Component oneTab) {
        new ItgoTabPane(title, oneTab, tabIndex++, this);
    }

    public void addTab(String title, Component oneTab, Icon icon) {
        new ItgoTabPane(title, oneTab, tabIndex++, icon, this);
    }

    public void removeTab(Component compent) {
        int index = tabbedPane.indexOfComponent(compent);
        System.out.println("closing index:"+index);
        if (index < 0) {
            System.out.println("closing error");
        } else {
            tabbedPane.removeTabAt(index);
            this.tabIndex--;
        }
    }

    public void removeTab(int index) {
        System.out.println("closing index:"+index);
        if (index < 0 || index > tabIndex) {
            System.out.println("closing error");
        } else {
            tabbedPane.removeTabAt(index);
            this.tabIndex--;
        }
    }
}

ItgoTabPane

package com.itgo.swing.body;

import com.itgo.swing.component.button.TabCloseButton;

import javax.swing.*;
import java.awt.*;

public class ItgoTabPane {
    private int index;
    private JScrollPane scrollPane;
    private ItgoTabbedPane itgoTabbedPane;

    public ItgoTabPane(String title, Component oneTab, int index, ItgoTabbedPane itgoTabbedPane) {
        this(title, oneTab, index, null, itgoTabbedPane);
    }

    public ItgoTabPane(String title, Component oneTab, int index, Icon icon, ItgoTabbedPane itgoTabbedPane) {

        this.index = index;
        this.itgoTabbedPane = itgoTabbedPane;
        // scroll pane
        scrollPane = new JScrollPane();
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setViewportView(oneTab);

        itgoTabbedPane.getTabbedPane().addTab(title, icon, scrollPane, "tab_" + index);

        itgoTabbedPane.getTabbedPane().setTabComponentAt(index, tabTitle(title));
    }


    private JPanel tabTitle(String title) {
        // title
        JPanel panel_tab = new JPanel();
//        panel_tab.setLayout(new GridLayout(1, 2, 10, 0));
        panel_tab.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel_tab.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
        //不画出panel的边界
        panel_tab.setOpaque(false);

        // label title1
        JLabel label_title = new JLabel(title);
        label_title.setHorizontalAlignment(JLabel.LEFT);
        label_title.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
        label_title.setOpaque(false);
        panel_tab.add(label_title);
        TabCloseButton closeButton = new TabCloseButton(scrollPane, itgoTabbedPane);
        panel_tab.add(closeButton);
        return panel_tab;
    }

}

tab上关闭按钮

package com.itgo.swing.component.button;

import com.itgo.swing.body.ItgoTabbedPane;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TabCloseButton extends JButton implements ActionListener {
    private final int size = 16;
    private ItgoTabbedPane itgoTabbedPane;
    private JScrollPane scrollPane;

    public TabCloseButton(JScrollPane scrollPane, ItgoTabbedPane itgoTabbedPane) {
        this.itgoTabbedPane = itgoTabbedPane;
        this.scrollPane = scrollPane;
        setPreferredSize(new Dimension(size, size));
        this.setForeground(Color.RED);
        this.setBackground(Color.RED);
        this.setText("X");
        //设置按键的提示信息
        setToolTipText("关闭窗口");
        //设置按键的绘制于普通按键相同
//        setUI(new BasicButtonUI());
        //不对Button进行填充,就是按键是透明的
        setContentAreaFilled(false);
        //按键不能获得焦点
        setFocusable(false);
        //设置按键的边框为雕刻样式
        setBorder(BorderFactory.createEtchedBorder());
        //系统不自动绘制按键边界(这个边界在鼠标放上去之后才绘制)
        setBorderPainted(false);

        this.addActionListener(TabCloseButton.this);

        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                //鼠标移入按键,绘制按键边界
                Component c = e.getComponent();
                if (c instanceof AbstractButton)
                    ((AbstractButton) c).setBorderPainted(true);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                //鼠标移出按键,不绘制按键边界
                Component c = e.getComponent();
                if (c instanceof AbstractButton)
                    ((AbstractButton) c).setBorderPainted(false);
            }
        });

        this.registerKeyboardAction(e -> {
            // remove tab
            int selectedIndex = itgoTabbedPane.getTabbedPane().getSelectedIndex();
            System.out.println("selected index:" + selectedIndex);
            itgoTabbedPane.removeTab(selectedIndex);
        }, "remove tab", KeyStroke.getKeyStroke(KeyEvent.VK_W, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("button closing");
        itgoTabbedPane.removeTab(scrollPane);
    }

//    @Override
//    public void paintComponents(Graphics g) {
//        super.paintComponents(g);
//        //创建一个graphics2D,因为需要在Button上画差
//        Graphics2D g2 = (Graphics2D) g.create();
//
//        //设置画笔,宽度为2
//        g2.setStroke(new BasicStroke(6));
//        //设置画笔颜色
//        g2.setColor(Color.BLACK);
//        //当鼠标移动到Button上时,画笔为紫色
//        if (getModel().isRollover())
//            g2.setColor(Color.PINK);
//        //绘制差
//        int delta = 10;
//        g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
//        g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
//        //释放画笔资源
//        g2.dispose();
//    }
}


6、全部代码

简易文本编译器,源码路径下载

链接: https://pan.baidu.com/s/1wUSA_bc1Pbyvb4z6dYfknA 提取码: xmqp
为了防止百度链接过期,不用再来修改blog,后面会将分享都链接总结在一起 iworkh-百度盘分享

代码所在路径(/share2all/共享全部代码/简易文本编辑器):
这里写图片描述

注意:
1、代码入口SwingDemo的main方法。
2、如果需要用到beautyeye.下载相关jar包。
beautyeye的github地址


7、备注

这是小编,用swing搭着玩的,主要是练习下swing控件的使用,而没有具体的记事本的功能,如(保存文件,加载文件等等)。其中一些方法也是借鉴网上他人blog里的方法。
如果你有什么想法,或者觉得不好的地方,需要加些其他功能的话,可以直接修改代码。大家可以互相学习,美化小工具。(也可以在评论中贴出你完善后的功能和代码链接地址

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值