程序代码:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
public class Test implements ActionListener{
JFrame frame = new JFrame("C/S考试系统"); public static JTextArea questArea = new JTextArea(15,40);
JScrollPane questScrollPane = new JScrollPane(questArea);
JPanel southPanel = new JPanel();
JPanel northPanel = new JPanel();
JPanel centerPanel = new JPanel();
JLabel label1 = new JLabel("考试剩余时间:");
JLabel label2 = new JLabel();
JButton startButton = new JButton("开始考试");
JButton submitButton = new JButton("提交答案");
JButton nextButton = new JButton("下一题");
JRadioButton rbutton1 = new JRadioButton("A");
JRadioButton rbutton2 = new JRadioButton("B");
JRadioButton rbutton3 = new JRadioButton("C");
JRadioButton rbutton4 = new JRadioButton("D");
ButtonGroup group = new ButtonGroup();
public Test(){
frame.setLayout(new BorderLayout());
frame.add(southPanel, BorderLayout.SOUTH);
frame.add(northPanel, BorderLayout.NORTH);
frame.add(centerPanel, BorderLayout.CENTER);
northPanel.setLayout(new FlowLayout());
northPanel.add(label1);
northPanel.add(label2);
northPanel.add(startButton);
centerPanel.add(questScrollPane);
southPanel.setLayout(new FlowLayout());
southPanel.add(rbutton1);
southPanel.add(rbutton2);
southPanel.add(rbutton3);
southPanel.add(rbutton4);
southPanel.add(nextButton);
southPanel.add(submitButton);
group.add(rbutton1);
group.add(rbutton2);
group.add(rbutton3);
group.add(rbutton4);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);
frame.setLocation(300, 200);
frame.setVisible(true);
nextButton.addActionListener(this);
startButton.addActionListener(this);
submitButton.addActionListener(this);
} public void actionPerformed(ActionEvent e){
if(e.getSource()==nextButton){
//返回答案,并显示下一题;
}
if(e.getSource()==startButton){//开始考试
countTime();
}
if(e.getSource()==submitButton){
}
} public void countTime(){//考试倒计时
int minutes = 0;
int seconds = 5;
String s;
while(true){
s = minutes+"分"+seconds+"秒";
label2.setText(s);
System.out.println(s);
questArea.setText(s);
if(seconds==0&&minutes!=0){
seconds=60;
minutes--;
}
else if(seconds==0&&minutes==0){
break;
}
seconds--;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String []args){
new Test();
}
}当按下"开始考试"按钮时,调用countTime()方法里面包含有sleep(1000),能实现System.out.println(s);
不能出现label2.setText(s);
要等时间到0,break出来时,再会在label2上加上s;但是如果在构造函数里直接调用countTime();就不会出现问题.怎么解决
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
public class Test extends Thread implements ActionListener {
JFrame frame = new JFrame("C/S考试系统");public static JTextArea questArea = new JTextArea(15,40);
JScrollPane questScrollPane = new JScrollPane(questArea);JPanel southPanel = new JPanel();
JPanel northPanel = new JPanel();
JPanel centerPanel = new JPanel();JLabel label1 = new JLabel("考试剩余时间:");
JLabel label2 = new JLabel();JButton startButton = new JButton("开始考试");
JButton submitButton = new JButton("提交答案");
JButton nextButton = new JButton("下一题");JRadioButton rbutton1 = new JRadioButton("A");
JRadioButton rbutton2 = new JRadioButton("B");
JRadioButton rbutton3 = new JRadioButton("C");
JRadioButton rbutton4 = new JRadioButton("D");
ButtonGroup group = new ButtonGroup();public Test(){
frame.setLayout(new BorderLayout());
frame.add(southPanel, BorderLayout.SOUTH);
frame.add(northPanel, BorderLayout.NORTH);
frame.add(centerPanel, BorderLayout.CENTER);northPanel.setLayout(new FlowLayout());
northPanel.add(label1);
northPanel.add(label2);
northPanel.add(startButton);centerPanel.add(questScrollPane);southPanel.setLayout(new FlowLayout());
southPanel.add(rbutton1);
southPanel.add(rbutton2);
southPanel.add(rbutton3);
southPanel.add(rbutton4);
southPanel.add(nextButton);
southPanel.add(submitButton);group.add(rbutton1);
group.add(rbutton2);
group.add(rbutton3);
group.add(rbutton4);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);
frame.setLocation(300, 200);
frame.setVisible(true);
frame.validate();
nextButton.addActionListener(this);
startButton.addActionListener(this);
submitButton.addActionListener(this);}public void actionPerformed(ActionEvent e){
if(e.getSource()==nextButton){
//返回答案,并显示下一题;
}
if(e.getSource()==startButton){//开始考试
new Test().start();
//countTime();
}
if(e.getSource()==submitButton){}
}public void run(){//考试倒计时
int minutes = 0;
int seconds = 5;
String s;
while(true){
s = minutes+"分"+seconds+"秒";
label2.setText(s);
System.out.println(s);
questArea.setText(s);
frame.repaint();
if(seconds==0&&minutes!=0){
seconds=60;
minutes--;
}
else if(seconds==0&&minutes==0){
break;
}
seconds--;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}public static void main(String []args){
new Test();
}
}
具体用法还是自己慢慢去学吧大概看了一下,不知道说得有没有错