標題:
TQC301
[打印本頁]
作者:
may
時間:
2012-8-18 12:11
標題:
TQC301
import java.awt.EventQueue;
9 l9 L0 L- s E; B: ~
9 m( b, L/ m! h8 g' J; `
2 h5 y% o4 k B' g! W. K' x
public class TQC301 {
/ `$ m5 M$ `0 ~$ a. H
+ f* @; A3 L* q
private JFrame frame;
i' ^. x2 _; R# G' f; _
private JTextField tf1;
2 { Q( G# b7 t6 I
private JTextField tf2;
0 @$ M# [8 C0 x) N9 q. G
private JTextField tf3;
4 f; K6 D/ D- a4 l
private JTextField tf4;
8 ?" s# c) R3 _- C) q
private JTextField tf5;
( S- D$ f, @- ~5 r
private JTextField answer;
2 o8 w' I2 { |
& [4 `' ? P5 p6 f7 P+ V S/ j4 H
/**
+ P& R1 j. P! O' ^: X: q
* Launch the application.
. _, r% g1 y+ ?
*/
1 ~% m; D# A7 e. V
public static void main(String[] args) {
) @! g; L8 s. e; B+ u
EventQueue.invokeLater(new Runnable() {
0 Q& y/ H. y7 H' Y. v+ j' x0 k
public void run() {
" U" A: V" E6 T! {
try {
1 q; g7 v/ U& P7 c! N$ x; q
TQC301 window = new TQC301();
8 ^$ N h4 D0 O! n4 }5 \ H$ r( n
window.frame.setVisible(true);
) s+ ~' F- x2 Y' \ v+ Z. t8 _- ]1 z8 l
} catch (Exception e) {
7 R ]7 }( u4 ?* U1 l# q' k* b2 z K
e.printStackTrace();
- W- Z/ K9 R n1 b6 H6 `
}
1 P$ |5 ]- u g o, z
}
- V8 ?! S$ G8 P6 }) B3 N1 H# T7 q
});
7 t; b. M! ~$ \+ {& {
}
" {+ m, `" |3 E$ [4 M6 @; s7 B
7 ~" }) N4 |7 p
/**
4 B# a# T& x* s' x- I5 A) e( X
* Create the application.
1 b$ d9 i5 l9 f/ L" w& j: o8 ]# _
*/
: Q, y% m, t* c! B2 N
public TQC301() {
1 [/ W8 Z" E; q1 |3 x" s+ D
initialize();
0 ~/ l/ T, F) s. p- }
}
# ]3 s) Y$ U/ [9 d5 p
$ M$ ]. t% d0 X6 t! L' Q' |
/**
0 q1 _: Q+ t4 C. c; d" Z
* Initialize the contents of the frame.
% v1 E* H8 I4 v
*/
7 Y; F3 |9 G5 j3 V' k% X5 ]4 W0 T! g
private void initialize() {
# P5 U7 U3 W+ U) B. S( o
frame = new JFrame();
6 e) ~: G) w6 ~. @5 m5 e: p. S! D: U1 ^
frame.setBounds(100, 100, 450, 300);
& F) w* l C; b) S
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
: R( ~3 {6 K* W/ E
frame.getContentPane().setLayout(null);
4 p V3 a; i' H3 [- Y! J7 a/ |
; W# y7 B; B) x! W
JLabel jl1 = new JLabel("\u7F8E\u91D1\u73FE\u503C");
0 G9 k" L3 n$ D3 B) _" x
jl1.setForeground(Color.BLACK);
: V# v* E/ c M' i
jl1.setBounds(20, 27, 60, 15);
4 k% K9 I# V+ X, n. {( e
frame.getContentPane().add(jl1);
% f: H5 {1 x1 u2 y! m0 l1 m F f
& Q- m: J/ e! I
JLabel jl2 = new JLabel("\u65E5\u5713\u73FE\u503C");
5 U$ t8 X0 S$ k/ e6 C( s! H
jl2.setBounds(20, 63, 53, 15);
/ `# E! p L( e5 N& ^4 {; L
frame.getContentPane().add(jl2);
% M) q3 t( U1 F5 y' j
$ a g& Z( r' E& \4 ~, D" b; i! Z m
tf1 = new JTextField();
* H3 T2 ~1 Q9 y5 L. Y; o$ g4 A
tf1.setBounds(83, 24, 96, 21);
( o9 T+ L8 b/ W1 W7 t
frame.getContentPane().add(tf1);
5 j T" v- T" W8 w W2 ~5 X' t
tf1.setColumns(10);
0 e! w# L8 l1 e
: t' y2 H; J$ _1 l7 C
JLabel jl3 = new JLabel("\u6E2F\u5E63\u73FE\u503C");
) b) t& F8 ^+ H) G6 g' b" P. D% [
jl3.setBounds(20, 102, 53, 15);
2 Q# u2 `. f5 y; Y- x
frame.getContentPane().add(jl3);
W) V1 S K$ ^! L* I( T
9 @4 `0 t9 _9 V' q) v5 L( G& Z
JLabel jl4 = new JLabel("\u82F1\u938A\u73FE\u503C");
4 z/ J5 y! H0 P4 R. p& w/ S
jl4.setBounds(20, 141, 60, 15);
; V9 w, Y: x/ e
frame.getContentPane().add(jl4);
5 B0 x, O" ?( Z* `( N- W
' j5 @# h- D; D! b
JLabel jl5 = new JLabel("\u6B50\u5143\u73FE\u503C");
) m. `8 k/ C0 I1 D& ?5 O
jl5.setBounds(20, 180, 60, 15);
# r( y7 n8 O! b6 f( `7 ~
frame.getContentPane().add(jl5);
" ^8 D( C( C4 |5 }. ]
3 r3 s) G; t& F
tf2 = new JTextField();
( b, |/ U) m6 D/ i( L4 p8 M A
tf2.setBounds(83, 60, 96, 21);
# x9 L9 J* s9 q2 P$ G
frame.getContentPane().add(tf2);
7 ]4 M) Y# S3 G, j$ G
tf2.setColumns(10);
9 S' H1 P2 S' C( ]" l' d; O
9 @- U( F' D& U0 E @% L" G. _9 o
tf3 = new JTextField();
3 D( y/ }* i( ~( y
tf3.setBounds(83, 99, 96, 21);
6 W* h+ B& k8 s- v
frame.getContentPane().add(tf3);
4 n* w% p) v0 Z' `: [& _6 a
tf3.setColumns(10);
* z+ d+ Z) i K! H8 ^
6 P3 M& v" \ @& H6 a
tf4 = new JTextField();
0 s) J' Y$ Z! o+ C( G& {
tf4.setBounds(83, 138, 96, 21);
8 A# ]! s# E! p
frame.getContentPane().add(tf4);
* E+ O+ F4 y9 y9 d0 f
tf4.setColumns(10);
3 ^- _. r$ e6 c; d" x4 g
L' l! c: ]& \* [: i; |
tf5 = new JTextField();
2 S. [# U0 H0 @/ t0 q' k T
tf5.setBounds(83, 177, 96, 21);
* r, H' u' X" P' P; F
frame.getContentPane().add(tf5);
) O8 b+ q- }& {$ @& G& Q" ^
tf5.setColumns(10);
, d8 f- o* e6 N% q5 a5 U
, L% ?! D( U2 f1 a
answer = new JTextField();
" x0 j) O/ @5 a# N+ w" [
answer.addKeyListener(new KeyAdapter() {
- o* Q- P' t# F! w
@Override
: b' r7 ?% o D' i
public void keyReleased(KeyEvent e) {
& ^3 }- F& ]- [& V: t4 W
System.out.println(answer.getText());
0 ^! q( N- R6 L; V
. u3 _5 E' i& h$ r, F/ X! @& {/ }
try{
( H* v7 P9 ?4 D( L
double num = Double.parseDouble(answer.getText());
7 l' b2 w% Y# F& p4 ?6 v
]8 R% [9 y; q/ [) E% D
+ H1 A6 n1 z9 ]7 |+ Q3 O+ K
tf1.setText( String.valueOf(num * 0.029) );
/ j0 p5 s5 X( q/ }+ r4 Y
tf2.setText( String.valueOf(num * 33.53) );
0 V% r6 [& m& D, }
tf3.setText( String.valueOf(num * 0.22) );
8 M; D% m4 P9 S( W2 z) L
tf4.setText( String.valueOf(num * 0.018) );
3 U# i2 w4 T3 ]7 Z
tf5.setText( String.valueOf(num * 0.03) );
0 J, i& K, L" }
}catch(Exception ex)
^! |* R7 A4 k
{
( `4 k" Y- T& ^' p$ y# k
tf1.setText( "請勿輸入非數字的字元" );
& _' r3 k6 F+ Q
tf2.setText( "請勿輸入非數字的字元" );
# `/ Y Z4 {9 V$ ~5 H5 g
tf3.setText( "請勿輸入非數字的字元");
8 |+ A& x- r* F- M6 K4 B
tf4.setText( "請勿輸入非數字的字元" );
! j3 `6 s" Y/ G5 i% i
tf5.setText( "請勿輸入非數字的字元" );
/ Q& ]0 w1 f' j8 ]( M4 `
}
) T; [; ?* ?: n/ x! {( w
+ T3 I; b# f% B5 ~* _% Z. W
}
( n% I! z' r% v9 {
});
! ~+ b. p6 p6 }! ?4 L& e
7 v( e- }% A j
answer.setBounds(20, 224, 159, 21);
8 c1 R/ u4 P+ Y4 M! W# a
frame.getContentPane().add(answer);
& j5 M- [, ]9 N' `' L
answer.setColumns(10);
2 y0 {' I' r" ^& t
}
$ b, j5 V8 J1 Q6 j* n) I! ]6 g
}
作者:
johnson
時間:
2012-8-26 17:46
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class JAVA301 {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField answer;
private JLabel label_5;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JAVA301 window = new JAVA301();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public JAVA301() {
initialize();
}
void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 594, 385);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel label = new JLabel("\u7F8E\u91D1\u73FE\u503C");
label.setFont(new Font("新細明體", Font.PLAIN, 15));
label.setBounds(10, 36, 80, 15);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("\u65E5\u5713\u73FE\u503C");
label_1.setFont(new Font("新細明體", Font.PLAIN, 15));
label_1.setBounds(10, 87, 80, 15);
frame.getContentPane().add(label_1);
JLabel label_2 = new JLabel("\u6E2F\u5E63\u73FE\u503C");
label_2.setFont(new Font("新細明體", Font.PLAIN, 15));
label_2.setBounds(10, 138, 80, 15);
frame.getContentPane().add(label_2);
JLabel label_3 = new JLabel("\u82F1\u938A\u73FE\u503C");
label_3.setFont(new Font("新細明體", Font.PLAIN, 15));
label_3.setBounds(10, 189, 80, 15);
frame.getContentPane().add(label_3);
JLabel label_4 = new JLabel("\u6B50\u5143\u73FE\u503C");
label_4.setFont(new Font("新細明體", Font.PLAIN, 15));
label_4.setBounds(10, 240, 80, 15);
frame.getContentPane().add(label_4);
textField = new JTextField();
textField.setBounds(100, 238, 476, 21);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(100, 33, 476, 21);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(100, 85, 476, 21);
frame.getContentPane().add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(100, 136, 476, 21);
frame.getContentPane().add(textField_3);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(100, 187, 476, 21);
frame.getContentPane().add(textField_4);
textField_4.setColumns(10);
answer = new JTextField();
answer.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
System.out.println(answer.getText());
try {
double num = Double.parseDouble(answer.getText());
textField.setText(String.valueOf(num * 0.03));
textField_1.setText(String.valueOf(num * 0.029));
textField_2.setText(String.valueOf(num * 33.53));
textField_3.setText(String.valueOf(num * 0.22));
textField_4.setText(String.valueOf(num * 0.018));
} catch (Exception ex) {
textField.setText("請勿輸入非數字的字元");
textField_1.setText("請勿輸入非數字的字元");
textField_2.setText("請勿輸入非數字的字元");
textField_3.setText("請勿輸入非數字的字元");
textField_4.setText("請勿輸入非數字的字元");
}
}
});
answer.setBounds(139, 294, 437, 21);
frame.getContentPane().add(answer);
answer.setColumns(10);
label_5 = new JLabel("\u8ACB\u8F38\u5165\u9700\u63DB\u7B97\u4E4B\u91D1\u984D");
label_5.setBounds(10, 297, 119, 15);
frame.getContentPane().add(label_5);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2