返回列表 發帖
  1. package bbs.istak.org.tw;

  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.KeyAdapter;
  5. import java.awt.event.KeyEvent;

  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.JTextField;
  10. import javax.swing.text.BadLocationException;



  11. public class Main implements ActionListener{

  12.         public static void main(String[] args) {
  13.                 // TODO 自動產生的方法 Stub
  14.                 Main app = new Main();
  15.         }
  16.         public void actionPerformed(ActionEvent e) {
  17.                     // TODO 自動產生的方法 Stub
  18.                 if(e.getSource() == btn1)
  19.                                 tf2.setText("面積為: "+Double.parseDouble(tf1.getText())*3.3058+" 平方公尺");
  20.        
  21.                         else if(e.getSource() == btn2)
  22.                         tf2.setText("   ");
  23.                
  24.             }
  25.      
  26.         
  27.         private JFrame fm;
  28.     private JLabel lb1, lb2;
  29.     private JTextField tf1, tf2;
  30.     private JButton btn1, btn2;
  31.     Main()
  32.     {
  33.         lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  34.         lb1.setBounds(0, 10, 215, 30);
  35.         lb2=new JLabel("輸入坪數:");
  36.         lb2.setBounds(10, 40, 60, 40);
  37.         
  38.         tf1=new JTextField();
  39.         tf1.setBounds(70, 45, 134, 30);
  40.         tf1.addKeyListener(new KeyAdapter(){
  41.                 public void KeyReleased (KeyEvent e){
  42.                         if(e.getKeyCode()<96 && e.getKeyCode()>105){{
  43.                                     int str = tf1.getText().length();
  44.                                     try {
  45.                                                
  46.                                                 tf2.setText(tf1.getText(0, --str));
  47.                                         } catch (BadLocationException e1) {
  48.                                                 // TODO 自動產生的 catch 區塊
  49.                                                 e1.printStackTrace();
  50.                                         }
  51.                    
  52.                             }
  53.                            
  54.                     }
  55.                                 }

  56.         });
  57.    
  58.         tf2=new JTextField();
  59.         tf2.setBounds(10, 85, 195, 40);
  60.         tf2.setEditable(false);
  61.         
  62.         btn1=new JButton("確定");
  63.         btn1.setBounds(10, 135, 92, 25);
  64.         btn1.addActionListener(this);
  65.         btn2=new JButton("清除");
  66.         btn2.setBounds(112, 135, 92, 25);
  67.         btn2.addActionListener(this);
  68.         
  69.         fm=new JFrame("土地面積計算");
  70.         fm.setBounds(100, 100, 220, 200);
  71.         fm.setVisible(true);
  72.         fm.setResizable(false);
  73.         fm.setLayout(null);
  74.         fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  75.         fm.add(lb1);
  76.         fm.add(lb2);
  77.         fm.add(tf1);
  78.         fm.add(tf2);
  79.         fm.add(btn1);
  80.         fm.add(btn2);
  81.    
  82.     }
  83.        
  84. }
複製代碼

TOP

返回列表