java学习之swing和JFC

来源:互联网 发布:通信网络设计师是什么 编辑:程序博客网 时间:2024/05/18 00:43


————————————————————————————————————————————————————————————————————————


————————————————————————————————————————————————————————————————————


JViewport 为数据源提供一个窗口或“视口”,例如,一个文本文件。该数据源为由 JViewport 视图显示的“scrollable 客户端”(即数据模型)。JScrollPane 基本上由JScrollBar、一个 JViewport 以及它们之间的连线组成,

————————————————————————————————————————————————————————————————



JFileChooser 为用户选择文件提供了一种简单的机制。有关使用 JFileChooser 的更多信息,请参阅The Java Tutorial 中的 How to Use File Choosers 一节。

以下代码弹出一个针对用户主目录的文件选择器,其中只显示 .jpg 和 .gif 图像:

    JFileChooser chooser = new JFileChooser();    FileNameExtensionFilter filter = new FileNameExtensionFilter(        "JPG & GIF Images", "jpg", "gif");    chooser.setFileFilter(filter);    int returnVal = chooser.showOpenDialog(parent);    if(returnVal == JFileChooser.APPROVE_OPTION) {       System.out.println("You chose to open this file: " +            chooser.getSelectedFile().getName());    } 

——————————————————————————————————————————————————————————————————————

例子:

package testSwing;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.filechooser.FileNameExtensionFilter;public class testSwing extends JFrame {String str=null;JScrollPane jsp;JTextArea jta;JFileChooser chooser;int returnVal=0;public testSwing(){this.setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE);    //JOptionPane标准对话框JOptionPane.showConfirmDialog(null, "程序开始运行!");     jsp=new JScrollPane();     jta=new JTextArea(50,50);    //组件添加到JScrollPane获取返回当前的 JViewport上面    jsp.getViewport().add(jta);    this.getContentPane().add(jsp);        addWindowListener(new WindowAdapter()   {   public void windowClosing(WindowEvent e)   {   if(JOptionPane.OK_OPTION==JOptionPane.showConfirmDialog(testSwing.this, "真的要退出吗?",   "结束程序!",JOptionPane.YES_NO_CANCEL_OPTION,   JOptionPane.QUESTION_MESSAGE))   {   dispose();   System.exit(0);   }      }   });    }public static void main(String[] args) {// TODO Auto-generated method stubtestSwing dw=new testSwing();dw.setSize(200, 200);dw.setTitle("test");dw.setVisible(true);}}



0 0
原创粉丝点击