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

  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;

  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JLabel;
  7. import javax.swing.JTextField;
  8. import javax.swing.text.BadLocationException;

  9. import java.awt.event.KeyAdapter;
  10. import java.awt.event.KeyEvent;



  11. public class Main implements ActionListener{

  12.         public static void main(String[] args) {
  13.                 // TODO 自動產生的方法 Stub
  14.                 Main app = new Main();
  15.         }

  16.         @Override
  17.         public void actionPerformed(ActionEvent e) {
  18.                 // TODO 自動產生的方法 Stub
  19.                 if(e.getSource()==btn1)
  20.         {
  21.             tf2.setText("面積為: "+Double.parseDouble(tf1.getText())*3.3058+" 平方公尺");
  22.         }
  23.                 if(e.getSource()==btn2)
  24.                 {
  25.                         tf1.setText("");
  26.                         tf2.setText("");
  27.                 }
  28.         }
  29.         
  30.         private JFrame fm;
  31.     private JLabel lb1, lb2;
  32.     private JTextField tf1, tf2;
  33.     private JButton btn1, btn2;
  34.     Main()
  35.     {
  36.             lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  37.         lb1.setBounds(0, 10, 215, 30);
  38.         lb2=new JLabel("輸入坪數:");
  39.         lb2.setBounds(10, 40, 60, 40);
  40.         
  41.         tf1=new JTextField();
  42.         tf1.addKeyListener(new KeyAdapter() {
  43.                 @Override
  44.                 public void keyReleased(KeyEvent e) {
  45.                         if(e.getKeyCode()<96 || e.getKeyCode()>105)
  46.                         {
  47.                                 int temp = tf1.getText().length();
  48.                                 try {
  49.                                                 String str = tf1.getText(0, --temp);
  50.                                                 tf1.setText(str);
  51.                                         } catch (BadLocationException e1) {
  52.                                                 // TODO 自動產生的 catch 區塊
  53.                                                 e1.printStackTrace();
  54.                                         }
  55.                                
  56.                         }
  57.                 }
  58.         });
  59.         
  60.         tf1.setBounds(70, 45, 134, 30);
  61.         tf1.addActionListener(this);
  62.         tf2=new JTextField();
  63.         tf2.setBounds(10, 85, 195, 40);
  64.         tf2.setEditable(false);
  65.         
  66.         btn1=new JButton("確定");
  67.         btn1.setBounds(10, 135, 92, 25);
  68.         btn1.addActionListener(this);
  69.         btn2=new JButton("清除");
  70.         btn2.setBounds(112, 135, 92, 25);
  71.         btn2.addActionListener(this);
  72.         
  73.         fm=new JFrame("土地面積計算");
  74.         fm.setBounds(100, 100, 220, 200);
  75.         fm.setVisible(true);
  76.         fm.setResizable(false);
  77.         fm.getContentPane().setLayout(null);
  78.         fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  79.         fm.getContentPane().add(lb1);
  80.         fm.getContentPane().add(lb2);
  81.         fm.getContentPane().add(tf1);
  82.         fm.getContentPane().add(tf2);
  83.         fm.getContentPane().add(btn1);
  84.         fm.getContentPane().add(btn2);
  85.     }

  86. }
複製代碼

TOP

返回列表