当前位置: 代码迷 >> Java相关 >> 程序运行出现使用了未经检查或不安全的操作
  详细解决方案

程序运行出现使用了未经检查或不安全的操作

热度:538   发布时间:2012-11-30 22:41:02.0
程序运行出现使用了未经检查或不安全的操作
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class jishiben{
public static void main(String args[]){
   FirstWindow win=new FirstWindow("记事本");
}
}
class FirstWindow extends JFrame implements ActionListener{
   JMenuBar menubar;
   JTextArea text;
   JFileChooser fileChooser;
   JMenu menu,menu1,menu3,menu4;
   JPopupMenu menu2;
   JMenuItem itemOpen,itemSave,itemSave2,itemExit,itemCopy,itemCut,itemPaste,itemQuanxuan,
             itemPCopy,itemPCut,itemPPaste,itemPQuanxuan,itemZi,itemGuanyu;
   JCheckBoxMenuItem itemHang;//复选按钮换行
   FirstWindow(String s){
      setTitle(s);
      setSize(160,170);
      setLocation(600,400);
      setBounds(120,100,600,400);
      text=new JTextArea();
      menubar=new JMenuBar();
      menu=new JMenu("文件");
      menu1=new JMenu("编辑");
      menu3=new JMenu("格式");
      menu4=new JMenu("帮助");
      menu2=new JPopupMenu();
      itemOpen=new JMenuItem("打开");
      itemSave=new JMenuItem("保存");
      itemExit=new JMenuItem("退出");
      itemSave2=new JMenuItem("另存为");
      itemCopy=new JMenuItem("复制");
      itemCut=new JMenuItem("剪切");
      itemPaste=new JMenuItem("粘贴");
      itemQuanxuan=new JMenuItem("全选");
      itemPCopy=new JMenuItem("复制");
      itemPCut=new JMenuItem("剪切");
      itemPPaste=new JMenuItem("粘贴");
      itemPQuanxuan=new JMenuItem("全选");
      itemHang=new JCheckBoxMenuItem("自动换行");
      itemZi=new JMenuItem("字体");
      itemGuanyu=new JMenuItem("关于记事本");
      itemOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
      itemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
      itemCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
      itemCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
      itemPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
      itemPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
      itemQuanxuan.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
      menu.add(itemOpen);
      menu.addSeparator();
      menu.add(itemSave);
      menu.addSeparator();
      menu.add(itemSave2);
      menu.addSeparator();
      menu.add(itemExit);
      menubar.add(menu);
      setJMenuBar(menubar);
      menu1.add(itemCopy);
      menu1.addSeparator();
      menu1.add(itemCut);
      menu1.addSeparator();
      menu1.add(itemPaste);
      menu1.addSeparator();
      menu1.add(itemQuanxuan);
      menubar.add(menu1);
      setJMenuBar(menubar);
      menu2.add(itemPCopy);
      menu2.addSeparator();
      menu2.add(itemPCut);
      menu2.addSeparator();
      menu2.add(itemPPaste);
      menu2.addSeparator();
      menu2.add(itemPQuanxuan);
      menu3.add(itemHang);
      menu3.addSeparator();
      menu3.add(itemZi);
      menubar.add(menu3);
      menu4.add(itemGuanyu);
      menubar.add(menu4);
      setJMenuBar(menubar);
      add(text);
      text.addMouseListener(new MouseAdapter(){
                                          public void mousePressed(MouseEvent e){
                                            if(e.getModifiers()==InputEvent.BUTTON3_MASK)
                                               menu2.show(text,e.getX(),e.getY());
                                           }
     });
      add(new JScrollPane(text),BorderLayout.CENTER);
      setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      itemOpen.addActionListener(this);
      itemExit.addActionListener(this);
      itemSave.addActionListener(this);
      itemSave2.addActionListener(this);
      itemCopy.addActionListener(this);
      itemCut.addActionListener(this);
      itemPaste.addActionListener(this);
      itemQuanxuan.addActionListener(this);
      itemPQuanxuan.addActionListener(this);
      itemPCopy.addActionListener(this);
      itemPCut.addActionListener(this);
      itemPPaste.addActionListener(this);
      itemPQuanxuan.addActionListener(this);
      itemHang.addActionListener(this);
      itemZi.addActionListener(this);
      itemGuanyu.addActionListener(this);
      setVisible(true);
      }
public void actionPerformed(ActionEvent e){
if(e.getSource()==itemCopy||e.getSource()==itemPCopy)
     text.copy();
else if(e.getSource()==itemCut||e.getSource()==itemPCut)
     text.cut();
else if(e.getSource()==itemPaste||e.getSource()==itemPPaste)
     text.paste();
else if(e.getSource()==itemQuanxuan||e.getSource()==itemPQuanxuan){
     int end=text.getText().length();
     text.setSelectionStart(0);
     text.setSelectionEnd(end);
}
else if(e.getSource()==itemOpen){
       JFileChooser chooser=new JFileChooser("D:/");
       int n=chooser.showOpenDialog(null);
       if(n==JFileChooser.APPROVE_OPTION){
             File file=chooser.getSelectedFile();
             try{ FileReader readfile=new FileReader(file);
                   BufferedReader in=new  BufferedReader(readfile);
                   String s=null;
                   while((s=in.readLine())!=null)
                       text.append(s+"\n");
}
catch(IOException ee){}            

}
}
else if(e.getSource()==itemSave||e.getSource()==itemSave2){
      JFileChooser chooser=new JFileChooser("D:/");     
      int n=chooser.showSaveDialog(text);
       if(n==JFileChooser.APPROVE_OPTION){
         try {File file=chooser.getSelectedFile();
             FileWriter tofile=new FileWriter(file + ".txt");
             BufferedWriter out = new BufferedWriter(tofile);
             out.write(text.getText());
             out.flush();      
             out.close(); }
catch (IOException ee){}
             }
}
else if(e.getSource()==itemExit)
      System.exit(0);
else if(e.getSource()==itemHang){
    if(itemHang.isSelected())
         text.setLineWrap(true);
    else
         text.setLineWrap(false);
}
else if(e.getSource()==itemZi){
     class dialog extends JDialog implements ItemListener,ActionListener{
     JComboBox list;
     JTextArea text;
     Font font;
     JButton yes,cancel;
     JComponent com;
     JTextArea textzi;
     dialog(JComponent com){
       this.com=com;
       setModal(true);
       yes=new JButton("Yes");
       cancel=new JButton("cancel");
       yes.addActionListener(this);
       cancel.addActionListener(this);
       textzi=new JTextArea(2,25);
       GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
       String fontName[]=ge.getAvailableFontFamilyNames();
       list=new JComboBox();
       for(int i=0;i<fontName.length;i++)
           list.addItem(fontName[i]);
       list.addItemListener(this);
       Container con=getContentPane();
       con.setLayout(new FlowLayout());
       Box boxH1=Box.createHorizontalBox(),
           boxH2=Box.createHorizontalBox(),
           boxH3=Box.createHorizontalBox();
       Box boxBase=Box.createVerticalBox();
       boxH1.add(list);
       boxH2.add(text);
       boxH3.add(yes);
       boxH3.add(cancel);
       boxBase.add(boxH1);
       boxBase.add(boxH2);
       boxBase.add(boxH3);
       con.add(boxBase);
       setBounds(100,100,280,170);
       validate();
}
public void itemStateChanged(ItemEvent e){
  String name=(String)list.getSelectedItem();
  font=new Font(name,Font.PLAIN,14);
  textzi.setFont(font);
  textzi.setText("字体预览");
}   
public void actionPerformed(ActionEvent e){
   if(e.getSource()==yes){
      com.setFont(font);
      setVisible(false);
}
   else if(e.getSource()==cancel){
      setVisible(false);
}
}
}
     Dialog dialog=new JDialog();
     dialog.setVisible(true);
     
}
else if(e.getSource()==itemGuanyu){
        JLabel lab1,lab2,lab3;
        Dialog dialog=new JDialog();
        dialog.setTitle("关于记事本");
        dialog.setBounds(200,200,250,150);
        dialog.setModal(true);
        dialog.setLayout(new GridLayout(3, 1));
        lab1 = new JLabel("记事本版本");
        lab2 = new JLabel("NQ1.0");
        lab3 = new JLabel("                  2012.11");
        lab1.setHorizontalAlignment(JLabel.CENTER);
    lab2.setHorizontalAlignment(JLabel.CENTER);
    lab3.setHorizontalAlignment(JLabel.CENTER);
        dialog.add(lab1);
        dialog.add(lab2);
    dialog.add(lab3);
        dialog.setVisible(true);
}

}
}
搜索更多相关的解决方案: 记事本  安全  public  import  

----------------解决方案--------------------------------------------------------
人工置顶  这是什么问题啊
----------------解决方案--------------------------------------------------------
  相关解决方案