复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69package ex1; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class EX3 { public static void main(String args[]) { WindowButton win=new WindowButton("猜数字"); } } class WindowButton extends JFrame implements ActionListener { /** * */ private static final long serialVersionUID = 1L; int number; JLabel hintLabel; JTextField inputGuess; JButton buttonGetNumber,buttonEnter; WindowButton(String s) { super(s); addWindowListener( new WindowAdapter(){ //匿名类的实例监视窗口事件 public void windowClosing(WindowEvent e) { dispose(); } } ); setLayout(new FlowLayout()); buttonGetNumber=new JButton("得到一个随机数"); add(buttonGetNumber); hintLabel=new JLabel("输入你的猜测:",JLabel.CENTER); hintLabel.setBackground(Color.cyan); inputGuess=new JTextField("0",10); add(hintLabel); add(inputGuess); buttonEnter=new JButton("确定"); add(buttonEnter); buttonEnter.addActionListener(this); buttonGetNumber.addActionListener(this); setBounds(100,100,450,250); setVisible(true); validate(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==buttonGetNumber) { number=(int)(Math.random()*100)+1; hintLabel.setText("输入你的猜测:"); } else if(e.getSource()==buttonEnter) { int guess=0; try { guess=Integer.parseInt(inputGuess.getText()); if(guess==number) { hintLabel.setText("猜对了!"); } else if(guess>number) { hintLabel.setText("猜大了!"); inputGuess.setText(null); } else if(guess<number) { hintLabel.setText("猜小了!"); inputGuess.setText(null); } } catch(NumberFormatException event) { hintLabel.setText("请输入数字字符"); } } } }
最后
以上就是羞涩微笑最近收集整理的关于匿名类的实例监视窗口事件的全部内容,更多相关匿名类内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复