標題:
[隨堂練習] keyrelease
[打印本頁]
作者:
周政輝
時間:
2018-1-20 11:58
標題:
[隨堂練習] keyrelease
將上次做的坪數轉換的程式,透過keyrelease事件
將TextField中限制只允許數字
package tw.kuas.edu.tw;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Color;
public class Main implements ActionListener{
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
Main app = new Main();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自動產生的方法 Stub
if(e.getSource()==btn1)
{
tf2.setText("面積為: "+Double.parseDouble(tf1.getText())*3.3058+" 平方公尺");
}
if(e.getSource()==btn2)
{
tf1.setText("");
tf2.setText("");
}
}
private JFrame fm;
private JLabel lb1, lb2;
private JTextField tf1, tf2;
private JButton btn1, btn2;
private JButton btnNewButton;
private JButton btnNewButton_1;
Main()
{
lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
lb1.setBounds(0, 10, 215, 30);
lb2=new JLabel("輸入坪數:");
lb2.setBounds(10, 40, 60, 40);
tf1=new JTextField();
// keypress => 文字放上去後觸發
//
// keyrelease =>觸發後再將文字放上去
tf1.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
if(code >= 97 && code <= 105)
{
System.out.println(code);
}
else{
//先取得畫面上的文字內容
//刪除掉最後一個字
//刪除後的結果放到畫面上
int temp = tf1.getText().length();
try {
// char [] chr = tf1.getText().toCharArray();
// String ss ="";
// for(int i=0;i<chr.length-1;i++)
// {
// ss+=chr[i];
// }
String str = tf1.getText(0, --temp);
tf1.setText(str);
} catch (BadLocationException e1) {
// TODO 自動產生的 catch 區塊
e1.printStackTrace();
}
}
}
});
tf1.setBackground(Color.WHITE);
tf1.setBounds(70, 45, 134, 30);
tf1.addActionListener(this);
tf2=new JTextField();
tf2.setBounds(10, 85, 195, 40);
tf2.setEditable(false);
btn1=new JButton("確定");
btn1.setBounds(10, 135, 92, 25);
btn1.addActionListener(this);
btn2=new JButton("清除");
btn2.setBounds(112, 135, 92, 25);
btn2.addActionListener(this);
fm=new JFrame("土地面積計算");
fm.setBounds(100, 100, 306, 306);
fm.setVisible(true);
fm.setResizable(false);
fm.getContentPane().setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.getContentPane().add(lb1);
fm.getContentPane().add(lb2);
fm.getContentPane().add(tf1);
fm.getContentPane().add(tf2);
fm.getContentPane().add(btn1);
fm.getContentPane().add(btn2);
}
}
複製代碼
作者:
張健勳
時間:
2018-1-20 11:58
package bbs.istak.org.tw;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
public class actionTest implements ActionListener{
protected static final char[] code = null;
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
actionTest app = new actionTest();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自動產生的方法 Stub
if(e.getSource()==btn1)
{
tf2.setText("面積為: "+Double.parseDouble(tf1.getText())*3.3058+" 平方公尺");
}
if(e.getSource()==btn2)
{
tf1.setText("");
tf2.setText("");
}
}
private JFrame fm;
private JLabel lb1, lb2;
private JTextField tf1, tf2;
private JButton btn1, btn2;
actionTest()
{
lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
lb1.setBounds(0, 10, 215, 30);
lb2=new JLabel("輸入坪數:");
lb2.setBounds(10, 40, 60, 40);
tf1=new JTextField();
tf1.addKeyListener(new KeyAdapter(){
public void KeyReleased (KeyEvent e){
if(e.getKeyCode()>=96 || e.getKeyCode()<=105)
{
System.out.println(code);
}
else{
int str = tf1.getText().length();
try {
tf2.setText(tf1.getText(0, --str));
}catch(BadLocationException e1){
e1.printStackTrace();
}
}
}
});
tf1.setBounds(70, 45, 134, 30);
tf1.addActionListener(this);
tf2=new JTextField();
tf2.setBounds(10, 85, 195, 40);
tf2.setEditable(false);
btn1=new JButton("確定");
btn1.setBounds(10, 135, 92, 25);
btn1.addActionListener(this);
btn2=new JButton("清除");
btn2.setBounds(112, 135, 92, 25);
btn2.addActionListener(this);
fm=new JFrame("土地面積計算");
fm.setBounds(100, 100, 220, 200);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb1);
fm.add(lb2);
fm.add(tf1);
fm.add(tf2);
fm.add(btn1);
fm.add(btn2);
}
}
複製代碼
作者:
黃茂勛
時間:
2018-1-20 11:58
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Main implements ActionListener {
public static void main(String[] args) {
Main app=new Main();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1)
{
tf2.setText("面積為: "+Float.parseFloat(tf1.getText())*3.3058+"平方公尺");
}
if(e.getSource()==btn2)
{
tf1.setText("");
tf2.setText("");
}
}
public JFrame fm;
private JTextField tf1, tf2;
private JButton btn1, btn2;
private JLabel lb1, lb2;
Main() {
fm = new JFrame("JTextField配置練習");
tf1 = new JTextField();
tf1.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
int code=e.getKeyCode();
if(code>96 && code<105)
{
System.out.println(code);
}
else
{
int temp=tf1.getText().length();
try {
String str=tf1.getText(0, --temp);
tf1.setText(str);
} catch (BadLocationException e1) {
// TODO 自動產生的 catch 區塊
e1.printStackTrace();
}
}
}
});
tf1.setBackground(Color.WHITE);
tf2 = new JTextField();
tf2.setBackground(Color.WHITE);
lb1 = new JLabel("1坪=3.3058平方公尺", JLabel.CENTER);
lb2 = new JLabel("輸入坪數:");
btn1 = new JButton("確定");
btn2 = new JButton("清除");
tf1.setBounds(70, 45, 134, 30);
tf1.addActionListener(this);
tf2.setBounds(10, 82, 195, 40);
tf2.addActionListener(this);
tf2.setEditable(false);
lb1.setBounds(0, 10, 215, 30);
lb2.setBounds(10, 40, 60, 40);
btn1.setBounds(10, 135, 92, 25);
btn1.addActionListener(this);
btn2.setBounds(112, 135, 92, 25);
btn2.addActionListener(this);
fm.setBounds(100, 100, 227, 206);
fm.setVisible(true);
fm.setResizable(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.getContentPane().setLayout(null);
fm.getContentPane().add(tf1);
fm.getContentPane().add(tf2);
fm.getContentPane().add(lb1);
fm.getContentPane().add(lb2);
fm.getContentPane().add(btn1);
fm.getContentPane().add(btn2);
}
}
複製代碼
作者:
蔡庭豪
時間:
2018-1-20 12:00
package bbs.istak.org.tw;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
public class Main implements ActionListener{
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
Main app = new Main();
}
public void actionPerformed(ActionEvent e) {
// TODO 自動產生的方法 Stub
if(e.getSource() == btn1)
tf2.setText("面積為: "+Double.parseDouble(tf1.getText())*3.3058+" 平方公尺");
else if(e.getSource() == btn2)
tf2.setText(" ");
}
private JFrame fm;
private JLabel lb1, lb2;
private JTextField tf1, tf2;
private JButton btn1, btn2;
Main()
{
lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
lb1.setBounds(0, 10, 215, 30);
lb2=new JLabel("輸入坪數:");
lb2.setBounds(10, 40, 60, 40);
tf1=new JTextField();
tf1.setBounds(70, 45, 134, 30);
tf1.addKeyListener(new KeyAdapter(){
public void KeyReleased (KeyEvent e){
if(e.getKeyCode()<96 && e.getKeyCode()>105){{
int str = tf1.getText().length();
try {
tf2.setText(tf1.getText(0, --str));
} catch (BadLocationException e1) {
// TODO 自動產生的 catch 區塊
e1.printStackTrace();
}
}
}
}
});
tf2=new JTextField();
tf2.setBounds(10, 85, 195, 40);
tf2.setEditable(false);
btn1=new JButton("確定");
btn1.setBounds(10, 135, 92, 25);
btn1.addActionListener(this);
btn2=new JButton("清除");
btn2.setBounds(112, 135, 92, 25);
btn2.addActionListener(this);
fm=new JFrame("土地面積計算");
fm.setBounds(100, 100, 220, 200);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb1);
fm.add(lb2);
fm.add(tf1);
fm.add(tf2);
fm.add(btn1);
fm.add(btn2);
}
}
複製代碼
作者:
陳泓瑜
時間:
2018-1-20 12:01
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class CALC implements ActionListener{
public static void main(String[] args)
{
// TODO 自動產生的方法 Stub
CALC calc = new CALC();
}
@Override
public void actionPerformed(ActionEvent e)
{
// TODO 自動產生的方法 Stub
if(e.getSource()==bt1)
{
tf2.setText("面積為 "+Double.parseDouble(tf1.getText())*3.3058+" sqmeter.");
}
if(e.getSource()==bt2)
{
tf1.setText("");
tf2.setText("");
}
}
private JFrame fm;
private JLabel lb1;
private JLabel lb2;
private JTextField tf1;
private JTextField tf2;
private JButton bt1;
private JButton bt2;
CALC()
{
fm=new JFrame("土地面積計算");
lb1=new JLabel("1坪=3.3058sqmeter");
lb2=new JLabel("輸入坪數:");
tf1=new JTextField();
tf1.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent arg0) {
int kc=arg0.getKeyCode();
if(kc==8 || 96<= kc && kc<=105 || kc==110)
{
System.out.println("y");
}
else
{
int length = tf1.getText().length();
String str;
try
{
System.out.println("n");
str = tf1.getText(0, --length);
tf1.setText(str);
}
catch (BadLocationException e)
{
// TODO 自動產生的 catch 區塊
e.printStackTrace();
}
}
}
});
tf2=new JTextField();
bt1=new JButton("確定");
bt2=new JButton("清除");
lb1.setBounds(45, 10, 215, 30);
lb2.setBounds(10, 40, 60, 40);
tf1.setBounds(70, 45, 125, 30);
tf2.setBounds(10, 85, 185, 40);
tf1.setEditable(true);
tf2.setEditable(false);
tf1.addActionListener(this);
tf2.addActionListener(this);
bt1.setBounds(10, 135, 92, 25);
bt2.setBounds(105, 135, 92, 25);
bt1.addActionListener(this);
bt2.addActionListener(this);
fm.setBounds(100, 100, 220, 200);
fm.setVisible(true);
fm.setResizable(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.getContentPane().setLayout(null);
fm.getContentPane().add(tf1);
fm.getContentPane().add(tf2);
fm.getContentPane().add(lb1);
fm.getContentPane().add(lb2);
fm.getContentPane().add(bt1);
fm.getContentPane().add(bt2);
}
}
複製代碼
作者:
蔡季樺
時間:
2018-1-20 12:01
package bbs.istak.org.tw;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Main implements ActionListener{
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
Main app = new Main();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自動產生的方法 Stub
if(e.getSource()==btn1)
{
tf2.setText("面積為: "+Double.parseDouble(tf1.getText())*3.3058+" 平方公尺");
}
if(e.getSource()==btn2)
{
tf1.setText("");
tf2.setText("");
}
}
private JFrame fm;
private JLabel lb1, lb2;
private JTextField tf1, tf2;
private JButton btn1, btn2;
Main()
{
lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
lb1.setBounds(0, 10, 215, 30);
lb2=new JLabel("輸入坪數:");
lb2.setBounds(10, 40, 60, 40);
tf1=new JTextField();
tf1.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()<96 || e.getKeyCode()>105)
{
int temp = tf1.getText().length();
try {
String str = tf1.getText(0, --temp);
tf1.setText(str);
} catch (BadLocationException e1) {
// TODO 自動產生的 catch 區塊
e1.printStackTrace();
}
}
}
});
tf1.setBounds(70, 45, 134, 30);
tf1.addActionListener(this);
tf2=new JTextField();
tf2.setBounds(10, 85, 195, 40);
tf2.setEditable(false);
btn1=new JButton("確定");
btn1.setBounds(10, 135, 92, 25);
btn1.addActionListener(this);
btn2=new JButton("清除");
btn2.setBounds(112, 135, 92, 25);
btn2.addActionListener(this);
fm=new JFrame("土地面積計算");
fm.setBounds(100, 100, 220, 200);
fm.setVisible(true);
fm.setResizable(false);
fm.getContentPane().setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.getContentPane().add(lb1);
fm.getContentPane().add(lb2);
fm.getContentPane().add(tf1);
fm.getContentPane().add(tf2);
fm.getContentPane().add(btn1);
fm.getContentPane().add(btn2);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2