import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.datatransfer.*;
class text implements Serializable

 String keyword;//密码
 String textmain;//文本部分
}
public class NotePad extends JFrame implements ActionListener,ListSelectionListener
{
 MenuBar bar;//菜单栏
 Menu file,edit,format,help;
 MenuItem open,dsave,save,newwork,quit,about,fontchoose,colorchoose,backchoose,timeadd,keyopen,keysave,zcancel,copy,paste,cut,delete,selectall,search,autoline;
 MenuShortcut opencut,dsavecut,savecut,newworkcut,quitcut,aboutcut,fontchoosecut,colorchoosecut,backchoosecut,timeaddcut,keyopencut,keysavecut,zcancelcut,copycut,pastecut,cutcut,deletecut,selectallcut,searchcut,autolinecut; JTextArea textarea;//文本区域
 JDialog dialog,fontdialog,keydialog,searchdialog;//问是否 JScrollPane listscroll,textscroll;
 JPanel fontbuttonpanel,fontmainpanel,fontsizepanel,searchpanel,searchbuttonpanel,searchtextpanel,changepanel,changetextpanel,changebuttonpanel;
 GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
 String fontname[]=ge.getAvailableFontFamilyNames();//字体库
 boolean havesave=true;//是否已存
 boolean enterquit=false;//用来判定对话框之前是否按退出
 JButton yes,no,cancel,ok,can,keyok,searchok,searchcancel,changeok,changeall;//对话框按钮
 JLabel tip,fonttype,fontsize,searchtip,changetip;//提示
 FileDialog filedialog;//输入输出对话框
 text text;//存储对象
 String opensource="";//用来判断是否已保存
 String texttype="宋体";//字体
 String direct="";//保存路径
 String canceltext="";//用来撤销
 int size=18;//字体大小
 int para=0;//字体粗斜
 Font font=new Font(texttype,Font.PLAIN,size);
 Color fontcolor=Color.black;//字体颜色
 Color backcolor=Color.white;//背景颜色
 Date nowtime;//现在时间
 char savemode1='s';
 char savemode2='t';//加密保存方式
 public NotePad(String title){
  setTitle(title);//标题
  this.setResizable(true);//大小可调
  this.setBounds(new Rectangle(150,100,800,550));//窗口大小
  this.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e)
    {System.out.println("程序关闭");
     System.exit(0);
    }
   public void windowOpened(WindowEvent e)
    {System.out.println("程序打开");
       }
   });//程序关闭监听
   textarea=new JTextArea();
   textarea.setLineWrap(false);
   textarea.setFont(font);
   textarea.setForeground(fontcolor);
   textarea.setBackground(backcolor);
   textscroll=new JScrollPane(textarea);
   this.add(textscroll);                     //文本域
//------------------------------------------------
       //密码对话框
   yes=new JButton("是");
   no=new JButton("否");
   cancel=new JButton("取消");
   tip=new JLabel("文件的文字已经改动,要保存文件吗?");
   dialog=new JDialog(this,"记事本",true);
   dialog.setResizable(false);
   dialog.setLayout(new FlowLayout());
   dialog.setSize(250,100);
   dialog.add(tip);
   dialog.add(yes);
   dialog.add(no);
   dialog.add(cancel);
   yes.addActionListener(this);
   no.addActionListener(this);
   cancel.addActionListener(this);          //保存提示对话框
   ok=new JButton("确定");
   can=new JButton("取消");
      ok.addActionListener(this);
   can.addActionListener(this);                    //字体对话框
//-------------------------------------------------------
   bar=new MenuBar();
   file=new Menu("文件");
   edit=new Menu("编辑");
   format=new Menu("格式");
   help=new Menu("帮助");
   newwork=new MenuItem("新建");
   open=new MenuItem("打开");
   dsave=new MenuItem("保存");
   save=new MenuItem("另存为");
   quit=new MenuItem("退出");
   zcancel=new MenuItem("撤销");
   copy=new MenuItem("复制");
   paste=new MenuItem("粘贴");
   cut=new MenuItem("剪切");
   delete=new MenuItem("删除");
   selectall=new MenuItem("全选");
   search=new MenuItem("搜索/替換");
   timeadd=new MenuItem("时间/日期");
   autoline=new MenuItem("自动换行");
   fontchoose=new MenuItem("字体格式...");
   colorchoose=new MenuItem("字体颜色...");
   backchoose=new MenuItem("背景颜色...");
   about=new MenuItem("关于");
   keyopen=new MenuItem("载入加密文件");
   keysave=new MenuItem("加密保存为");   newworkcut=new MenuShortcut(KeyEvent.VK_N);
   newwork.setShortcut(newworkcut);
   opencut=new MenuShortcut(KeyEvent.VK_O);
   open.setShortcut(opencut);
   dsavecut=new MenuShortcut(KeyEvent.VK_S);
   dsave.setShortcut(dsavecut);
   savecut=new MenuShortcut(KeyEvent.VK_L);
   save.setShortcut(savecut);
   quitcut=new MenuShortcut(KeyEvent.VK_Q);
   quit.setShortcut(quitcut);
   fontchoosecut=new MenuShortcut(KeyEvent.VK_F);
   fontchoose.setShortcut(fontchoosecut);
   colorchoosecut=new MenuShortcut(KeyEvent.VK_Y);
   colorchoose.setShortcut(colorchoosecut);
   backchoosecut=new MenuShortcut(KeyEvent.VK_B);
   backchoose.setShortcut(backchoosecut);
   aboutcut=new MenuShortcut(KeyEvent.VK_H);
   about.setShortcut(aboutcut);
   timeaddcut=new MenuShortcut(KeyEvent.VK_F5);
   timeadd.setShortcut(timeaddcut);
   keyopencut=new MenuShortcut(KeyEvent.VK_P);
   keyopen.setShortcut(keyopencut);
   keysavecut=new MenuShortcut(KeyEvent.VK_K);
   keysave.setShortcut(keysavecut);
   copycut=new MenuShortcut(KeyEvent.VK_C);
   copy.setShortcut(copycut);
   pastecut=new MenuShortcut(KeyEvent.VK_V);
   paste.setShortcut(pastecut);
   cutcut=new MenuShortcut(KeyEvent.VK_X);
   cut.setShortcut(cutcut);
   deletecut=new MenuShortcut(KeyEvent.VK_DELETE);
   delete.setShortcut(deletecut);
   selectallcut=new MenuShortcut(KeyEvent.VK_A);
   selectall.setShortcut(selectallcut);
   zcancelcut=new MenuShortcut(KeyEvent.VK_Z);
   zcancel.setShortcut(zcancelcut);
   searchcut=new MenuShortcut(KeyEvent.VK_F3);
   search.setShortcut(searchcut);
   autolinecut=new MenuShortcut(KeyEvent.VK_F2);
   autoline.setShortcut(autolinecut);   file.add(newwork);
   file.add(open);
   file.add(dsave);
   file.add(save);
   file.addSeparator();
   file.add(keyopen);
   file.add(keysave);
   file.addSeparator();
   file.add(quit);
   edit.add(zcancel);
   edit.addSeparator();
   edit.add(copy);
   edit.add(paste);
   edit.add(cut);
   edit.add(delete);
   edit.addSeparator();
   edit.add(search);
   edit.addSeparator();
   edit.add(selectall);
   edit.add(timeadd);
   format.add(autoline);
   format.addSeparator();
   format.add(fontchoose);
   format.add(colorchoose);
   format.add(backchoose);
   help.add(about);
   bar.add(file);
   bar.add(edit);
   bar.add(format);
   bar.add(help);
   setMenuBar(bar);   newwork.addActionListener(this);
   open.addActionListener(this);
   dsave.addActionListener(this);
   save.addActionListener(this);
   quit.addActionListener(this);
   zcancel.addActionListener(this);
   copy.addActionListener(this);
   paste.addActionListener(this);
   cut.addActionListener(this);
   search.addActionListener(this);
   delete.addActionListener(this);
   selectall.addActionListener(this);
   keyopen.addActionListener(this);
   keysave.addActionListener(this);
   fontchoose.addActionListener(this);
   colorchoose.addActionListener(this);
   backchoose.addActionListener(this);
   timeadd.addActionListener(this);
   autoline.addActionListener(this);
   about.addActionListener(this);      //菜单栏
 }
 //------------------------------------------------------
 public void actionPerformed(ActionEvent e){
 
  
 
  if(e.getSource()==fontchoose){
   fontdialog.setVisible(true);
  }  if(e.getSource()==backchoose){
   this.backchoose();
  }  if(e.getSource()==cancel){
   dialog.setVisible(false);
   enterquit=false;
  }
  if(e.getSource()==no){
   if(!enterquit){
   dialog.setVisible(false);
   textarea.setText("");
   }
   else if(enterquit){
    System.exit(0);
   }
   else{}
  }
  if(e.getSource()==can){
   fontdialog.setVisible(false);
  }
  if(e.getSource()==keyok){
   keydialog.setVisible(false);
  }
 }
 public void valueChanged(ListSelectionEvent e){
  
 }
 public void newwork(){
  if(!havesave){
   dialog.setVisible(true);
  }
  else{
  textarea.setText("");
  direct="";
  canceltext="";//用于撤销
  }
  opensource=textarea.getText();
 }
 
 public void quit(){
  if(!havesave){
   enterquit=true;
   dialog.setVisible(true);
  }
  else{
  System.exit(0);
  }
 } 
 public void backchoose(){//背景颜色
  backcolor=JColorChooser.showDialog(this,"背景颜色选择",textarea.getBackground());
  textarea.setBackground(backcolor);
 } public static void main(String[] args) 
 {
  NotePad NotePad=new NotePad("1.0正式版");
  NotePad.setVisible(true);
 }
}
1。为什么我用中文输入法,往记事本里面打字的时候会出现一个细的小框框,同时显示我输入的字符,很不美观,怎么样才能去掉。
2。我用格式-背景颜色,什么都不做,点开来后直接按X关闭,背景颜色会变成灰色,这个是为什么,我设置的是白色。

解决方案 »

  1.   

    1、似乎与LookAndFeel相关2、因为你用JColorChooser的showDialog的值直接赋给了backcolor
    如果你点进背景颜色,没有选择而取消了话,会被赋值为null
    如果选择了才会被赋值为textarea.getBackground()你点了取消相当于执行了textarea.setBackground(null)
    会以JAVA的默认色彩着色
      

  2.   

    showDialog
    public static Color showDialog(Component component,
                                   String title,
                                   Color initialColor)
                            throws HeadlessException显示有模式的颜色选取器,在隐藏对话框之前一直阻塞。如果用户按下 "OK" 按钮,则此方法隐藏/释放对话框并返回所选颜色。如果用户按下 "Cancel" 按钮或者在没有按 "OK" 的情况下关闭对话框,则此方法隐藏/释放对话框并返回 null。 参数:
    component - 对话框的父 Component
    title - 包含对话框标题的 String
    initialColor - 显示颜色选取器时的初始 Color 设置 
    返回:
    所选颜色;如果用户退出,则返回 null 抛出: 
    HeadlessException - 如果 GraphicsEnvironment.isHeadless() 返回 true。
    另请参见:
    GraphicsEnvironment.isHeadless()
      

  3.   

     public void backchoose(){//背景颜色
      backcolor=JColorChooser.showDialog(this,"背景颜色选择",textarea.getBackground());
      if (backcolor != null) {
        textarea.setBackground(backcolor);
      }
     }
    这个函数建议这样修改
      

  4.   

    [Quote=引用 5 楼 sunyiz 的回复:]Java code
     public void backchoose(){//背景颜色
      backcolor=JColorChooser.showDialog(this,"背景颜色选择",textarea.getBackground());
      if (backcolor != null) {
        textarea.setBackground(backcolor);
      }
     }顶一下