有点问题的代码

本文介绍了一个使用Java Swing实现的简易文本编辑器程序。该编辑器包含基本的文件操作(如新建、打开、保存和退出),编辑功能(复制、剪切、粘贴),以及颜色选择等附加功能。通过具体的代码示例展示了如何构建菜单栏和工具栏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个有问题的程序。。

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class EditBook  
{
 public static void main(String[] args) 
 {
  Edit e=new Edit();
  e.run();
 }
}
class Edit extends JFrame
{
 
 JMenuBar jmb;
 JMenu file;
 JMenu compile;
 JMenu tool;
 JMenu help;
 JMenuItem newF;
 JMenuItem open;
 JMenuItem save;
 JMenuItem exit;
 JMenuItem copy;
 JMenuItem cut;
 JMenuItem paste;
 JMenuItem s_all;
 JMenuItem color;
 JMenuItem toolb;
 JMenuItem toolc;
 JMenuItem h;
 JTextArea jta;
 JDialog jd;
 JPanel jp;
 JToolBar jtb;
 String message;
 public void run(){
  jmb=new JMenuBar();
  jp=new JPanel();
  file=new JMenu("文件");
  compile=new JMenu("编辑");
  tool=new JMenu("工具");
  help=new JMenu("帮助");
  
  newF=new JMenuItem("新建");
  open=new JMenuItem("打开");
  save=new JMenuItem("保存");
  exit=new JMenuItem("退出");
  copy=new JMenuItem("Copy");
  cut=new JMenuItem("Cut");
  paste=new JMenuItem("Paste");
  s_all=new JMenuItem("保存全部");
  color=new JMenuItem("Color");
  toolb=new JMenuItem("MS记事本");
  toolc=new JMenuItem("MS计算器");
  
  h=new JMenuItem("关于");
  jtb=new JToolBar("MyJToolBar");

  jta=new JTextArea(20,30);
  /*
  ef.addWindowListener(new WindowAdapter(WindowEvent e){
   System.exit(0);
  });
  */

  //文件
  file.add(newF);
  file.add(open);
  //////////如何显示文件内容
  open.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    jd=new JDialog(new JFrame(),"Color");
    JFileChooser jfc=new JFileChooser();
    String f=jfc.getName();
    System.out.println(f);
    jd.add(jfc);
    jd.pack();
    jd.setVisible(true);
   }
  });
  file.add(save);
  file.add(exit);
  exit.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    System.exit(0);
    }
  });

  //编辑
  compile.add(copy);
  copy.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    jta.copy();
   }
  });
  compile.add(cut);
  cut.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    jta.cut();
   }
  });
  compile.add(paste);
  paste.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    jta.paste();
   }
  });
  compile.add(s_all);
  compile.add(color);
  color.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    jd=new JDialog(new JFrame(),"Color");
    jd.add(new JColorChooser());
    jd.pack();
    jd.setVisible(true);
   }
  });
  
  //工具
  tool.add(toolb);
  tool.add(toolc);
  toolc.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    Calculate cc=new Calculate();
    cc.display();
   }
  });
  
  //帮助
  help.add(h);
  jmb.add(file);
  jmb.add(compile);
  jmb.add(tool);
  jmb.add(help);
  jtb.add(new JButton(new ImageIcon("copy.jpg")));
  jtb.add(new JButton(new ImageIcon("copy.jpg")));
  jtb.add(new JButton(new ImageIcon("copy.jpg")));
  jp.setLayout(new BorderLayout());
  jp.add("North",jtb);
  jp.add("Center",jta);
  this.setJMenuBar(jmb);
  this.add(jp);
  this.pack();
  this.setTitle("简单文本编辑器");
  this.setLocation(100,150);
  this.setVisible(true);
 }
 
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值