自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java圖形用戶界面:高級組件綜合例子

開發(fā) 后端
本文主要講解了Java圖形用戶界面的高級組建中和例子,下面是Java編寫的記事本以及相關(guān)源碼。

運(yùn)行結(jié)果截圖如下:

 

  1. /**  
  2. 作者:wwj  
  3. 日期:2012/4/18  
  4. 功能:利用圖像用戶界面高級組件實(shí)現(xiàn)簡單記事本  
  5.  
  6. 說明:  
  7. (1)創(chuàng)建一個(gè)名為“簡單記事本”窗口,在窗口中添加一個(gè)帶有滾動條的文本區(qū)。  
  8. (2)在窗口中添加一個(gè)具有以下菜單的菜單欄:  
  9.    1.“文件”菜單,包含“打開”、“保存”、“退出”的菜單項(xiàng),菜單間加分隔線,  
  10.       添加事件處理方法,使菜單具有打開、保存文件及退出的功能。  
  11.    2.“編輯”菜單,包含“復(fù)制”、“剪切”、“粘貼”的菜單項(xiàng),添加事件處理方法,  
  12.       使菜單具有“復(fù)制”、“剪切”、“粘貼”的功能。  
  13.    3.“格式”菜單,包含“格式設(shè)置”的二級子菜單;而二級“格式設(shè)置”子菜單,  
  14.       包含 “自動換行”、“取消自動換行”、“斷行不斷字”、“取消斷行不斷字”的菜單項(xiàng),添加事件處理方法,  
  15.       設(shè)置文本框自動換行和斷行不斷字的格式。  
  16.     (提示:JTextArea類中,使用setLineWrap(true)方法設(shè)置自動換行,使用setWrapStyleword(true)方法設(shè)置斷行不斷字,參考書本例8.7)  
  17. (3)在窗口中添加工具欄,包含“打開”、“保存”、“復(fù)制”、“剪切”、“粘貼”圖片按鈕的工具欄,  
  18.      添加事件處理方法,單擊相應(yīng)的按鈕能實(shí)現(xiàn)相應(yīng)的功能。  
  19. (4)在窗口中添加彈出式菜單,包含“打開”、“保存”、“復(fù)制”、“剪切”、“粘貼”的菜單項(xiàng),  
  20.      直接添加分隔線,添并加事件處理方法,選擇相應(yīng)的菜單項(xiàng)能實(shí)現(xiàn)相應(yīng)的功能。  
  21. **/ 
  22.  
  23. import java.awt.*;  
  24. import javax.swing.*;  
  25. import java.awt.event.*;  
  26. import java.io.*;  
  27. public class Nodepad extends JFrame  
  28. {  
  29.     private JTextArea editor;  
  30.     private Container c;  
  31.     private Font f=new Font("sanserif",Font.PLAIN,12);  
  32.     //菜單欄成員屬性聲明  
  33.     private JMenuBar mb;        //菜單棒  
  34.     private JMenu fileMenu;     //文件菜單  
  35.     private JMenu editMenu;     //編輯菜單  
  36.     private JMenu formatMenu;   //格式菜單  
  37.     private JMenuItem fileMenuOpen,fileMenuSave,fileMenuExit;   //文件菜單的菜單項(xiàng)  
  38.     private JMenuItem editMenuCopy,editMenuCut,editMenuPaste;   //編輯菜單的菜單項(xiàng)  
  39.     private JMenu formatSet;//格式菜單第一級菜單  
  40.     private JMenuItem lineWrap,cancleLineWrap,wrapStyleWord,cancleWrapStyleWord;//格式菜單的第二級菜單  
  41.  
  42.     //工具欄成員屬性聲明  
  43.     private JToolBar toolBar;   //工具棒  
  44.     private JButton b1,b2,b3,b4,b5; //聲明5個(gè)按鈕,分別為:“打開”、“保存”、“復(fù)制”、“剪切”、“粘貼”  
  45.       
  46.     //彈出式菜單屬性聲明  
  47.     private JPopupMenu pm;  
  48.     private JMenuItem item1,item2,item3,item4,item5;  
  49.  
  50.  
  51.  
  52.     public Nodepad()  
  53.     {  
  54.         super("簡單記事本");  
  55.         setSize(400,300);  
  56.       
  57.         try{  
  58.             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  
  59.         } catch (Exception e){ System.err.println("不能被設(shè)置外觀的原因:"+e);}  
  60.           
  61.  
  62.  
  63.         c=getContentPane();         //創(chuàng)建一個(gè)內(nèi)容面板  
  64.         editor = new JTextArea();   //創(chuàng)建一個(gè)文本區(qū)  
  65.         c.add(new JScrollPane(editor)); //設(shè)置滾動條,并添加到內(nèi)容面板  
  66.  
  67.         //菜單欄的實(shí)現(xiàn)  
  68.         //文件菜單的實(shí)現(xiàn)  
  69.         mb = new JMenuBar();    //創(chuàng)建菜單棒  
  70.         fileMenu = new JMenu("文件(F)");//創(chuàng)建菜單  
  71.         fileMenuOpen = new JMenuItem("打開(O)...Ctrl+O");  
  72.         fileMenuSave = new JMenuItem("保存(S)...Ctrl+S");  
  73.         fileMenuExit = new JMenuItem("退出");  
  74.         JMHandler JM=new JMHandler();       //創(chuàng)建監(jiān)聽器  
  75.         fileMenuOpen.addActionListener(JM); //注冊監(jiān)聽器  
  76.         fileMenuSave.addActionListener(JM);  
  77.         fileMenuExit.addActionListener(JM);  
  78.         fileMenu.add(fileMenuOpen);  
  79.         fileMenu.add(fileMenuSave);  
  80.         fileMenu.addSeparator();    //添加分隔線  
  81.         fileMenu.add(fileMenuExit);  
  82.         fileMenu.setFont(f);        //設(shè)置菜單中文體的字體  
  83.  
  84.           
  85.         //編輯菜單的實(shí)現(xiàn)  
  86.         editMenu = new JMenu("編輯(E)");  
  87.         editMenuCopy = new JMenuItem("復(fù)制(C) Ctrl+C");  
  88.         editMenuCut = new JMenuItem("剪切(T) Ctrl+X");  
  89.         editMenuPaste = new JMenuItem("粘貼(P) Ctrl+v");  
  90.         EMHandler EM=new EMHandler();   //創(chuàng)建監(jiān)聽器  
  91.         editMenuCopy.addActionListener(EM);//注冊監(jiān)聽器  
  92.         editMenuCut.addActionListener(EM);  
  93.         editMenuPaste.addActionListener(EM);  
  94.         editMenu.add(editMenuCopy);  
  95.         editMenu.add(editMenuCut);  
  96.         editMenu.add(editMenuPaste);  
  97.         editMenu.setFont(f);  
  98.  
  99.  
  100.         //格式菜單的實(shí)現(xiàn)  
  101.         formatMenu = new JMenu("格式(O)");            //創(chuàng)建菜單  
  102.         formatSet = new JMenu("格式設(shè)置");          //二級菜單  
  103.         lineWrap = new JMenuItem("自動換行");       //二級菜單項(xiàng)  
  104.         cancleLineWrap = new JMenuItem("取消自動換行");  
  105.         wrapStyleWord = new JMenuItem("斷行不斷字");  
  106.         cancleWrapStyleWord = new JMenuItem("取消斷行不斷字");  
  107.         FMHandler FM=new FMHandler();               //創(chuàng)建事件監(jiān)聽器  
  108.         lineWrap.addActionListener(FM);             //注冊二級菜單項(xiàng)的監(jiān)聽器  
  109.         cancleLineWrap.addActionListener(FM);  
  110.         wrapStyleWord.addActionListener(FM);  
  111.         cancleWrapStyleWord.addActionListener(FM);  
  112.         formatSet.add(lineWrap);  
  113.         formatSet.add(cancleLineWrap);  
  114.         formatSet.addSeparator();   //添加分隔線  
  115.         formatSet.add(wrapStyleWord);  
  116.         formatSet.add(cancleWrapStyleWord);  
  117.         formatMenu.add(formatSet);  
  118.         formatMenu.setFont(f);  
  119.  
  120.  
  121.         //將菜單全部添加菜單棒里  
  122.         mb.add(fileMenu);  
  123.         mb.add(editMenu);  
  124.         mb.add(formatMenu);  
  125.  
  126.         //工具欄的實(shí)現(xiàn)  
  127.         //按鈕分別為:“打開”、“保存”、“復(fù)制”、“剪切”、“粘貼”  
  128.         toolBar =new JToolBar();    //創(chuàng)建工具棒  
  129.         b1= new JButton(new ImageIcon("img/open.gif"));  
  130.         b2= new JButton(new ImageIcon("img/save.gif"));  
  131.         b3= new JButton(new ImageIcon("img/copy.gif"));  
  132.         b4= new JButton(new ImageIcon("img/cut.gif"));  
  133.         b5= new JButton(new ImageIcon("img/paste.gif"));  
  134.         TBHandler TB=new TBHandler();       //創(chuàng)建按鈕監(jiān)聽器  
  135.         b1.addActionListener(TB);   b2.addActionListener(TB);  
  136.         b3.addActionListener(TB);   b4.addActionListener(TB);  
  137.         b5.addActionListener(TB);  
  138.         //把按鈕全部添加到工具棒中  
  139.         toolBar.add(b1);    toolBar.add(b2);  
  140.         toolBar.add(b3);    toolBar.add(b4);  
  141.         toolBar.add(b5);  
  142.  
  143.  
  144.         //彈出菜單的實(shí)現(xiàn)  
  145.         pm =new JPopupMenu();   //創(chuàng)建彈出菜單  
  146.         item1 = new JMenuItem("打開");  
  147.         item2 = new JMenuItem("保存");  
  148.         item3 = new JMenuItem("復(fù)制");  
  149.         item4 = new JMenuItem("剪切");  
  150.         item5 = new JMenuItem("粘貼");  
  151.         JPHandler JP=new JPHandler();  
  152.         item1.addActionListener(JP);  //注冊菜單項(xiàng)的鼠標(biāo)事件監(jiān)聽器  
  153.         item2.addActionListener(JP);  
  154.         item3.addActionListener(JP);  
  155.         item4.addActionListener(JP);  
  156.         item5.addActionListener(JP);  
  157.         editor.addMouseListener(JP);  //注冊文本區(qū)的鼠標(biāo)事件監(jiān)聽器  
  158.         pm.add(item1);  pm.add(item2);  
  159.         pm.add(item3);  pm.add(item4);  
  160.         pm.add(item5);  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.           
  167.         //把菜單欄、工具欄、彈出菜單添加到內(nèi)容面板  
  168.         setJMenuBar(mb);        //顯示菜單欄  
  169.         c.add(toolBar,BorderLayout.NORTH);  
  170.           
  171.  
  172.         setVisible(true);  
  173.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  174.     }  
  175.  
  176.  
  177.  
  178.     //自定義類實(shí)現(xiàn)文件菜單項(xiàng)的事件處理  
  179.     private class JMHandler implements ActionListener  
  180.     {     
  181.         public void actionPerformed(ActionEvent e)  
  182.         {  
  183.             if(e.getSource()==fileMenuOpen){ loadFile(); }  
  184.             else if(e.getSource()==fileMenuSave){ saveFile(); }  
  185.             else{System.exit(0);}  
  186.         }  
  187.  
  188.     }  
  189.  
  190.     public void loadFile()  //打開文件方法  
  191.     {  
  192.         JFileChooser fc=new JFileChooser();  
  193.         int r=fc.showOpenDialog(this);  
  194.         if(r==JFileChooser.APPROVE_OPTION)  
  195.         {  
  196.             File file=fc.getSelectedFile();  
  197.             try{ editor.read(new FileReader(file),null);}  
  198.             catch(IOException e){}  
  199.         }  
  200.     }  
  201.           
  202.     public void saveFile()  //保存文件的方法  
  203.     {  
  204.         JFileChooser fc=new JFileChooser();  
  205.         int r=fc.showSaveDialog(this);  
  206.         if(r==JFileChooser.APPROVE_OPTION)  
  207.         {  
  208.             File file=fc.getSelectedFile();  
  209.             try{ editor.write(new FileWriter(file));}  
  210.             catch(IOException e){}  
  211.         }  
  212.     }  
  213.  
  214.       
  215.     //編輯菜單菜單項(xiàng)的事件處理  
  216.     private class EMHandler implements ActionListener  
  217.     {  
  218.         public void actionPerformed(ActionEvent e)  
  219.         {  
  220.             if(e.getSource()==editMenuCopy)     //實(shí)現(xiàn)復(fù)制功能  
  221.             {  
  222.                 editor.copy();  
  223.                 editor.requestFocus();  
  224.             }  
  225.             else if(e.getSource()==editMenuCut)//實(shí)現(xiàn)剪切功能  
  226.             {  
  227.                 editor.cut();  
  228.                 editor.requestFocus();  
  229.             }  
  230.             else                                //實(shí)現(xiàn)粘貼功能  
  231.             {  
  232.                 editor.paste();  
  233.                 editor.requestFocus();  
  234.             }  
  235.         }  
  236.     }  
  237.     //格式菜單二級菜單的菜單項(xiàng)的事件處理  
  238.     private class FMHandler implements ActionListener  
  239.     {  
  240.         public void actionPerformed(ActionEvent e)  
  241.         {  
  242.             if(e.getSource()==lineWrap){ editor.setLineWrap(true); }  
  243.             else if(e.getSource()==cancleLineWrap) { editor.setLineWrap(false);}  
  244.             else if(e.getSource()==wrapStyleWord) { editor.setWrapStyleWord(true);}  
  245.             else{ editor.setWrapStyleWord(false); }  
  246.         }  
  247.     }  
  248.  
  249.  
  250.     //自定義類實(shí)現(xiàn)工具欄的按鈕事件處理  
  251.     private class TBHandler implements ActionListener  
  252.     {  
  253.         public void actionPerformed(ActionEvent e)  
  254.         {  
  255.             if(e.getSource()==b1){ loadFile(); }        //實(shí)現(xiàn)打開文件功能  
  256.             else if(e.getSource()==b2) {saveFile();}    //實(shí)現(xiàn)保存文件功能  
  257.             else if(e.getSource()==b3)  //文件復(fù)制  
  258.             {  
  259.                 editor.copy();  
  260.                 editor.requestFocus();  
  261.             }  
  262.             else if(e.getSource()==b4) //文件剪切  
  263.             {  
  264.                 editor.cut();  
  265.                 editor.requestFocus();  
  266.             }  
  267.             else                        //文件粘貼  
  268.             {  
  269.                 editor.paste();  
  270.                 editor.requestFocus();  
  271.             }  
  272.         }  
  273.     }  
  274.  
  275.     //自定義類實(shí)現(xiàn)彈出式菜單的事件處理  
  276.     private class JPHandler implements ActionListener,MouseListener  
  277.     {  
  278.         public void actionPerformed(ActionEvent e)  
  279.         {  
  280.             if(e.getSource()==item1){ loadFile(); }     //實(shí)現(xiàn)打開文件功能  
  281.             else if(e.getSource()==item2) {saveFile();} //實(shí)現(xiàn)保存文件功能  
  282.             else if(e.getSource()==item3)   //文件復(fù)制  
  283.             {  
  284.                 editor.copy();  
  285.                 editor.requestFocus();  
  286.             }  
  287.             else if(e.getSource()==item4) //文件剪切  
  288.             {  
  289.                 editor.cut();  
  290.                 editor.requestFocus();  
  291.             }  
  292.             else                        //文件粘貼  
  293.             {  
  294.                 editor.paste();  
  295.                 editor.requestFocus();  
  296.             }  
  297.         }  
  298.  
  299.         public void mouseReleased(MouseEvent e)  
  300.         {  
  301.             if(e.isPopupTrigger())                  //判斷是否按下鼠標(biāo)右鍵  
  302.                 pm.show(editor,e.getX(),e.getY());  //顯示彈出式菜單  
  303.         }  
  304.  
  305.         public void mouseClicked(MouseEvent e){}  
  306.         public void mouseEntered(MouseEvent e){}  
  307.         public void mouseExited(MouseEvent e){}  
  308.         public void mousePressed(MouseEvent e){}  
  309.     }  
  310.  
  311.       
  312.  
  313.     public static void main(String []args)  
  314.     {  
  315.         Nodepad N=new Nodepad();  
  316.     }  
  317.           

 

 

原文鏈接:http://blog.csdn.net/wwj_748/article/details/7475950

【編輯推薦】

  1. Java程序員應(yīng)該遵循的10條戒律
  2. Java核心類庫:內(nèi)部類那點(diǎn)事兒
  3. 在Java程序中調(diào)用Matlab函數(shù)
  4. Java線程:線程安全與不安全
  5. Java學(xué)習(xí)之路:不走彎路,就是捷徑
責(zé)任編輯:林師授 來源: wwj_748的博客
相關(guān)推薦

2012-01-16 11:03:09

javaswing

2012-04-28 10:25:11

JavaSwing

2012-04-24 09:40:42

SwingJava

2012-04-27 15:21:45

JavaSwing

2010-01-15 10:39:32

Firefox 4.0圖形用戶界面

2024-10-28 16:03:24

2024-09-24 10:00:55

2011-06-15 16:36:27

Qt 圖形

2009-06-26 16:05:04

嵌入式Linux

2024-12-23 14:54:47

2012-05-29 14:42:47

Ubuntu 12.0

2009-06-24 14:59:00

圖形bean組件JSF圖形組件

2023-11-29 07:30:08

Python用戶界面

2013-04-10 09:28:24

CSS3CSS

2020-10-15 11:05:59

Java開發(fā)界面

2013-04-25 10:12:02

unity3D手機(jī)游戲引擎

2011-07-28 17:40:04

MySQLMySQL Workb

2012-01-16 16:16:49

JavaSwing

2009-06-10 18:18:43

Java GUI用戶界面

2009-05-26 15:22:14

Linux圖形備份
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號