JAVA 实验报告 程序
我是搞C的,现在有两个JAVA程序要实现,都是为了应付实验报告,希望大家帮帮我,急啊,晚上7点就要上交!(1) 设计一个随机显示单词功能,要求:
――显示当前时间;
――每隔3秒钟随机显示一个英文单词;
――要求用多线程技术实现;
(2) 学生选课(学生课程自定):
――所有的课程信息存在一个课程文件中;
――多个学生选课,每个学生选课的信息也存在相应的文件中;
――输出所有课程信息;输出每个学生选课的文件信息;
[[it] 本帖最后由 liumang_D 于 2008-11-16 15:40 编辑 [/it]]
----------------解决方案--------------------------------------------------------
写了个第一题
程序代码:
import java.awt.Container;
import java.awt.GridLayout;
import java.util.Calendar;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class ThreadTest extends JFrame{
private Container container;
private JLabel timeLabel;
private JTextField timeField;
private JLabel wordLabel;
private JTextField wordField;
public ThreadTest() {
super("多线程");
container = getContentPane();
timeLabel = new JLabel("当 前 时 间",JLabel.CENTER);
timeField = new JTextField(17);
timeField.setEditable(false);
wordLabel = new JLabel("随 机 单 词",JLabel.CENTER);
wordField = new JTextField(17);
wordField.setEditable(false);
container.setLayout(new GridLayout(2,2));
container.add(timeLabel);
container.add(timeField);
container.add(wordLabel);
container.add(wordField);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void runThread(){
Thread timeThread = new Thread(new TimeDisplay());
timeThread.start();
Thread wordThread = new Thread(new WordDisplay());
wordThread.start();
}
public static void main(String[] args) {
new ThreadTest().runThread();
}
private class TimeDisplay implements Runnable{
public void run(){
while(true){
timeField.setText(Calendar.getInstance().getTime().toString());
try{
Thread.sleep(1000);
}catch(Exception e){}
}
}
}
private class WordDisplay implements Runnable{
private String[] words =
("Java software powers the onboard computers in toys cars planes rockets and " +
"even the NASA Mars Rover It brings interactivity to the Internet real-time " +
"graphics to television instant imaging to cameras and multi-player games " +
"to mobile phones and desktop PCs It connects the largest enterprises and " +
"smallest businesses to their employees customers and data And it secures the " +
"vast majority of electronic transactions in retail finance government science " +
"and medicine In short Java technology goes everywhere you go").toLowerCase().split(" ");
private Random rand;
{
rand = new Random();
}
public void run(){
while(true){
wordField.setText(words[rand.nextInt(words.length)]);
try{
Thread.sleep(3000);
}catch(Exception e){}
}
}
}
}
import java.awt.GridLayout;
import java.util.Calendar;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class ThreadTest extends JFrame{
private Container container;
private JLabel timeLabel;
private JTextField timeField;
private JLabel wordLabel;
private JTextField wordField;
public ThreadTest() {
super("多线程");
container = getContentPane();
timeLabel = new JLabel("当 前 时 间",JLabel.CENTER);
timeField = new JTextField(17);
timeField.setEditable(false);
wordLabel = new JLabel("随 机 单 词",JLabel.CENTER);
wordField = new JTextField(17);
wordField.setEditable(false);
container.setLayout(new GridLayout(2,2));
container.add(timeLabel);
container.add(timeField);
container.add(wordLabel);
container.add(wordField);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void runThread(){
Thread timeThread = new Thread(new TimeDisplay());
timeThread.start();
Thread wordThread = new Thread(new WordDisplay());
wordThread.start();
}
public static void main(String[] args) {
new ThreadTest().runThread();
}
private class TimeDisplay implements Runnable{
public void run(){
while(true){
timeField.setText(Calendar.getInstance().getTime().toString());
try{
Thread.sleep(1000);
}catch(Exception e){}
}
}
}
private class WordDisplay implements Runnable{
private String[] words =
("Java software powers the onboard computers in toys cars planes rockets and " +
"even the NASA Mars Rover It brings interactivity to the Internet real-time " +
"graphics to television instant imaging to cameras and multi-player games " +
"to mobile phones and desktop PCs It connects the largest enterprises and " +
"smallest businesses to their employees customers and data And it secures the " +
"vast majority of electronic transactions in retail finance government science " +
"and medicine In short Java technology goes everywhere you go").toLowerCase().split(" ");
private Random rand;
{
rand = new Random();
}
public void run(){
while(true){
wordField.setText(words[rand.nextInt(words.length)]);
try{
Thread.sleep(3000);
}catch(Exception e){}
}
}
}
}
----------------解决方案--------------------------------------------------------
非常感谢版主
----------------解决方案--------------------------------------------------------