1、 add(textA2); textA1.addTextListener(this); setBounds(0,0,300,300); setVisible(true); validate(); public void textValueChanged(TextEvent e) String s = textA1.getText(); StringTokenizer fenxi = new StringTokenizer(s, nr int n = fenxi.countTokens(); long sum=0; try for(int i = 0; in; i+) sum += Long.p
2、arseLong(fenxi.nextToken(); textA2.setText(总和 + String.valueOf(sum); double avg = (double)sum/n; textA2.append(n平均数 + String.valueOf(avg); catch (NumberFormatException e1) /System.out.println(输入有误! System.out.println(s); public class T7_5 new MyFrame(挑单词class MyFrame extends Frame implements ActionL
3、istener TextArea ta1,ta2; Button btn; MyFrame (String s) super(s); setLayout(new BorderLayout(); ta1 = new TextArea(5,15); ta2 = new TextArea(5,15); btn = new Button(追加 btn.addActionListener(this); add(ta1,BorderLayout.EAST); add(ta2,BorderLayout.WEST); add(btn,BorderLayout.SOUTH); setBounds(100,100
4、,300,200); public void actionPerformed(ActionEvent e) String s = ta1.getSelectedText(); ta2.append(s);/带关闭功能的窗口public class T7_6 new MathWindow(计算class MathWindow extends Frame implements ActionListener /定义一个类继承于Frame 并实现了接口ActionListener Button btn_Add,btn_Sub,btn_Mul,btn_Mov;/定义四个表示运算的按钮 TextField
5、 tf1,tf2,tf3;/定义三个文本框 Label l1,l2;/定义两个标签 MathWindow(String s) /设置标题/设置窗口模式 btn_Add = new Button(加 btn_Sub = new Button(减 btn_Mul = new Button(乘 btn_Mov = new Button(除/实例化四个表示运算的按钮 tf1 = new TextField(8); tf2 = new TextField(8); tf3 = new TextField(8);/实例化三个文本框 l1 = new Label(,1); l2 = new Label(=/实
6、例化两个标签 add(tf1); add(l1); add(tf2); add(l2); add(tf3); add(btn_Add); add(btn_Sub); add(btn_Mul); add(btn_Mov);/将所需的组件加入到窗口中 btn_Add.addActionListener(this); btn_Sub.addActionListener(this); btn_Mul.addActionListener(this); btn_Mov.addActionListener(this);/设置四个表示运算按钮的监视器为本窗口 this.addWindowListener(ne
7、w WindowAdapter() public void windowClosing(WindowEvent e) dispose();/撤销当前窗口 ); setBounds(100,100,500,80);/设置窗口大小 setResizable(false);/设置窗口大小是否可由用户改变/设置窗口的显示状态/刷新 public void actionPerformed(ActionEvent e) /实现ActionListener接口的方法 String s1 = tf1.getText(); String s2 = tf2.getText();/获取两个文本框的内容 try lo
8、ng m = Long.parseLong(s1); long n = Long.parseLong(s2);/将获取的文本框的内容转换成长整型 if(e.getSource() = btn_Add) /判断事件的发生源 并做出相应的处理 l1.setText(+ tf3.setText(String.valueOf(m+n); else if(e.getSource() = btn_Sub) - tf3.setText(String.valueOf(m-n); else if (e.getSource() = btn_Mul) x tf3.setText(String.valueOf(m*n
9、); else if(e.getSource() = btn_Mov) / tf3.setText(String.valueOf(1.0*m/n); else System.exit(0); catch(NumberFormatException e1) /异常处理 System.out.println(输入数字字符串!import javax.swing.*;import javax.swing.border.*;public class T7_7 WindowBox wb = new WindowBox(确定class WindowBox extends Frame implements
10、ActionListener Box baseBox,boxV1,boxV2; Button btn_OK; TextArea ta; WindowBox(String s) tf1 = new TextField(12); tf2 = new TextField(12); tf3 = new TextField(12); boxV1 = Box.createVerticalBox(); boxV1.add(new Label(姓名); boxV1.add(Box.createVerticalStrut(8);E_mail职业 boxV2 = Box.createVerticalBox(); boxV2.add(tf1); boxV2.add(Box.createVerticalStrut(8); boxV2.add(tf2); boxV2.add(tf3); baseBox = Box.createHorizontalBox(); baseBox.add(boxV1); baseBox.add(Box.createHorizontalStrut(10); baseBox.add(boxV2); btn_OK = new Button(提交 btn_OK.
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1