java课程设计-文本编辑器

来源:互联网 发布:炒股如何看k线图 知乎 编辑:程序博客网 时间:2024/05/16 12:26

做这个文本编辑器的时候,分为几步来做;

第一步,先为文本编辑器搭好框架也就是布局。大小用灵活的能够跟着屏幕大小变动的设计,用Dimension dim=getToolkit().getscreenseize来获取屏幕大小然后设置文本编辑器的时候就用这个dim去设。布局的话采用BorderLayout布局管理,将textArea文本框放在中心以便于能随着文本编辑器的拉大缩小而跟着变动,将工具条toolbar放在了north,然后将工具条中的组件一一加上。为了使正题代码更加简洁,我最后用一个函数addmenu()来加菜单。

第二步组件添加的步骤:注意事项 1、之后要用到的组件要在构造函数前面声明,不然后面调不到这个变量。 2、添加textarea的时候最好用this.getcontentpane去加,因为我们用到的JFrame本身就是类似于一个pane。3、个人习惯要实现AcitonLiseter的时候用的是e.getactioncommand所以最好在每一步都设置好命令这样是有好处的,之后你会发现在做颜色和字体的同步时候你会发现这样是挺好用的,之前我就是因为有的想偷懒没设,后面怎么都不能同步弄了好久。大哭哭所以这里特别提醒。4.以为颜色这个东西是互斥的对吧,所以就要做一个buttongroup来使他们互斥。

第三步,实现AcitonListener的分流:因为实现的监听实在是太多了,如果一个个去找的话是非常耗时的,这里采用分流的方式,可以大大减少寻找所需时间。

第四步:实现按钮功能:  注意事项:1、在获取comboxname时要注意设置这个是可以编辑的,也就是说有可能用户会乱输导致程序出错,所以我们这里要捕捉异常用到try-catch语句。在设置字体的时候我用到了一个抽取代码。为什么呢?因为我后面要是菜单中的字体与工具条中的字体样式进行同步这样我就用到了相同的代码,所以就将相同的代码提取出来。同理在颜色按钮哪里也是如此。

第五步:设置快捷菜单就是右键点击弹出快捷菜单及快捷键的添加 如ctrl+c这种类型,我这里做的是ctrl+i 剪切,ctrl+j复制,ctrl+k 粘贴 快捷菜单的步骤是:1声明 2new对象 3挂到容器中去4在容器中监听鼠标动作 5监听鼠标右键被点击时,把快捷菜单show出来,6菜单项的响应动作 7为菜单项设置快捷键


特别说明一下我这里大部分用的的是数组添加组件。这种方法比较灵活。也可以一定程度上减少代码量

到这里基本上就可以完成了。具体的代码bug调试我在后面说明



下面是我已经可以实现的代码 具体的我会在后面用//备注。

界面如下:




代码bug:1 在向comboxSize中输入错误信息或者其他值按回车的时候会出现两次错误提示或者加入两个数,我们用来解决这个bug,这个bug的原理是jcombox组件事件响应的时候会在内容更新之后自动再调用一次即使是在编辑框中输入后回车并没有执行insertitem也会调用,因此我们通过卫条件过滤掉自动调用的那一次事件响应。


bug2:一开始我们点击运行程序颜色并没有直接是红色 要点击一次之后才能变,所以我们在menu()里面将红色的radiobutton设为默认的,这样就成功解决了。


package cn.hncu.gui5;



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;


public class EditorJFrame extends JFrame implements ActionListener, MouseListener {
private JTextArea text;
private JComboBox comboxName, comboxSize;
private JCheckBox chkBold, chkItalic;
private JRadioButton radioColor[];
private Font font=null;
private JCheckBoxMenuItem chkMenuItems[];
private JRadioButtonMenuItem[] radioMenuItem;
private JPopupMenu popMenu;


public EditorJFrame() {
super("文本编辑器");
Dimension dim = getToolkit().getScreenSize();
this.setBounds(dim.width / 4, dim.height / 4, dim.width / 2,
dim.height / 2);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);//实现窗口关闭按钮


JToolBar toolbar = new JToolBar();
this.getContentPane().add(toolbar, BorderLayout.NORTH);//加工具条


GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();//获取本地字体并添加ActiomListener
String fontsName[] = ge.getAvailableFontFamilyNames();
comboxName = new JComboBox(fontsName);
toolbar.add(comboxName);
comboxName.addActionListener(this);


String[] sizeStr = { "20", "30", "40", "50", "60", "70" };//做字体大小的combox组件并添加ActiomListener
comboxSize = new JComboBox(sizeStr);
toolbar.add(comboxSize);
comboxSize.setEditable(true);
comboxSize.addActionListener(this);


chkBold = new JCheckBox("粗体");//做两个字体样式的组件并添加ActiomListener
chkItalic = new JCheckBox("斜体");
toolbar.add(chkBold);
chkBold.setActionCommand("bold");
chkBold.addActionListener(this);
toolbar.add(chkItalic);
chkItalic.setActionCommand("italic");
chkItalic.addActionListener(this);





text = new JTextArea("welcome 欢迎");//把文本框放进去初始为 welcome 欢迎
this.getContentPane().add(new JScrollPane(text));

String[] strColor = { "红", "绿", "蓝" };
radioColor = new JRadioButton[strColor.length];
ButtonGroup group = new ButtonGroup();
for (int i = 0; i < radioColor.length; i++) {
radioColor[i] = new JRadioButton(strColor[i]);
toolbar.add(radioColor[i]);
group.add(radioColor[i]);
radioColor[i].addActionListener(this);
}
radioColor[0].setActionCommand("red");
radioColor[1].setActionCommand("green");
radioColor[2].setActionCommand("blue");
radioColor[0].setSelected(true);
text.setForeground(Color.red);


addMyMenu();



this.setVisible(true);
}


private void addMyMenu() {
JMenuBar menubar=new JMenuBar();
setJMenuBar(menubar);
String[] menuStr={"文件","编辑","帮助"};
JMenu menu[]=new JMenu[menuStr.length];
for(int i=0;i<menu.length;i++){
menu[i]=new JMenu(menuStr[i]);
menubar.add(menu[i]);
}

//文件菜单
String[] menu0Str={"打开","保存","退出"};
JMenuItem[] menuText=new JMenuItem[menu0Str.length];
for(int i=0;i<menuText.length;i++){
menuText[i]=new JMenuItem(menu0Str[i]);
menu[0].add(menuText[i]);
if(i==1){
menu[0].addSeparator();
}
}
menuText[2].setActionCommand("exit");
menuText[2].addActionListener(this);

JMenu menuStyle = new JMenu("字形");
menu[1].add(menuStyle);//菜单添加菜单,被添加的即是二级菜单



String[] menuFont={"粗体","斜体"};
chkMenuItems=new JCheckBoxMenuItem[menuFont.length];
for(int i=0;i<chkMenuItems.length;i++){
chkMenuItems[i]=new JCheckBoxMenuItem(menuFont[i]);
menuStyle.add(chkMenuItems[i]);
chkMenuItems[i].addActionListener(this);
}
//设置命令
chkMenuItems[0].setActionCommand("bold");
chkMenuItems[1].setActionCommand("italic");



//编辑-颜色菜单
JMenu menuColor= new JMenu("颜色");
menu[1].add(menuColor);
String[] colorStr={"红","绿","蓝"};
radioMenuItem=new JRadioButtonMenuItem[colorStr.length];
ButtonGroup group=new ButtonGroup();
for(int i=0;i<radioMenuItem.length;i++){
radioMenuItem[i]=new JRadioButtonMenuItem(colorStr[i]);
menuColor.add(radioMenuItem[i]);
group.add(radioMenuItem[i]);
radioMenuItem[i].addActionListener(this);
}
radioMenuItem[0].setActionCommand("red");
radioMenuItem[1].setActionCommand("green");
radioMenuItem[2].setActionCommand("blue");



popMenu =new JPopupMenu();
String[] strPopMenu={"剪切","复制","粘贴"};
JMenuItem[] popMenuItems=new JMenuItem[strPopMenu.length];
for(int i=0;i<popMenuItems.length;i++){
popMenuItems[i]=new JMenuItem(strPopMenu[i]);
popMenu.add(popMenuItems[i]);
popMenuItems[i].addActionListener(this);
}
popMenuItems[0].setActionCommand("cut");
popMenuItems[1].setActionCommand("copy");
popMenuItems[2].setActionCommand("paste");


text.add(popMenu);
text.addMouseListener(this);


//设置快捷键
popMenuItems[0].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.CTRL_MASK));
popMenuItems[1].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_J, InputEvent.CTRL_MASK));
popMenuItems[2].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K, InputEvent.CTRL_MASK));

}
boolean isFilte=false;
String str="";
@Override
public void actionPerformed(ActionEvent e) {
if(isFilte&&e.getSource()==comboxSize){
isFilte=false;
return;
}

if (e.getSource() instanceof JComboBox
|| e.getSource() instanceof JCheckBox) {
// 提取字体
String fontName = (String) comboxName.getSelectedItem();
//字体大小
int foneSize = 0;
String strSize = (String) comboxSize.getSelectedItem();
try {
foneSize = Integer.parseInt(strSize);
} catch (NumberFormatException e1) {


String msg = "\"" + strSize + "\"不能转换为整数,请重新输入";
JOptionPane.showMessageDialog(this, msg);
if(!strSize.equals(str)){
str=strSize;
isFilte=true;
}
return;
}

//字体样式

int style = changleStyle(e);
   font = new Font(fontName, style, foneSize);
   text.setFont(font);
}
if(e.getSource()==comboxSize){
String strSize=(String) comboxSize.getSelectedItem();
int size=Integer.parseInt(strSize);
int i=0,n=comboxSize.getItemCount();
while(i<n){
int isSize=Integer.parseInt((String) comboxSize.getItemAt(i));
if(size>isSize){
i++;
}else if(size==isSize){
return;
}else{
break;
}

}
comboxSize.insertItemAt(strSize, i);
}
if(e.getSource() instanceof JMenuItem|| e.getSource() instanceof JRadioButton){

if(e.getActionCommand().equalsIgnoreCase("red")||e.getActionCommand().equalsIgnoreCase("green")||e.getActionCommand().equalsIgnoreCase("blue")){
changeColor(e);
}

if(e.getActionCommand().equals("exit")){
System.exit(0);
}
if(e.getActionCommand().equalsIgnoreCase("bold")||e.getActionCommand().equalsIgnoreCase("italic")){

int style = changleStyle(e);
   font = new Font(text.getFont().getName(), style, text.getFont().getSize());
   text.setFont(font);
}

if(e.getActionCommand().equalsIgnoreCase("cut")){
text.cut();
}
if(e.getActionCommand().equalsIgnoreCase("copy")){
text.copy();
}
if(e.getActionCommand().equalsIgnoreCase("paste")){
text.paste();
}

}




//做菜单字体与工具条中字体形式的同步






}


private void changeColor(ActionEvent e) {
Color color=null;
if(e.getActionCommand().equalsIgnoreCase("red")){
color=new Color(255,0,0);
radioColor[0].setSelected(radioMenuItem[0].isSelected());
radioMenuItem[0].setSelected(radioColor[0].isSelected());
}
if(e.getActionCommand().equalsIgnoreCase("green")){
color=new Color(0,255,0);
radioColor[1].setSelected(radioMenuItem[1].isSelected());
radioMenuItem[1].setSelected(radioColor[1].isSelected());
}
if(e.getActionCommand().equalsIgnoreCase("blue")){
color=new Color(0,0,255);
radioColor[2].setSelected(radioMenuItem[2].isSelected());
radioMenuItem[2].setSelected(radioColor[2].isSelected());


}





text.setForeground(color);
}






private int changleStyle(ActionEvent e) {

int style = text.getFont().getStyle();


if (e.getActionCommand().equalsIgnoreCase("bold") ) {
style = style ^ 1;
}


if (e.getActionCommand().equalsIgnoreCase("italic") ) {
style = style ^ 2;


}
common(style);
return style;
}


private void common(int style) {
boolean sta1,sta2;
sta1=((style & 1)==1);
sta2=((style & 2)==2);
chkMenuItems[0].setSelected(sta1);
chkBold.setSelected(sta1);
chkMenuItems[1].setSelected(sta2);
chkItalic.setSelected(sta2);

}




@Override
public void mouseClicked(MouseEvent e) {
if(e.getModifiers()==MouseEvent.BUTTON3_MASK){
popMenu.show(text, e.getX(), e.getY());
}
}


@Override
public void mousePressed(MouseEvent e) {

}


@Override
public void mouseReleased(MouseEvent e) {

}


@Override
public void mouseEntered(MouseEvent e) {

}


@Override
public void mouseExited(MouseEvent e) {

}
public static void main(String[] args) {
new EditorJFrame();
}

}


0 0
原创粉丝点击