我想用一个循环,分别在三个文本域(jTextAreaNo1,jTextAreaNo2,jTextAreaNo3)输出三个字符串(mem1,mem2,mem3)。
我想要的效果是:
jTextAreaNo1先显示mem1的第一个字符,然后jTextAreaNo2显示mem2的第一个字符,然后jTextAreaNo3显示mem3的第一个字符;
jTextAreaNo1再显示mem1的第二个字符,然后jTextAreaNo2再显示mem2的第二个字符……依次下去。
目的是:
模拟字节多路通道程序分时为3个设备服务。
我是这样做的:
int i;
for( i=0;i<mem1.length() || i<mem2.length() || i<mem3.length();i++){
  if(i<mem1.length()){
    jTextAreaNo1.append(String.valueOf(mem1.charAt(i)));
  }else{
    jTextAreaNo1.append(String.valueOf(" "));
  }
  for(int j=0;j<999999999;j++);  if(i<mem2.length()){
    jTextAreaNo2.append(String.valueOf(mem2.charAt(i)));
  }else{
    jTextAreaNo2.append(String.valueOf(" "));
  }
  for(int j=0;j<999999999;j++);

  if(i<mem3.length()){
    jTextAreaNo3.append(String.valueOf(mem3.charAt(i)));
  }else{
    jTextAreaNo3.append(String.valueOf(" "));
  }
  for(int j=0;j<999999999;j++);
}用了 for(int j=0;j<999999999;j++);希望能达到“分时显示”的效果。
但实际效果是,程序停顿了一会儿,然后三个文本域同时把内容显示出来了。
我把 for(int j=0;j<999999999;j++);换成wait()、sleep()也未达到想要的效果。
请问要怎么改进?请大家多多指教。十分感谢。
——————————
所有的源代码:import java.awt.*;
import javax.swing.*;
public class MainBoard extends JFrame { private static final long serialVersionUID = 1L;
private JMenuBar jJMenuBar = null;
private JPanel jPanel = null;
private JToolBar jJToolBarBar = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JLabel jLabelState = null;
static String mem1 ="love"; 
        static String mem2 ="study";
        static String mem3 ="wahaha";  
private JTextField jTextFieldCPU = null;
private JPanel jPanel1 = null;
private JTextArea jTextAreaNo1 = null;
private JTextArea jTextAreaNo2 = null;
private JTextArea jTextAreaNo3 = null;
/**
 * This is the default constructor
 */
public MainBoard() {
super();
initialize();
} /**
 * This method initializes this
 * 
 * @return void
 */
private void initialize() {
this.setSize(521, 217);
this.setContentPane(getJPanel());
this.setJMenuBar(getJJMenuBar());
this.setTitle("字节多路通道处理模拟");
} /**
 * This method initializes jJMenuBar
 * 
 * @return javax.swing.JMenuBar
 */
private JMenuBar getJJMenuBar() {
if (jJMenuBar == null) {
jJMenuBar = new JMenuBar();
}
return jJMenuBar;
} /**
 * This method initializes jPanel
 * 
 * @return javax.swing.JPanel
 */
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.add(getJJToolBarBar(), null);
jPanel.add(getJTextFieldCPU(), null);
jPanel.add(getJPanel1(), null);

}
return jPanel;
} /**
 * This method initializes jJToolBarBar
 * 
 * @return javax.swing.JToolBar
 */
private JToolBar getJJToolBarBar() {
if (jJToolBarBar == null) {

jLabelState = new JLabel();
jLabelState.setText("通道处理模拟");
jLabelState.setHorizontalAlignment(SwingConstants.CENTER);

jJToolBarBar = new JToolBar();
jJToolBarBar.setBounds(new Rectangle(0, 0, 517, 40));
jJToolBarBar.add(getJButton());
jJToolBarBar.add(getJButton1());
jJToolBarBar.add(getJButton2());
jJToolBarBar.setLayout(new GridLayout(1,4));
jJToolBarBar.add(jLabelState);

}
return jJToolBarBar;
} /**
 * This method initializes jButton
 * 
 * @return javax.swing.JButton
 */
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton("添加");
jButton.addActionListener(new java.awt.event.ActionListener() {
Add add;
public void actionPerformed(java.awt.event.ActionEvent e) {
if(add == null){
add = new Add();
}
add.setVisible(true);
}
});

}
return jButton;
} /**
 * This method initializes jButton1
 * 
 * @return javax.swing.JButton
 */
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton("请求");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jTextFieldCPU.setBackground(Color.BLUE);
jTextFieldCPU.setText("CPU正在组织通道程序");

jLabelState.setText("通道程序编制完毕!");
}
});

}
return jButton1;
} /**
 * This method initializes jButton2
 * 
 * @return javax.swing.JButton
 */
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton("启动");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {

jTextFieldCPU.setBackground(Color.RED);
jTextFieldCPU.setText("CPU正在运行");     int i;
int flag = 1;
for( i=0;i<mem1.length() || i<mem2.length() || i<mem3.length();i++){
if(i<mem1.length()){
jTextAreaNo1.append(String.valueOf(mem1.charAt(i)));
}else{
jTextAreaNo1.append(String.valueOf(" "));
}
for(int j=0;j<999999999;j++);

if(i<mem2.length()){
jTextAreaNo2.append(String.valueOf(mem2.charAt(i)));
}else{
jTextAreaNo2.append(String.valueOf(" "));
}
for(int j=0;j<999999999;j++);

if(i<mem3.length()){
jTextAreaNo3.append(String.valueOf(mem3.charAt(i)));
}else{
jTextAreaNo3.append(String.valueOf(" "));
}
for(int j=0;j<999999999;j++);
}

// 延迟
for(int j=0;j<999999999;j++);
jLabelState.setText("通道数据传输完毕!");
jTextFieldCPU.setText("CPU响应中断");
jTextFieldCPU.setBackground(Color.BLUE);


}
});

}
return jButton2;
} /**
 * This method initializes jTextFieldCPU
 * 
 * @return javax.swing.JTextField
 */
private JTextField getJTextFieldCPU() {
if (jTextFieldCPU == null) {
jTextFieldCPU = new JTextField();
jTextFieldCPU.setBounds(new Rectangle(391, 41, 121, 138));
jTextFieldCPU.setBackground(Color.red);
jTextFieldCPU.setHorizontalAlignment(JTextField.CENTER);
jTextFieldCPU.setText("CPU正在运行");
}
return jTextFieldCPU;
} /**
 * This method initializes jPanel1
 * 
 * @return javax.swing.JPanel
 */
private JPanel getJPanel1() {
if (jPanel1 == null) {
jPanel1 = new JPanel();
jPanel1.setLayout(new GridLayout(3,1,3,3));
jPanel1.setBounds(new Rectangle(4, 40, 388, 143));
jPanel1.add(getJTextAreaNo1(), null);
jPanel1.add(getJTextAreaNo2(), null);
jPanel1.add(getJTextAreaNo3(), null);
}
return jPanel1;
} /**
 * This method initializes jTextAreaNo1
 * 
 * @return javax.swing.JTextArea
 */
private JTextArea getJTextAreaNo1() {
if (jTextAreaNo1 == null) {
jTextAreaNo1 = new JTextArea();
jTextAreaNo1.setText("设备1:\n");
jTextAreaNo1.setBackground(Color.yellow);
}
return jTextAreaNo1;
} /**
 * This method initializes jTextAreaNo2
 * 
 * @return javax.swing.JTextArea
 */
private JTextArea getJTextAreaNo2() {
if (jTextAreaNo2 == null) {
jTextAreaNo2 = new JTextArea();
jTextAreaNo2.setText("设备2:\n");
jTextAreaNo2.setBackground(Color.yellow);
}
return jTextAreaNo2;
} /**
 * This method initializes jTextAreaNo3
 * 
 * @return javax.swing.JTextArea
 */
private JTextArea getJTextAreaNo3() {
if (jTextAreaNo3 == null) {
jTextAreaNo3 = new JTextArea();
jTextAreaNo3.setText("设备3:\n");
jTextAreaNo3.setBackground(Color.yellow);
}
return jTextAreaNo3;
}}