Exercise1

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/**

*java programming 2015-12-9
*Lab 10
*/
package lab10;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.Visibility;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Exercise1 extends JFrame {
private JButton btn;
private JPanel Npan;
private JPanel Cpan;
private JPanel Span;
private JPanel pan;
private JLabel Nlabel1;
private JLabel Nlabel2;
private JLabel Nlabel3;

private JTextField Ctf1;
private JTextField Ctf2;
private JTextField Ctf3;
private Object synObj = new Object();
private boolean isRun;

public Exercise1() {
setTitle("run");
setSize(200,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2, Toolkit.getDefaultToolkit().getScreenSize().height/2);
setLayout(new BorderLayout());
frame();
setVisible(true);
btn.addActionListener(new btnListener());
}

private class btnListener implements ActionListener{

@Override
public void actionPerformed(ActionEvent arg0) {
if(btn.getText().equals("开始")){
btn.setText("停止");
isRun=true;
// 点击启动线程
runLabel setRun1 =new runLabel(Ctf1);
runLabel setRun2 =new runLabel(Ctf2);
runLabel setRun3 =new runLabel(Ctf3);
Thread run1=new Thread(setRun1);
Thread run2=new Thread(setRun2);
Thread run3=new Thread(setRun3);

run1.start();
run2.start();
run3.start();

}
else {
isRun=false;
btn.setText("开始");
}
}
}

public class runLabel implements Runnable{
JTextField textField;
public runLabel(JTextField argTextField){
textField = argTextField; // 初始化属性
}

@Override
public void run() {
while(isRun) {//字母循环输入
try {
Thread.sleep(100);
String str="";
str= str+(char) (Math.random ()*26+'A');
textField.setText(str);
}
catch (Exception e)
{
e.printStackTrace();
}

}

}
}
public void frame(){
pan=new JPanel();
Npan=new JPanel();
Cpan=new JPanel();
Span=new JPanel();

// pan.setLayout(new BorderLayout());
// 设置布局
Npan.setLayout(new FlowLayout());
Cpan.setLayout(new FlowLayout());
Span.setLayout(new FlowLayout());

Nlabel1=new JLabel("线程一",10);
Nlabel2=new JLabel("线程二",10);
Nlabel3=new JLabel("线程三",10);

Ctf1 =new JTextField(" ");
Ctf2 =new JTextField(" ");
Ctf3 =new JTextField(" ");
Ctf1.setPreferredSize(new Dimension(50,20));
Ctf2.setPreferredSize(new Dimension(50,20));
Ctf3.setPreferredSize(new Dimension(50,20));

btn =new JButton("开始");


btn.setPreferredSize(new Dimension(80,20));

Npan.add(Nlabel1);
Npan.add(Nlabel2);
Npan.add(Nlabel3);

Cpan.add(Ctf1);
Cpan.add(Ctf2);
Cpan.add(Ctf3);

Span.add(btn);

add(Npan,BorderLayout.NORTH);
add(Cpan,BorderLayout.CENTER);
add(Span,BorderLayout.SOUTH);


}

public static void main(String[] args){
Exercise1 pro=new Exercise1();

}
}

相关文档
最新文档