Java_swing_图形化界面实例应用

关于传统awt应用>>
[java]  view plain copy
  1. import java.awt.BorderLayout;  
  2. import java.awt.FlowLayout;  
  3. import java.awt.event.ActionEvent;  
  4. import java.awt.event.ActionListener;  
  5. import java.io.BufferedReader;  
  6. import java.io.File;  
  7. import java.io.FileNotFoundException;  
  8. import java.io.FileReader;  
  9. import java.io.IOException;  
  10.   
  11. import javax.swing.JButton;  
  12. import javax.swing.JFileChooser;  
  13. import javax.swing.JFrame;  
  14. import javax.swing.JLabel;  
  15. import javax.swing.JPanel;  
  16. import javax.swing.JScrollPane;  
  17. import javax.swing.JTextArea;  
  18. import javax.swing.filechooser.FileFilter;  
  19.   
  20. public class OpenFile {  
  21.   
  22.     /** 
  23.      * @param args 
  24.      */  
  25.     public static void main(String[] args) {  
  26.           
  27.         final JFrame f = new JFrame("测试");  
  28.         f.setVisible(true);  
  29.         /* 
  30.          * 关于布局有几种常见方法 
  31.          * BorderLayout分为 南 北 东 西 中 
  32.          * FlowLayout好比一段文字,组件为文字,一个个附加 
  33.          * GridLayout 
  34.          * */  
  35.         f.setLayout(new BorderLayout());  
  36.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭  
  37.           
  38.         JPanel panel = new JPanel();  
  39.         panel.setLayout(new FlowLayout());  
  40.         JButton button = new JButton("打开文本");  
  41.         panel.add(button);  
  42.         JButton button1 = new JButton("保存文本");  
  43.         panel.add(button1);  
  44.           
  45.         final JTextArea txt = new JTextArea(3040);  
  46.         final JLabel label = new JLabel();  
  47.           
  48.         f.add(panel, BorderLayout.NORTH);  
  49.         f.add(new JScrollPane(txt), BorderLayout.CENTER);  
  50.         f.add(label, BorderLayout.SOUTH);  
  51.           
  52.         f.setBounds(5050500500);  
  53.         button.addActionListener(new ActionListener() {  
  54.               
  55.             public void actionPerformed(ActionEvent e) {  
  56.                 JFileChooser file = new JFileChooser(".");  //文件当期目录  
  57.                 file.addChoosableFileFilter(new MyFilter("txt"));//过滤器  
  58.                 int choose = file.showOpenDialog(f);  
  59.                 if(choose == 0){  
  60.                     File ff = file.getSelectedFile();  
  61.                     try {  
  62.                         BufferedReader bufr = new BufferedReader(new FileReader(ff));  
  63.                         String temp = null;  
  64.                         while((temp=bufr.readLine()) != null){  
  65.                             txt.append(temp+"\r\n");  
  66.                         }  
  67.                           
  68.                     } catch (FileNotFoundException e1) {  
  69.                         // TODO Auto-generated catch block  
  70.                         e1.printStackTrace();  
  71.                     } catch (IOException e1) {  
  72.                         // TODO Auto-generated catch block  
  73.                         e1.printStackTrace();  
  74.                     }  
  75.                 }  
  76.                   
  77.             }  
  78.         });  
  79.     }  
  80. }  
  81. class MyFilter extends FileFilter{  
  82.     String postfix; //后缀名  
  83.     MyFilter(String postfix) {  
  84.         this.postfix = postfix;  
  85.     }  
  86.   
  87.     @Override  
  88.     public boolean accept(File f) {//这个文件是否列出  
  89.         if(f.isDirectory())  
  90.             return true;  
  91.         String str = f.getName();  
  92.         int num = str.lastIndexOf('.');  
  93.         if(num >0){  
  94.             if(str.substring(num+1, str.length()).equals(postfix)){  
  95.                 return true;  
  96.             }  
  97.         }  
  98.         return false;  
  99.     }  
  100.   
  101.     @Override  
  102.     public String getDescription() {//描叙  
  103.           
  104.         return "*."+postfix+"文件";  
  105.     }  
  106.       
  107. }  

[java]  view plain copy
  1. import java.awt.Color;  
  2. import java.awt.Font;  
  3. import java.awt.Graphics;  
  4. import java.awt.event.MouseAdapter;  
  5. import java.awt.event.MouseEvent;  
  6. import java.io.File;  
  7. import java.util.ArrayList;  
  8.   
  9. import javax.swing.ImageIcon;  
  10. import javax.swing.JFrame;  
  11. import javax.swing.JPanel;  
  12. import javax.swing.JScrollPane;  
  13. import javax.swing.JTextArea;  
  14.   
  15. public class Test{  
  16.     public static void main(String[] args){  
  17.           
  18.         new MyPanel();  
  19.     }  
  20. }  
  21. class MyPanel extends JPanel{  
  22.     private static JFrame f = new JFrame("test");  
  23.     private static ArrayList<Shape> al = new ArrayList<Shape>();  
  24.     MyPanel(){  
  25.         f.setVisible(true);  
  26.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  27.         f.add(this);  
  28.         f.setBounds(5050500500);  
  29.           
  30.         addMouseListener(new MouseAdapter() {  
  31.             Shape s = new Shape();  
  32.             public void mousePressed(MouseEvent e) {  
  33.                 s.setPonit1(e.getX(), e.getY());  
  34.             }  
  35.             public void mouseReleased(MouseEvent e) {  
  36.                 s.setPonit2(e.getX(), e.getY());  
  37.                 al.add(s);  
  38.                 repaint();  
  39.             }  
  40.               
  41.         });  
  42.     }  
  43.     @Override  
  44.     public void paint(Graphics g){  
  45.         //g.drawImage(new ImageIcon("/2.jpg").getImage(), 20, 20, 440, 1909, this); //画图片  
  46.         //g.setFont(new Font("楷体", 30, 30));    //设置字体  
  47.         g.setColor(Color.red);                  //设置颜色  
  48.         //g.drawString("我爱你", 50, 50);      //画字符串  
  49.       
  50.         //g.drawLine(shape.getX1(), shape.getY1(), shape.getX2(), shape.getY2());   //直线  
  51.         for(int i=0; i<al.size(); i++){  
  52.             Shape shape = al.get(i);  
  53.             g.drawOval(shape.getX1(), shape.getY1(), shape.getX2()-shape.getX1(), shape.getY2()-shape.getY1());  
  54.             //画椭圆  
  55.         }  
  56.     }  
  57.       
  58. }  
  59. class Shape{  
  60.     private int x1 = 0;  
  61.     private int y1 = 0;  
  62.     private int x2 = 0;  
  63.     private int y2 = 0;  
  64.     private ShapeType type = ShapeType.line;  
  65.     public void setPonit1(int x1, int y1){  
  66.         this.x1 = x1;  
  67.         this.y1 = y1;  
  68.     }  
  69.     public void setPonit2(int x2, int y2){  
  70.         this.x2 = x2;  
  71.         this.y2 = y2;  
  72.     }  
  73.     public int getX1() {  
  74.         return x1;  
  75.     }  
  76.     public int getY1() {  
  77.         return y1;  
  78.     }  
  79.     public int getX2() {  
  80.         return x2;  
  81.     }  
  82.     public int getY2() {  
  83.         return y2;  
  84.     }  
  85. }  
  86. enum ShapeType{  
  87.     line,oval,polygon,rect,string;  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值