一个简单的文本编辑器

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-23 05:32   11   0

package UserJFrame;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.MenuBar;
import java.awt.Panel;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.font.TextLayout;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Group;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.LayoutStyle;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.plaf.basic.BasicBorders.RadioButtonBorder;
import javax.swing.text.StyledEditorKit.BoldAction;

public class UserJFrame extends JFrame implements ActionListener, MouseListener {
 private JButton btnAdd;
 private JTextArea textarea;
 private JRadioButton btnMan,btnFelman;
 private JTextField tfdNumber,tfdName;
 private JComboBox comProvince,comCitys;
 private int number=1;
 private Object[][] citys;
 private JTextField tfd1;
 private JMenuItem jmenuItemcolor[]=null;
 private JDialog jdialog1,jdialogsave;
 private Font font1;
 private JPopupMenu jpopupmenu;
 private JPanel jpanel1,jpanel2;
 private JComboBox combofonename,combofonesize;
 private JCheckBox checkBold,checkItalic;
 private JRadioButton radioColor[];
 private Font font;
 public UserJFrame(Object[] province, Object[][] citys) {
  super("个人信息统计");
  this.citys=citys;
  this.setBounds(200, 200, 650, 400);
  this.getContentPane().setBackground(Color.lightGray);
  this.getContentPane().setLayout(new BorderLayout());
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  //设置菜单栏
  addMymenu();//调用下面声明的菜单栏相关代码
  jpanel1=new JPanel();//用来存放工具栏的面板
  jpanel2=new JPanel();//用来搞下面的文本区和信息添加区的面板
  JToolBar jtoolbar=new JToolBar();//给工具栏开空间
  //以下为工具栏的内容,包括字体大小设置,字体设置,字形选择框
  //获取系统字体
  GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
  String fonesName[]=ge.getAvailableFontFamilyNames();
  combofonename=new JComboBox(fonesName);
  combofonename.addActionListener(this);//添加字体的监听
  jtoolbar.add(combofonename);
  //字体大小设置
  String fonesSize[]={"20","30","40","50","60","70"};
  combofonesize=new JComboBox(fonesSize);
  combofonesize.addActionListener(this);
  jtoolbar.add(combofonesize);
  combofonesize.setEditable(true);
  //字体选择
  checkBold=new JCheckBox("粗体");
  checkItalic=new JCheckBox("斜体");
  jtoolbar.add(checkBold);
  jtoolbar.add(checkItalic);
  //颜色选择
  String colorStr[]={"红","绿","蓝","黑"};
  radioColor=new JRadioButton[colorStr.length];
  ButtonGroup group=new ButtonGroup();
  for(int i=0;i<colorStr.length;i++){
   radioColor[i]=new JRadioButton(colorStr[i]);
   jtoolbar.add(radioColor[i]);
   group.add(radioColor[i]);
   radioColor[i].addActionListener(this);
  }
  jpanel1.add(jtoolbar);//工具栏加到jpanel1中
  this.getContentPane().add(jpanel1, BorderLayout.NORTH);//设定jpanel1的位置并添加到整个面板中
  this.getContentPane().add(jpanel2);//jpanel2添加进去
  jpanel2.setLayout(new GridLayout(1, 2));//把界面1分2,左边为文本框眼,右边为信息添加部分栏
  textarea=new JTextArea();
  //textarea.setEditable(false);   //如果有这条代码,则文本框不可编辑
  jpanel2.add(new JScrollPane(textarea));
  JPanel jpanel=new JPanel(new GridLayout(7, 1));
  jpanel2.add(jpanel);
  //把各种元素加进右边面板
  tfdNumber=new JTextField("1");
  tfdNumber.setEditable(false);
  jpanel.add(tfdNumber);
  
  tfdName=new JTextField("姓名:");
  jpanel.add(tfdName);
  
  JPanel jpanelrd=new JPanel(new GridLayout(1,2));
  btnMan=new JRadioButton("男",true);
  btnFelman=new JRadioButton("女");
  jpanelrd.add(btnMan);
  jpanelrd.add(btnFelman);
  jpanel.add(jpanelrd);
  ButtonGroup btngroup=new ButtonGroup();// 使其中的按键(男,女)互斥,只能选择一个,所以把他们放在同一个按键组里
  btngroup.add(btnMan);
  btngroup.add(btnFelman);
  //省份组合框
  comProvince=new JComboBox(province);
  comProvince.addActionListener(this);
  jpanel.add(comProvince);
  
  comCitys=new JComboBox(citys[0]);
  jpanel.add(comCitys);
  
  tfd1=new JTextField("备注:");
  jpanel.add(tfd1);
   
  btnAdd=new JButton("添加");
  btnAdd.addActionListener(this);
  jpanel.add(btnAdd);
  //添加鼠标右键快捷菜单
  jpopupmenu=new JPopupMenu();
  String popumenuItem[]={"粘贴","复制","剪切"};
  JMenuItem popumenuItemadd[]=new JMenuItem[popumenuItem.length];
  for(int i=0;i<popumenuItem.length;i++){
   popumenuItemadd[i]=new JMenuItem(popumenuItem[i]);
   jpopupmenu.add(popumenuItemadd[i]);
   popumenuItemadd[i].addActionListener(this);
  }
  textarea.add(jpopupmenu);
  textarea.addMouseListener(this);
  this.setVisible(true);
 }
 private void addMymenu() {
  JMenuBar jmenubar=new JMenuBar();
  setJMenuBar(jmenubar);
  String menustr[]={"文件","编辑","帮助"};
  JMenu menu[]=new JMenu[menustr.length];
  for(int i=0;i<menustr.length;i++){
   menu[i]=new JMenu(menustr[i]);
   jmenubar.add(menu[i]);
  }
  //"文件"菜单的子菜单
  JMenuItem open=new JMenuItem("打开");
  open.setActionCommand("open");
  open.addActionListener(this);
  menu[0].add(open);
  JMenuItem save=new JMenuItem("保存");
  menu[0].add(save);
  
  menu[0].addSeparator();
  
  JMenuItem past=new JMenuItem("粘贴");
  past.addActionListener(this);
  menu[0].add(past);
  
  JMenuItem copy=new JMenuItem("复制");
  copy.addActionListener(this);
  menu[0].add(copy);
  
  JMenuItem cut=new JMenuItem("剪切");
  cut.addActionListener(this);
  menu[0].add(cut);
  
  menu[0].addSeparator();
  
  JMenuItem exit=new JMenuItem("退出");
  exit.setActionCommand("exit");
  exit.addActionListener(this);
  menu[0].add(exit);
  //"编辑 "菜单的子菜单
  JMenu wordstyle=new JMenu("字形");
  menu[1].add(wordstyle);
  JMenu colorstyle=new JMenu("颜色设置");
  menu[1].add(colorstyle);
  //设置“字形的二级菜单”
  String wordstyle1[]={"粗体","斜体"};
  JMenuItem jmenuitem[]=new JMenuItem[wordstyle1.length];
  for(int i=0;i<wordstyle1.length;i++){
   jmenuitem[i]=new JMenuItem(wordstyle1[i]);
   wordstyle.add(jmenuitem[i]);
   jmenuitem[i].addActionListener(this);
  }
  //设置“颜色设置”二级菜单
  String colorstyle1[]={"红","绿","蓝","黑"};
  jmenuItemcolor=new JMenuItem[colorstyle1.length];
  for(int i=0;i<colorstyle1.length;i++){
   jmenuItemcolor[i]=new JMenuItem(colorstyle1[i]);
   colorstyle.add(jmenuItemcolor[i]);
   jmenuItemcolor[i].addActionListener(this);
  }
  
  JMenuItem jmenuitemhelp=new JMenuItem("相关帮助咨询");
  jmenuitemhelp.addActionListener(this);
  menu[2].add(jmenuitemhelp);

 }
 
@Override
 public void actionPerformed(ActionEvent e) {
  if(e.getSource()==btnAdd){
   String aline;
   //添加名字和编号
   aline=this.number+","+tfdName.getText();
   //添加性别
   if(btnMan.isSelected()){
    aline=aline+","+btnMan.getText();
   }
   if(btnFelman.isSelected()){
    aline=aline+","+btnFelman.getText();
   }
   //添加省份
   aline=aline+","+comProvince.getSelectedItem();
   
   //添加市级
   aline=aline+","+comCitys.getSelectedItem();
   //添加备注
   aline=aline+","+tfd1.getText();
   tfd1.setText("备注:");
   
   textarea.append(aline+"\n");
   this.number++;
   tfdNumber.setText(""+this.number);
   tfdName.setText("姓名:");
   
  }
  
  else if(e.getSource()==comProvince){
   int i=comProvince.getSelectedIndex();
   comCitys.removeAllItems();
   System.out.println(citys[i].length);
   for(int j=0;j<citys[i].length;j++){
    comCitys.addItem(citys[i][j]);
   }
  }
  else if(e.getSource() instanceof JMenuItem||e.getSource() instanceof JRadioButton){
   //设置颜色的监听
   Color color=null;  //用传参的思维做颜色
   if(e.getActionCommand().equals("红")){
    color=new Color(255,0,0);
   }
   if(e.getActionCommand().equals("绿")){
    color=new Color(0,255,0);
   }
   if(e.getActionCommand().equals("蓝")){
    color=new Color(0,0,255);
   }
   if(e.getActionCommand().equals("黑")){
    color=new Color(0,0,0);
   }
   textarea.setForeground(color);
   //设置帮助的监听
   if(e.getActionCommand().equalsIgnoreCase("相关帮助咨询")){
    jdialog1=new JDialog(this,true);
    jdialog1.setSize(330, 100);
    int lx=getX()+20;
    int ly=getY()+20;
    jdialog1.setLocation(lx, ly);
    jdialog1.add(new Label("相关帮助请咨询电话10010,最终解释权归联通公司所有!"));
    jdialog1.setVisible(true);
   }
   //设置字形的监听
   if(e.getActionCommand().equals("粗体")){
    font1=new Font(getName(),20, 20);
    textarea.setFont(font1);
   }
   if(e.getActionCommand().equals("斜体")){
    
   }
   //退出的监听
   if(e.getActionCommand().equalsIgnoreCase("exit")){
    int option=JOptionPane.showConfirmDialog(this, "你确定保存并退出么?");
    if(option==JOptionPane.OK_OPTION){
     System.out.println("保存中...");
     System.exit(0);
    }
    if(option==JOptionPane.NO_OPTION){
     System.exit(0);
    }
    if(option==JOptionPane.CANCEL_OPTION){
     return;
    }
    
   }
   //保存的监听
   if(e.getActionCommand().equalsIgnoreCase("复制")){
    textarea.copy();
   }
   //粘贴监听
   if(e.getActionCommand().equalsIgnoreCase("粘贴")){
    textarea.paste();
   }
   //剪切监听
   if(e.getActionCommand().equalsIgnoreCase("剪切")){
    textarea.cut();
   }
   //打开的监听
   if(e.getActionCommand().equals("open")){
    openFile();
   }
  
  } //字形的监听
  /*if(e.getSource() instanceof JComboBox ){
    String fontName=(String) combofonename.getSelectedItem();
    int fontSize=0;
    String strSize=(String) combofonesize.getSelectedItem();
    int style = changeStyle(e);
    font=new Font(fontName, style, fontSize);
    textarea.setFont(font);
  }
 }
 private int changeStyle(ActionEvent e) {
 // TODO Auto-generated method stub
 return 0;*/
 }
 private void openFile() {
  FileDialog openFileDlg = new FileDialog(this, "打开文件", FileDialog.LOAD);
        openFileDlg.setVisible(true);
}
 
 @Override
 public void mouseClicked(MouseEvent e) {
  if(e.getModifiers()==MouseEvent.BUTTON3_MASK){
   jpopupmenu.show(textarea,e.getX(), e.getY());
  }
 }
 public void mousePressed(MouseEvent e) {
 }
 public void mouseReleased(MouseEvent e) {
 }
 public void mouseEntered(MouseEvent e) {
 }
 public void mouseExited(MouseEvent e) {
 }
 public static void main(String[] args) {
  Object province[]={"四川省","湖南省","江苏省"};
  Object citys[][]={{"成都市","遂宁市","南充市","德阳市","绵阳市"},
        {"益阳市","长沙市","株洲市","湘潭市","岳阳市"},
        {"南京市","苏州市","无锡市"}
  };
  new UserJFrame(province,citys);
 }
}<img src="https://img-blog.csdn.net/20160306201345897?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

转载于:https://www.cnblogs.com/asd529735325/p/10216045.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP