内容发布更新时间 : 2025/5/6 10:11:26星期一 下面是文章的全部内容请认真阅读。
setLayout(new FlowLayout()); //窗口布局为FlowLayout text1=new TextArea(5,23); text2=new TextArea(5,23); add(text1); add(text2);
text2.setEditable(false); //显示求和结果和平均值的文本区禁止编辑
text1.addTextListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0); } } );
setBounds(100,100,400,160); setVisible(true); validate(); }
public void textValueChanged(TextEvent e) {
String s=text1.getText();
StringTokenizer geshu=new StringTokenizer(s); int n=geshu.countTokens(); double a[]=new double[n]; for(int i=0;i
String temp=geshu.nextToken(); double date=Double.parseDouble(temp); a[i]=date; }
double sum=0,average; for(int i=0;i
sum=sum+a[i]; }
average=sum/n; text2.setText(null);
text2.append(\和:\平均数:\} }
public class Test {
public static void main(String args[]) {
Calculated calc=new Calculated(\计算的窗口\} }
5. 文本区可以使用getSelectedText()方法获取该文本区通过拖动鼠标选中的文件。编写应用程序,有一个标题为“挑单词”的窗口,窗口的布局为BorderLayout布局。窗口中添加两个文本去和一个按钮组
件,要求文本区分别添加到窗口的东部区域和西部区域;按钮添加到窗口的南部区域,当单击按钮时,程序将东部区域的文本区中鼠标选中的内容尾加到西部区域的文本区中。 答: import java.awt.*; import java.awt.event.*;
class WindowSelectedText extends Frame implements ActionListener {
TextArea text1,text2; //定义2个文本区 Button button; //定义一个按钮
WindowSelectedText(String s) //窗口名字为“挑单词” { super(s);
setLayout(new BorderLayout()); //窗口布局是BorderLayout布局 text1=new TextArea(6,15); text2=new TextArea(6,15); button=new Button(\确定\add(text1,BorderLayout.EAST); add(text2,BorderLayout.WEST); add(button,BorderLayout.SOUTH); button.addActionListener(this); addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0); } } );
text2.setEditable(false); setBounds(100,100,350,200); setVisible(true); validate(); }
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button) {
String s=text1.getSelectedText()+\String str=text2.getText(); text2.setText(str+s); } } }
public class Test {
public static void main(String args[]) {
new WindowSelectedText(\挑单词\} }
6. 编写一个应用程序,有一个标题为“计算”的窗口,窗口的布局为FlowLayout布局。设计4个按钮,分别命名为“加”、“差”、“积”、“除”,另外,窗口中还有3个文本框。单击相应的按钮,将两个文本框的数字做运算,在第三个文本框中显示结果。要求处理NumberFormatException。 答: import java.awt.*; import java.awt.event.*;
class Calculated extends Frame implements ActionListener {
TextField text1,text2,text3; //3个文本框
Button buttonH,buttonC,buttonJ,buttonS; //4个按钮 Calculated(String s) { super(s);
setLayout(new FlowLayout()); //FlowLayout布局 text1=new TextField(10); text2=new TextField(10); text3=new TextField(17); buttonH=new Button(\加\buttonC=new Button(\差\buttonJ=new Button(\积\buttonS=new Button(\除\add(text1); add(text2); add(text3);
text3.setEditable(false);
add(buttonH); add(buttonC); add(buttonJ); add(buttonS);
buttonH.addActionListener(this); buttonC.addActionListener(this); buttonJ.addActionListener(this); buttonS.addActionListener(this); addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0); } }
);
setBounds(100,100,160,180); setVisible(true); validate(); }
public void actionPerformed(ActionEvent e) {
double num1=0,num2=0,totle=0; try {
num1= Double.parseDouble(text1.getText()); num2= Double.parseDouble(text2.getText()); if(e.getSource()==buttonH) {
totle=num1+num2;
text3.setText(\和\}
else if(e.getSource()==buttonC) {
totle=num1-num2;
text3.setText(\差\}
else if(e.getSource()==buttonJ) {
totle=num1*num2;
text3.setText(\积\}
else if(e.getSource()==buttonS) {
totle=num1/num2;
text3.setText(\商\} }
catch(NumberFormatException event) {
text3.setText(\请输入数字字符!\} } }
public class Test {
public static void main(String args[]) {
Calculated calc=new Calculated(\计算\标题为“计算”的窗口
} }
7. 改进例子7.16,在程序中增加一个名称为“确定”的按钮和一个文本区。当单击按钮时,程序验证用户是否输入了合法的E-mail地址格式,如果合法就将用户输入的姓名、E-mail和职业尾加到文本区中,否则在输入E-mail的文本框中提示用户输入了非法格式的E-mail地址。 答: import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*;
class WindowBox extends Frame implements ActionListener {
TextField text1,text2,text3; TextArea textarea; Box baseBox,boxV1,boxV2; Button button; WindowBox() {
button=new Button(\确定\textarea=new TextArea(6,12); text1=new TextField(12); text2=new TextField(12); text3=new TextField(12); boxV1=Box.createVerticalBox(); boxV1.add(new Label(\姓名\
boxV1.add(Box.createVerticalStrut(8)); boxV1.add(new Label(\