public void actionPerformed(ActionEvent e) { if (e.getSource() == button) {
String s = textA.getText(); double sum =0; //请在以下位置编写代码 //答案
int n=s.length(); int i=0; while(i 4.创建一个标题为\按钮\的窗框,窗框中显示有\按下我\字样的按钮。 import java.awt.*; public class abc { public static void main(String args[]) { new FrameOut(); } } class FrameOut extends Frame { Button btn; FrameOut() { } } super(\按钮\); btn = new Button(\按下我\); setLayout(new FlowLayout()); add(btn); setSize(300, 200); setVisible(true); 5. 设计学生类student,其数据域至少包括:学号(ID)姓名(name)性别(sex)英语等三门课程成绩(score)3门功课总成绩(sum)3门功课平均成绩(average)。成员方法至少包括:构造方法、计算总成绩方法getSun()、计算平均成绩方法getAve()。 public class Student { private int ID; private String name; private String sex; private int score[3]; private int sum; private int average; public Student( ) {ID=0; name=null; sex=0; } public Student(int ID, String name, String sex, int s1, int s2,int s3) { this.ID = ID; this.name = name; this.sex = sex; score[0]=s1; score[1]=s2; score[2]=s3; } public int getSun( ) { sum=english+score[0]+score[1]+score[2]; return sum; } public int getAve( ) { average=sum/3; return average; } } 6.设计按钮,要求:标题“按钮示例”实现2个按钮,“First”与“Second”,位置(180,160),窗口大小(300,300),类名ButtonApp,(提示需要导入的: java.awt.*; javax.swing.*;) 答案: import java.awt.Button; import java.awt.FlowLayout; import javax.swing.JFrame; public class ButtonApp extends JFrame { Button button1 = new Button(\Button button2 = new Button(\public ButtonApp() { super(\按钮示例\ this.setLayout(new FlowLayout()); this.setLocation(180, 160); this.setSize(300, 300); this.add(button1); this.add(button2); } public static void main(String[] args) { buttonApp = new ButtonApp(); buttonApp.setVisible(true); } } 7.用java语言设计一个程序,计算1+2+3+??+100; 要求:1)类名 Test 2)在主方法外写一个计算连加的方法,方法名为method且要通过传一整形参数,及 方法声明时用的的语句的一部分为 int method(int n); 3)在主方法中调用2)中写的方法,并在控制台输出值5050。 public class Test { public static int method(int n){ int sum=0; for(int i=1;i<=n;i++){ sum=sum+i; } return sum; } public static void main(String[] args) { System.out.println(method(100)); } } 8.最小公倍数是小学数学中的一个重点,为方便计算,现要求你用java语言写一个求两个大于0的整数的最小公倍数的程序以解决所有求两个整数最小公倍数的问题; 要求:1)类名 TestApp 2)在主方法外写一个求最小公倍数的方法,方法名为method且要求通过传两个整 形参数,及方法声明时的语句的一部分为 int method(int a , int b ); 3)在主方法中调用2)中写的方法,并用一组数据3和11进行测试,即求出3和11的最小公倍数,根据控制台的输出判断程序的正确性。 public class TestApp { public static int method(int a, int b) { int c = a > b ? a : b; int min = 0; for (int temp = c; true; temp++) { if (temp % a == 0 && temp % b == 0) { min = temp; break; } } } return min; } public static void main(String[] args) { System.out.println(method(3, 11)); } 9,试用布局管理器实现如下应用程序的界面设计,其中输入文字为标签(Label),中间为文本区(TextArea),其它为六项按纽(Button),用语言描述或用程序实现均可。 import java.awt.*; public class document extends Frame { document() { setLayout(new BorderLayout());/*窗口布局*/ Panel pan1 = new Panel();/*容器*/ pan1.setLayout(new GridLayout(3,1,0,5));/*容器布局*/ Panel pan2 = new Panel();/*容器*/ pan2.setLayout(new FlowLayout());/*容器布局*/ Panel pan3 = new Panel();/*容器*/ pan3.setLayout(new FlowLayout());/*容器布局*/ Button but1 = new Button(\检查\按钮*/ Button but2 = new Button(\搜索\ Button but3 = new Button(\选项\ Button but4 = new Button(\存档\ Button but5 = new Button(\取消\ Button but6 = new Button(\求助\ Label lab = new Label(\输入文字\标签*/ TextArea ta = new TextArea();/*文本框*/ pan1.add(but1);/*加载到容器1*/ pan1.add(but2); pan1.add(but3); pan2.add(but4);/*加载到容器2*/ pan2.add(but5); pan2.add(but6); pan3.add(lab);/*加载到容器3*/ add(pan1,\加载到窗口 布局位置*/ add(pan2,\ add(pan3,\ add(ta,\ setSize(300,150);/*窗口大小*/ setVisible(true); } 10,一个加法运算器的简易运算软件,实现三个文本行对象(TextField:text1,text2,result),一个标签(Label:lab),一个按纽(Button:btn),程序运行时,在前两个文本行中输入两个整数,点击按纽时,将前两文本行中的两个数相加,结果显示在第三个文本行result中。 以下是要求你编写的actionPerformed(ActionEvent e)方法的部分代码。 public void actionPerformed(ActionEvent e) //实现接口的完成动作消息处//理方法:actionPerformed,注意:参数必须有事件:ActionEvent 和 事件对//象e { int x = Integer.parseInt(text1.getText()) + Integer.parseInt(text2.getText()); result.setText(Integer.toString(x)); } } 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库java复习题 - 补充(3)在线全文阅读。
相关推荐: