Chinaunix首页 | 论坛 | 博客
  • 博客访问: 369659
  • 博文数量: 100
  • 博客积分: 2586
  • 博客等级: 少校
  • 技术积分: 829
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-09 15:20
个人简介

我是一个Java爱好者

文章分类

全部博文(100)

文章存档

2014年(2)

2013年(7)

2012年(2)

2010年(44)

2009年(28)

2008年(17)

我的朋友

分类: Java

2009-07-20 15:50:19

     JFileChooser类专门用来实现文件存取对话框

:选择一图片添加到JLabel上。

package filechooser;

import java.awt.BorderLayout;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.filechooser.FileNameExtensionFilter;;

publicclass TestJFileChooser extends JFrame

{

    privatestaticfinallongserialVersionUID = 1L;

    JButton bt=new JButton("选择");

    JLabel lb=new JLabel();

    JPanel pl=new JPanel();

    public TestJFileChooser()

    {

       pl.setLayout(new BorderLayout());

       pl.add(bt,"North");

       pl.add(lb,"Center");

       this.getContentPane().add(pl);

       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       bt.addActionListener(new ActionListener()

       {

           publicvoid actionPerformed(ActionEvent e)

           {

              JFileChooser jchooser=new JFileChooser();

              FileNameExtensionFilter filter=new FileNameExtensionFilter

                  ("JPG & GIF Images", "jpg", "gif");

              jchooser.setFileFilter(filter);

              int returnValue=jchooser.showOpenDialog(TestJFileChooser.this);

              if(returnValue==JFileChooser.APPROVE_OPTION)

              {

                  System.out.println("You chose to open this file: " +

                         jchooser.getSelectedFile().getName());

                         String strPath=jchooser.getSelectedFile().getAbsolutePath();

                         ImageIcon icon=new ImageIcon(strPath);

                         lb.setIcon(icon);

              }

           }

       });

    }

    publicstaticvoid main(String[] args)

    {

       // TODO Auto-generated method stub

       TestJFileChooser mainJFrame=new TestJFileChooser();

       mainJFrame.setTitle("TestJFileChooser");

       mainJFrame.setSize(400,400);

       mainJFrame.setVisible(true);

    }

}

计算器界面的程序实例

                                                                                                         

要求:

      单击任何按钮,按钮上面的数字或符号按从左到右的顺序显示在文本框中

      文本框中的文字是右对齐显示

package calc;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class TestCalc extends JFrame implements ActionListener,KeyListener

{

    private static final long serialVersionUID = 1L;

    JTextField tf=new JTextField(20);

    JButton bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt10,bt11,bt12,

           bt13,bt14,bt15,bt16,bt17,bt18,bt19,bt20;

    //按钮旗标

    int flag=0;

    //键盘旗标

    int flagk=0;

    double x=0;

    double y=0;

    double result=0;

    String str="";

    public TestCalc()

    {

       super("计算器");

       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       tf.setHorizontalAlignment(JTextField.RIGHT);

       tf.setText("0");

       tf.setEditable(false);

       Container c=this.getContentPane();

       JPanel pl=new JPanel();

       JPanel pltf=new JPanel();

       //this.setBackground(Color.LIGHT_GRAY);

       bt1=new JButton("1");

       bt1.setForeground(Color.RED);

       bt1.setFont(new Font("",Font.BOLD,20));

       bt1.addActionListener(this);

       bt2=new JButton("2");

       bt2.setForeground(Color.RED);

       bt2.setFont(new Font("",Font.BOLD,20));

       bt2.addActionListener(this);

       bt3=new JButton("3");

       bt3.setForeground(Color.RED);

       bt3.setFont(new Font("",Font.BOLD,20));

       bt3.addActionListener(this);

       bt11=new JButton("+");

       bt11.setForeground(Color.RED);

       bt11.setFont(new Font("",Font.BOLD,20));

       bt11.addActionListener(this);

       bt4=new JButton("4");

       bt4.setForeground(Color.RED);

       bt4.setFont(new Font("",Font.BOLD,20));

       bt4.addActionListener(this);

       bt5=new JButton("5");

       bt5.setForeground(Color.RED);

       bt5.setFont(new Font("",Font.BOLD,20));

       bt5.addActionListener(this);

       bt6=new JButton("6");

       bt6.setForeground(Color.RED);

       bt6.setFont(new Font("",Font.BOLD,20));

        bt6.addActionListener(this);

       bt12=new JButton("-");

       bt12.setForeground(Color.RED);

       bt12.setFont(new Font("",Font.BOLD,20));

       bt12.addActionListener(this);

       bt7=new JButton("7");

       bt7.setForeground(Color.RED);

       bt7.setFont(new Font("",Font.BOLD,20));

       bt7.addActionListener(this);

       bt8=new JButton("8");

       bt8.setForeground(Color.RED);

       bt8.setFont(new Font("",Font.BOLD,20));

       bt8.addActionListener(this);

       bt9=new JButton("9");

       bt9.setForeground(Color.RED);

       bt9.setFont(new Font("",Font.BOLD,20));

       bt9.addActionListener(this);

       bt13=new JButton("*");

       bt13.setForeground(Color.RED);

       bt13.setFont(new Font("",Font.BOLD,20));

       bt13.addActionListener(this);

       bt10=new JButton("0");

       bt10.setForeground(Color.RED);

       bt10.setFont(new Font("",Font.BOLD,20));

       bt10.addActionListener(this);

       bt14=new JButton(".");

       bt14.setForeground(Color.RED);

       bt14.setFont(new Font("",Font.BOLD,20));

       bt14.addActionListener(this);

       bt15=new JButton("/");

       bt15.setForeground(Color.RED);

       bt15.setFont(new Font("",Font.BOLD,20));

       bt15.addActionListener(this);

       bt16=new JButton("=");

       bt16.setForeground(Color.RED);

       bt16.setFont(new Font("",Font.BOLD,20));

       bt16.addActionListener(this);

       bt17=new JButton("清空");

       bt17.setForeground(Color.BLUE);

       bt17.addActionListener(this);

       bt18=new JButton("Backspace");

       bt18.setForeground(Color.BLUE);

       bt18.addActionListener(this);

       bt19=new JButton("");

       bt20=new JButton("");

      

       pltf.add(tf);

       pl.add(bt19);

       pl.add(bt17);

       pl.add(bt20);

       pl.add(bt18);

       pl.add(bt1);

       pl.add(bt2);

       pl.add(bt3);

       pl.add(bt11);

       pl.add(bt4);

       pl.add(bt5);

       pl.add(bt6);

       pl.add(bt12);

       pl.add(bt7);

       pl.add(bt8);

       pl.add(bt9);

       pl.add(bt13);

       pl.add(bt10);

       pl.add(bt14);

       pl.add(bt15);

       pl.add(bt16);

       GridLayout gltf=new GridLayout(1,1,10,5);

       GridLayout gl=new GridLayout(5,4,10,20);

       pltf.setLayout(gltf);      

       pl.setLayout(gl);

       tf.setFocusable(true);

       c.add(pltf,BorderLayout.NORTH);

       c.add(pl,BorderLayout.CENTER);

      

       tf.addKeyListener(this);

    }

    public static void main(String[] args)

    {

       // TODO Auto-generated method stub

       TestCalc mainFrame=new TestCalc();

       //mainFrame.setTitle("计算器界面的实现");

       mainFrame.setSize(300,300);

       mainFrame.setLocation(100,100);

       mainFrame.setVisible(true);

       mainFrame.setResizable(false);

    }

    public void actionPerformed(ActionEvent e)

    {

       //数字键1

       if(e.getSource()==bt1)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(e.getActionCommand().trim());

                  //tf.setFocusable(true);

              }

              else

              {

                  tf.setText(tf.getText().trim()+e.getActionCommand().trim());

                  //tf.setFocusable(true);

              }

           }

       }

       //数字键2

       else if(e.getSource()==bt2)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(e.getActionCommand().trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+e.getActionCommand().trim());

              }

           }

       }

       //数字键3

       else if(e.getSource()==bt3)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(e.getActionCommand().trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+e.getActionCommand().trim());

              }

           }

       }

       //数字键4

       else if(e.getSource()==bt4)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(e.getActionCommand().trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+e.getActionCommand().trim());

              }

           }

       }

       //数字键5

       else if(e.getSource()==bt5)

        {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(e.getActionCommand().trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+e.getActionCommand().trim());

              }

           }

       }

       //数字键6

       else if(e.getSource()==bt6)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(e.getActionCommand().trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+e.getActionCommand().trim());

              }

           }

       }

       //数字键7

       else if(e.getSource()==bt7)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(e.getActionCommand().trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+e.getActionCommand().trim());

              }

           }

       }

       //数字键8

       else if(e.getSource()==bt8)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(e.getActionCommand().trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+e.getActionCommand().trim());

              }

           }

       }

       //数字键9

       else if(e.getSource()==bt9)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(e.getActionCommand().trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+e.getActionCommand().trim());

              }

           }

       }

       //数字键0

       else if(e.getSource()==bt10)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {}

              else

              {

                  tf.setText(tf.getText().trim()+e.getActionCommand().trim());

              }

           }

       }

       //数字键.

       else if(e.getSource()==bt14)

       {

           int dianStart=tf.getText().trim().indexOf('.');

           int dianEnd=tf.getText().trim().lastIndexOf('.');

           if(dianEnd-dianStart>4)

           {

              tf.setText(tf.getText().trim());

           }

           else

           {

              if(tf.getText().length()<0)

              {

                  tf.setText("0"+e.getActionCommand().trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+e.getActionCommand().trim());

              }

           }

       }

       //数字键+

       else if(e.getSource()==bt11)

       {

           if(flag==2||flag==3||flag==4)

           {}

           else

           {

              flag=1;

              if(tf.getText().trim().indexOf('+')==-1)

              {

                  x=Double.valueOf(tf.getText().trim());

                  tf.setText(tf.getText().trim()+"+");

                  y=0d;

              }

              else

              {

                  tf.setText(tf.getText().trim());

              }

           }

       }

       //数字键-

       else if(e.getSource()==bt12)

       {

           if(flag==1||flag==3||flag==4)

           {}

           else

           {

              flag=2;

              if(tf.getText().trim().indexOf('-')==-1)

              {

                  x=Double.valueOf(tf.getText().trim());

                  tf.setText(tf.getText().trim()+"-");

                  y=0d;

              }

              else

              {

                  tf.setText(tf.getText().trim());

              }

           }

       }

       //数字键*

       else if(e.getSource()==bt13)

       {

           if(flag==1||flag==2||flag==4)

           {}

           else

           {

              flag=3;

              if(tf.getText().trim().indexOf('*')==-1)

              {

                  x=Double.valueOf(tf.getText().trim());

                  tf.setText(tf.getText().trim()+"*");

                  y=0d;

              }

              else

              {

                  tf.setText(tf.getText().trim());

              }

           }

       }

阅读(831) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~