1、介绍几个相关的网址
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里的方法。
如果你有什么想法,或者觉得不好的地方,需要加些其他功能的话,可以直接修改代码。大家可以互相学习,美化小工具。(也可以在评论中贴出你完善后的功能和代码链接地址)
今天的文章Java swing 简易文本编辑器分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/29563.html