第10章 Swing UI设计课后题

  没事就练练。。。把代码和结果都贴贴。都是很简单的东西。。。

Code:
  1. package ch10;   
  2.   
  3. import java.awt.BorderLayout;   
  4. import java.awt.Dimension;   
  5. import java.awt.FlowLayout;   
  6. import java.awt.event.ActionEvent;   
  7. import java.awt.event.ActionListener;   
  8. import java.io.BufferedReader;   
  9. import java.io.File;   
  10. import java.io.FileNotFoundException;   
  11. import java.io.FileReader;   
  12. import java.io.IOException;   
  13.   
  14. import javax.swing.JButton;   
  15. import javax.swing.JFrame;   
  16. import javax.swing.JMenu;   
  17. import javax.swing.JMenuBar;   
  18. import javax.swing.JMenuItem;   
  19. import javax.swing.JScrollPane;   
  20. import javax.swing.JTextArea;   
  21. import javax.swing.JTextField;   
  22.   
  23. public class Ch10AllKey {   
  24.     public static void main(String[] args) {   
  25.         new AllKeyUI();   
  26.     }   
  27. }   
  28. class AllKeyUI implements ActionListener{   
  29.     private JButton jb1,jb2,jb4;   
  30.     private JFrame jf;   
  31.     public AllKeyUI(){   
  32.         jf=new JFrame("第十章答案示例集");   
  33.         jf.setBounds(200,200,600,100);   
  34.         jf.setVisible(true);   
  35.            
  36.         jb1=new JButton("第一题示例");   
  37.         jb2=new JButton("第二题示例");   
  38.         jb4=new JButton("第四题示例");   
  39.         jb1.addActionListener(this);   
  40.         jb2.addActionListener(this);   
  41.         jb4.addActionListener(this);   
  42.         jf.setLayout(new FlowLayout());   
  43.         jf.add(jb1);   
  44.         jf.add(jb2);   
  45.         jf.add(jb4);   
  46.         jf.validate();   
  47.         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  48.     }   
  49.     public void actionPerformed(ActionEvent e) {   
  50.       if(e.getSource()==jb1){   
  51.           new Key01();   
  52.       }else if(e.getSource()==jb2){   
  53.           new Key02();   
  54.       }else if(e.getSource()==jb4){   
  55.           new Key04();   
  56.       }   
  57.     }   
  58. }   
  59. //1.编写一个应用程序,在应用程序中有一个按钮和一个文本框,当单击按钮时,文本框   
  60. //显示按钮的名字。   
  61. class Key01{   
  62.     private JFrame jf;   
  63.     private JButton jb;   
  64.     private JTextField jt;   
  65.        
  66.     //构造函数,初始化界面。   
  67.     public Key01(){   
  68.         jf=new JFrame("第十章第一题");   
  69.         jf.setBounds(200,200,400,100);   
  70.         jf.setVisible(true);   
  71.         jf.setLayout(new FlowLayout());   
  72.            
  73.            
  74.         jt=new JTextField("");   
  75.         jt.setPreferredSize(new Dimension(100,30));   
  76.            
  77.         jb=new JButton("我是按钮");   
  78.         jb.addActionListener(new ActionListener(){  //单击在文本框显示按钮的名字。   
  79.             public void actionPerformed(ActionEvent e1){   
  80.                 jt.setText(jb.getText());   
  81.             }   
  82.         });    
  83.         jf.add(jt);   
  84.         jf.add(jb);   
  85.         jf.validate();   
  86.         //jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  87.         jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);   
  88.     }   
  89. }   
  90. //2.编写一个有两个文本框和一个按钮的应用程序,在一个文本框输入一个字符串按回车键或单击按钮,   
  91. //另一个文本框都是显示字符串中每个字符在Unicode表中的顺序。   
  92. class Key02 implements ActionListener{   
  93.     private JFrame jf;   
  94.     private JButton jb;   
  95.     private JTextField jt1,jt2;   
  96.        
  97.     //构造函数,初始化界面。   
  98.     public Key02() {   
  99.         jf=new JFrame("第十章第二题");   
  100.         jf.setBounds(200,200,800,110);   
  101.         jf.setVisible(true);   
  102.         jf.setLayout(new FlowLayout());   
  103.            
  104.            
  105.         jt1=new JTextField("");   
  106.         jt1.setPreferredSize(new Dimension(150,30));   
  107.            
  108.         jt2=new JTextField("");   
  109.         jt2.setPreferredSize(new Dimension(600,30));   
  110.            
  111.         jb=new JButton("测试");   
  112.         /*jb.addActionListener(new ActionListener(){  //单击在文本框显示按钮的名字。  
  113.             public void actionPerformed(ActionEvent e1){  
  114.                 char[] chars=jt1.getText().trim().toCharArray();  
  115.                 String result="";  
  116.                 for(int i=0;i<chars.length;i++){  
  117.                     String temp=""+(int)chars[i]+"   ";  
  118.                     result=result.concat(temp);  
  119.                 }  
  120.                 if(!result.equals("")){  
  121.                     System.out.println(result);  
  122.                     jt2.setText(result);  
  123.                 }  
  124.             }  
  125.         }); */         
  126.         jb.addActionListener(this);   
  127.         jt1.addActionListener(this);   
  128.         jf.add(jt1);   
  129.         jf.add(jt2);   
  130.         jf.add(jb);   
  131.         jf.validate();   
  132.         //jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  133.         jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);   
  134.     }   
  135.     public void actionPerformed(ActionEvent e1){   
  136.         if(e1.getSource()==jb||e1.getSource()==jt1){   
  137.             char[] chars=jt1.getText().trim().toCharArray();   
  138.             String result="";   
  139.             for(int i=0;i<chars.length;i++){   
  140.                 String temp=""+(int)chars[i]+"   ";   
  141.                 result=result.concat(temp);   
  142.             }   
  143.             if(!result.equals("")){   
  144.                 System.out.println(result);   
  145.                 jt2.setText(result);   
  146.             }   
  147.         }      
  148.     }   
  149. }   
  150.   
  151. //4.编写一个应用程序,要求有一个含有菜单的窗口,在窗口中有文本区组建。菜单有“打开文件”的菜单项,   
  152. //当单击该菜单项时,使用输入流将一个名字为“hello.txt”文件的内容读入到文本中。   
  153. class Key04 implements ActionListener{   
  154.     private JMenuBar menuBar;   
  155.     private JMenu menu;   
  156.     private JMenuItem menuItem;   
  157.     private JFrame jf;   
  158.     private JTextArea textArea;   
  159.        
  160.     public Key04(){   
  161.         jf=new JFrame("第十章第4题");   
  162.         jf.setBounds(200,200,400,450);   
  163.         jf.setVisible(true);   
  164.            
  165.         menuBar=new JMenuBar();   
  166.         menu=new JMenu("编辑");   
  167.         menuItem=new JMenuItem("打开文件");   
  168.         menuItem.addActionListener(this);   
  169.         menu.add(menuItem);   
  170.         menuBar.add(menu);   
  171.         jf.setJMenuBar(menuBar);   
  172.            
  173.         textArea=new JTextArea();   
  174.         jf.add(new JScrollPane(textArea),BorderLayout.CENTER);   
  175.         jf.validate();   
  176.         //jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  177.         jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);   
  178.     }   
  179.     public void actionPerformed(ActionEvent e) {   
  180.         File readfile=new File("hello.txt");   
  181.         textArea.setText(null);   
  182.         try {   
  183.             FileReader fileReader=new FileReader(readfile);   
  184.             BufferedReader br=new BufferedReader(fileReader);   
  185.             String s="";   
  186.             int i=0;//如果是第一行   
  187.             while((s=br.readLine())!=null){   
  188.                 if(i==0){   
  189.                     textArea.append(s);   
  190.                     i=1;   
  191.                 }else{   
  192.                     textArea.append("/n"+s);   
  193.                 }   
  194.             }   
  195.             fileReader.close();   
  196.             br.close();   
  197.         } catch (FileNotFoundException e1) {   
  198.             e1.printStackTrace();   
  199.         } catch (IOException e2) {   
  200.             e2.printStackTrace();   
  201.         }   
  202.            
  203.     }   
  204. }  

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值