} 6.
// 实现 ActionListener 中的方法
public void actionPerformed(ActionEvent e) { String s; if (e.getActionCommand() == \ System.exit(0); } else if (e.getActionCommand() == \ try { while ((s = in.readLine()) != null) text.append(s + '\\n'); } catch (IOException exp) { } } }
编写一个字符方式的Application程序。在程序中定义一个表示学生的类Student。Student类的属性为学号、姓名和成绩;方法为增加记录setRecord和得到记录getRecord。setRecord给出学号、姓名和成绩的赋值,getRecord通过学号得到学生的成绩。在程序的主类中创建一个Student类的对象objStu,调用setRecord方法为其属性赋值,然后输出该学生的成绩。请写出源程序代码。 class Student { private int ID; private String name; private float score; public void setRecord(int ID,String name,float score){ this.ID = ID; this.name = name; this.score = score; } public float getRecord(int ID){ if(ID == this.ID) return this.score; else return -1; } }
public class StudentApp { public static void main(String[] args) { Student objStu = new Student(); objStu.setRecord(0, \ float Sco = objStu.getRecord(0); System.out.print(Sco);
11
}
}
7. 按照下面要求编写程序:编写一个 JFrame 窗口程
序,要求窗口的内容面板上有一个按钮,点击按钮可以弹出一个对话框。请写出源程序代码。 import javax.swing.*; import java.awt.*;
import java.awt.event.*;
public class FrameApp implements ActionListener { public static void main(String[] args) { JFrame jframe = new JFrame(\ jframe.setSize(400,100); jframe.getContentPane().setLayout(new FlowLayout()); JButton jbtn1 = new JButton(\按钮\ jbtn1.addActionListener(new FrameApp()); jframe.getContentPane().add(jbtn1); jframe.setVisible(true); } public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, \ } }
8. 按照下面要求编写程序:编写一个 JFrame 窗口程
序,要求窗口标题栏的文字是“这是一个JFrame窗口程序”。请写出源程序代码。 import javax.swing.*; import java.awt.event.*;
class MyJFrame extends JFrame{ public MyJFrame(String name, int w, int h) { super(); setTitle(name); setSize(w, h); setLocation(100, 100); } }
public class MyJFrameApp { public static void main(String[] args) { String str1 = \ MyJFrame mw = new MyJFrame(str1, 400, 300); mw.setVisible(true); } }
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库北邮《Java技术》期末复习题(含答案)(3)在线全文阅读。
相关推荐: