返回列表 發帖
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;

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

  8. import java.awt.Color;
  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.                 Main app=new Main();
  14.         }

  15.         public void actionPerformed(ActionEvent e) {
  16.                  if(e.getSource()==btn1)
  17.                  {
  18.                          tf2.setText("面積為: "+Float.parseFloat(tf1.getText())*3.3058+"平方公尺");
  19.                  }
  20.                  if(e.getSource()==btn2)
  21.                  {
  22.                          tf1.setText("");
  23.                          tf2.setText("");
  24.                  }
  25.         }

  26.         public JFrame fm;
  27.         private JTextField tf1, tf2;
  28.         private JButton btn1, btn2;
  29.         private JLabel lb1, lb2;
  30.         Main() {
  31.                 fm = new JFrame("JTextField配置練習");
  32.                 tf1 = new JTextField();
  33.                 tf1.addKeyListener(new KeyAdapter() {
  34.                      public void keyReleased(KeyEvent e) {
  35.                                  int code=e.getKeyCode();
  36.                                if(code>96 && code<105)
  37.                                {
  38.                                        System.out.println(code);
  39.                                }
  40.                                else
  41.                                {
  42.                                        int temp=tf1.getText().length();
  43.                                        try {
  44.                                                                         String str=tf1.getText(0, --temp);
  45.                                                                         tf1.setText(str);
  46.                                                          } catch (BadLocationException e1) {
  47.                                                                         // TODO 自動產生的 catch 區塊
  48.                                                                         e1.printStackTrace();
  49.                                                          }
  50.                                }         
  51.                         }
  52.                 });
  53.                 tf1.setBackground(Color.WHITE);
  54.                 tf2 = new JTextField();
  55.                 tf2.setBackground(Color.WHITE);
  56.                 lb1 = new JLabel("1坪=3.3058平方公尺", JLabel.CENTER);
  57.                 lb2 = new JLabel("輸入坪數:");
  58.                 btn1 = new JButton("確定");
  59.                 btn2 = new JButton("清除");

  60.                 tf1.setBounds(70, 45, 134, 30);
  61.                 tf1.addActionListener(this);
  62.                 tf2.setBounds(10, 82, 195, 40);
  63.                 tf2.addActionListener(this);
  64.                 tf2.setEditable(false);

  65.                 lb1.setBounds(0, 10, 215, 30);
  66.                 lb2.setBounds(10, 40, 60, 40);

  67.                 btn1.setBounds(10, 135, 92, 25);
  68.                 btn1.addActionListener(this);
  69.                 btn2.setBounds(112, 135, 92, 25);
  70.                 btn2.addActionListener(this);
  71.                
  72.                 fm.setBounds(100, 100, 227, 206);
  73.                 fm.setVisible(true);
  74.                 fm.setResizable(true);
  75.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  76.                 fm.getContentPane().setLayout(null);
  77.                 fm.getContentPane().add(tf1);
  78.                 fm.getContentPane().add(tf2);
  79.                 fm.getContentPane().add(lb1);
  80.                 fm.getContentPane().add(lb2);
  81.                 fm.getContentPane().add(btn1);
  82.                 fm.getContentPane().add(btn2);
  83.         }
  84. }
複製代碼

TOP

返回列表