返回列表 發帖
  1. import javax.swing.*;
  2. import java.awt.event.ActionListener;
  3. import java.awt.event.ActionEvent;
  4. public class Ch83 implements ActionListener{
  5.         
  6.         private JFrame fm1;
  7.         private JButton btn1,btn2;
  8.         private JTextField tf1,tf2;
  9.         private JLabel lb1,lb2;
  10.         Ch83(){
  11.             JFrame fm1=new JFrame();
  12.             JLabel lb1=new JLabel("一坪=3.3058平方公尺");
  13.             JLabel lb2=new JLabel("輸入坪數:");
  14.             JTextField tf1=new JTextField();
  15.             JTextField tf2=new JTextField();
  16.             JButton btn1=new JButton("確定");
  17.             JButton btn2=new JButton("清除");
  18.             
  19.             tf1.setBounds(70,45,134,30);
  20.             tf1.addActionListener(this);
  21.             tf2.setBounds(10,85,195,40);
  22.             tf2.setEditable(false);
  23.             
  24.             lb1.setBounds(45,10,215,30);
  25.             lb2.setBounds(10,40,60,40);
  26.             
  27.             btn1.setBounds(10,135,92,25);
  28.             btn1.addActionListener(this);
  29.             btn2.setBounds(112,135,92,25);
  30.             btn2.addActionListener(this);
  31.             
  32.             fm1.setTitle("JTextField test");
  33.             fm1.setBounds(100,100,220,200);
  34.             fm1.setVisible(true);
  35.             fm1.setResizable(false);
  36.             fm1.setLayout(null);
  37.             fm1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38.             fm1.add(lb1);
  39.             fm1.add(lb2);
  40.             fm1.add(tf1);
  41.             fm1.add(tf2);
  42.             fm1.add(btn1);
  43.             fm1.add(btn2);
  44.         }
  45.         public void actionProfromed(ActionEvent e){
  46.                 if(e.getSource()==tf1 || e.getSource()== btn1){
  47.                         double asr=Double.parseDouble(tf1.getText())*3.3058;
  48.                         String layasr=String.valueOf(asr);
  49.                         tf2.setText("面積為:"+layasr+"平方公分");
  50.                 }
  51.                 if(e.getSource()==btn2){
  52.                         tf1.setText("");
  53.                         tf2.setText("");
  54.                 }
  55.         }
  56.     public static void main(String[] args){
  57.             Ch83 app=new Ch83();
  58.     }
  59. }
複製代碼

TOP

返回列表