今天新做的一个文本编辑器小项目,功能尚有不完善的地方,主要演示快捷菜单的使用
界面展示如下
文本编辑器本来就比较简单,就简单做了一下,之后再把其他功能加上
下面代码:
public class Main {
public static void main(String[] args) {
new TextFrame();
}
}
public class TextFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JToolBar tBar;// 上面的快捷菜单
private JComboBox<String> cbFont;// 字体
private JComboBox<String> cbSize;// 字号(用过的字号可以添加到这里)
private JCheckBox bold;// 粗体
private JCheckBox italic;// 斜体
private Font font;
private String colorString[] = { "黑", "红", "绿", "蓝" };
private Color color[] ;
private JRadioButton rButton[] = new JRadioButton[colorString.length];// 颜色单选按钮
private ButtonGroup bGroup;
private JTextArea tArea;// 文本域
private JMenuBar mBar;// 菜单栏
private JMenu menuFile, menuEdite;// 菜单
private JMenuItem mItemOpen, mItemExit, mItemSave;// 菜单项
public TextFrame() {
super("文本编辑器");
int w = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int h = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
setBounds(w / 4, h / 4, w / 2, h / 2);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container container = getContentPane();
/// 菜单栏
//菜单栏功能尚未完善
mBar = new JMenuBar();
setJMenuBar(mBar);
// 文件
menuFile = new JMenu("文件");
mBar.add(menuFile);
mItemOpen = new JMenuItem("打开");
menuFile.add(mItemOpen);
mItemSave = new JMenuItem("保存");
menuFile.add(mItemSave);
menuFile.addSeparator();
mItemExit = new JMenuItem("退出");
menuFile.add(mItemExit);
// 编辑
menuEdite = new JMenu("编辑");
mBar.add(menuEdite);
/// 快捷菜单
tBar = new JToolBar();
container.add(tBar, BorderLayout.NORTH);
// 字体
// 获取系统字体并添加到组合框
GraphicsEnvironment gEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
String systemFont[] = gEnvironment.getAvailableFontFamilyNames();
cbFont = new JComboBox<String>(systemFont);
cbFont.setEditable(false);
tBar.add(cbFont);
cbFont.addActionListener(this);
// 字号
String fontSize[] = { "20", "30", "40", "50", "60" };
cbSize = new JComboBox<String>(fontSize);
cbSize.setEditable(true);
tBar.add(cbSize);
cbSize.addActionListener(this);
// 粗体斜体(字体样式)
bold = new JCheckBox("粗体");
italic = new JCheckBox("斜体");
tBar.add(bold);
tBar.add(italic);
bold.addActionListener(this);
italic.addActionListener(this);
// 颜色
bGroup = new ButtonGroup();
for (int i = 0; i < rButton.length; i++) {
rButton[i] = new JRadioButton(colorString[i]);
bGroup.add(rButton[i]);
tBar.add(rButton[i]);
rButton[i].addActionListener(this);
}
color = new Color[]{Color.black,Color.red,Color.green,Color.blue};
/// 文本域
tArea = new JTextArea("每一个优秀的人背后都有一段默默努力的时光");
container.add(tArea);
//初始化
cbFont.setSelectedItem("方正舒体");
rButton[0].setSelected(true);
setVisible(true);
}
//监听
@Override
public void actionPerformed(ActionEvent e) {
//字体,大小,样式,
if(e.getSource() instanceof JComboBox || e.getSource() instanceof JCheckBox){//不能带泛型
String fontString = (String) cbFont.getSelectedItem();
String sizeString = (String) cbSize.getSelectedItem();
try {
int size = Integer.parseInt(sizeString);//e
int style = tArea.getFont().getStyle();//获取当前字体样式
//获取字体样式
if(e.getSource() == bold){
style = style^Font.BOLD;
}
if(e.getSource() == italic){
style = style^Font.ITALIC;
}
font = new Font(fontString, style, size);
tArea.setFont(font);
} catch (NumberFormatException e1) {//弹了两次!
JOptionPane.showMessageDialog(TextFrame.this, ""+sizeString+"不是整数,请输入整数");
}
}
if(e.getSource() instanceof JRadioButton){
for (int i = 0; i < colorString.length; i++) {
if(e.getSource() == rButton[i]){
tArea.setForeground(color[i]);
}
}
}
}
}