第16周周四:GUI编程及文件对话框的使用

来源:互联网 发布:xbmc使用教程网络硬盘 编辑:程序博客网 时间:2024/06/05 04:34
项目一:<span style="color: rgb(68, 68, 68); font-family: 'Microsoft YaHei UI'; font-size: 14px; line-height: 22px;">了解文件对话框的使用方法,熟悉GUI中事件驱动的核心步骤。</span>
package com.liang;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;public class FileChooser extends JFrame implements ActionListener{    JButton open=null;    JTextField jtfPath = null;        public static void main(String[] args) {        new FileChooser();    }    public FileChooser(){        this.setLayout(new FlowLayout());                // 按钮初始化        open=new JButton("open");        // 添加监听        open.addActionListener(this);        // 把按钮添加到JFrame容器中        this.add(open);                // 添加文本框控件        jtfPath = new JTextField("选择的文件",40);        jtfPath.setEditable(false);     // 不可编辑        jtfPath.setHorizontalAlignment(JTextField.CENTER);    // 居中        this.add(jtfPath);                // 设置JFrame的大小,可显示,默认关闭按钮        this.setBounds(400, 200, 700, 500);        this.setVisible(true);        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            }    @Override    public void actionPerformed(ActionEvent e) {        // TODO Auto-generated method stub        JFileChooser jfc=new JFileChooser();        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );        jfc.showDialog(new JLabel(), "选择");        File file=jfc.getSelectedFile();                if(file.isDirectory()){            System.out.println("文件夹:"+file.getAbsolutePath());                    }else if(file.isFile()){            System.out.println("文件:"+file.getAbsolutePath());        }        System.out.println(jfc.getSelectedFile().getName());                // 把文件路径显示在文本框中        jtfPath.setText(file.getAbsolutePath());    }}


Swing是目前Java中不可缺少的窗口工具组,是用户建立图形化用户界面(GUI)程序的强大工具。Java Swing组件自动产生各种事件来响应用户行为。如当用户点击按钮或选择菜单项目时,Swing组件会产生一个ActionEvent。Swing组件会产生许多事件,如ActionEvents,ChangeEvents,ItemEvents等,来响应用户的鼠标点击行为,列表框中值的改变,计时器的开始计时等行为。在Java Swing编程中,通过注册监听器,我们可以监听事件源产生的事件,从而在事件处理程序中处理我们所需要处理的用户行为。

GUI事件模式驱动的步骤:

1.  新建一个组件(如JButton)。 
2.  将该组件添加到相应的面板(如JPanel)。 
3.  注册监听器以监听事件源产生的事件(如通过ActionListener来响应用户点击按钮)。 
4.  定义处理事件的方法(如在ActionListener中的actionPerformed中定义相应方法)。 

项目三:实验六 图形用户页面 计算器

package aaa;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Computer implements ActionListener,ItemListener{ public static JFrame jf; public static Container c; public static JTextField jtf1,jtf2,jtf3; public static JButton jb1; public static Choice c2;  public static String fuhao=""; public Computer() {  jf=new JFrame("计算器");  c=jf.getContentPane();  c.setLayout(new FlowLayout());  jtf1=new JTextField(10);  jtf2=new JTextField(10);  jtf3=new JTextField(10);  c2=new Choice();  jb1=new JButton("=");   }  public void Jiemian() {   c.add(jtf1);  c2.addItemListener(this);  c2.add("");  c2.add("+");  c2.add("-");  c2.add("*");  c2.add("/");  c.add(c2);  c.add(jtf2);  jb1.addActionListener(this);  c.add(jb1);  c.add(jtf3);    jf.setLocation(200,100);  jf.setVisible(true);  jf.pack(); } public static void main(String args[]) {  Computer ys=new Computer();  ys.Jiemian(); }public void actionPerformed(ActionEvent e) {  // TODO Auto-generated method stub  double s1=Integer.parseInt(jtf1.getText());  double s2=Integer.parseInt(jtf2.getText());  double result=0;  if(fuhao.equals("+"))  {   result=s1+s2;   String result2=String.valueOf(result);   jtf3.setText(result2);  }  if(fuhao.equals("-"))  {   result=s1-s2;   String result2=String.valueOf(result);   jtf3.setText(result2);  }  if(fuhao.equals("*"))  {   result=s1*s2;   String result2=String.valueOf(result);   jtf3.setText(result2);  }  if(fuhao.equals("/"))  {   result=s1/s2;   String result2=String.valueOf(result);   jtf3.setText(result2);  } }public void itemStateChanged(ItemEvent ie)     {      if(ie.getSource()==c2)      {       String str1=c2.getSelectedItem();       fanhui(str1);      }     }     public  String fanhui(String str2)     {      return fuhao=str2;     }}



0 0
原创粉丝点击