利用Java線程及JFrame面板製作一個隨機搖號小程序

很多都是自己寫的,慢慢的嘗試,裏面改了很多次數

但是最後的結果是沒有錯的

最後的最後終於得到想要的結果

慢慢的訓練總能得到訓練的

加油!!!!

上代碼:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class MDialog extends JDialog {
private JTextField tf1,tf2,tf3,tf4;
MThread mt1,mt2,mt3,mt4;
JButton bt1,bt2,bt3;
JTextArea ja=new JTextArea();
JLabel jl[]={null,null,null,null};
JLabel jb=new JLabel();
JScrollPane scoll=new JScrollPane();
ThreadGroup tg=new ThreadGroup("搖號");
// static boolean isstop=true;
public MDialog(){
this.setTitle("隨機搖號小程序");
this.setBackground(Color.BLUE);
this.setBounds(100, 100, 450, 300);
getContentPane().setLayout(null);
bt1=createButton(this,"開始",0);
tf1=createTextField(this, 0);
tf2=createTextField(this, 1);
tf3=createTextField(this, 2);
tf4=createTextField(this, 3);
mt1=new MThread(tf1, tg);
mt2=new MThread(tf2, tg);
mt3=new MThread(tf3, tg);
mt4=new MThread(tf4, tg);
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// String n1=tf1.getText();
// String n2=tf2.getText();
// String n3=tf3.getText();
// String n4=tf4.getText();
// isstop=true;
// MThread t1=new MThread(tf1,tg);
// MThread t2=new MThread(tf2,tg);
// MThread t3=new MThread(tf3,tg);
// MThread t4=new MThread(tf4,tg);
//MThread t1=new MThread(tf1,tg);
mt1.start();
mt2.start();
mt3.start();
mt4.start();
bt1.setEnabled(false);
}
});
JLabel lb=new JLabel("數字顯示");
lb.setBounds(37, 27, 54, 15);
this.getContentPane().add(lb);

// jl[0]=createJLabel(this, 0);
// jl[1]=createJLabel(this, 1);
// jl[2]=createJLabel(this, 2);
// jl[3]=createJLabel(this, 3);
// jl1.setBounds(60, 90, 35, 21);
// jl2.setBounds(98, 90,35, 21);
// jl3.setBounds(136, 90,35, 21);
// jl4.setBounds(174, 90,35, 21);
// this.add(jl[0]);
// this.add(jl[1]);
// this.add(jl[2]);
// this.add(jl[3]);
ja.setBounds(37, 90, 200, 150);
this.add(ja);
this.add(scoll);
bt2=createButton(this, "抽取一次", 1);
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// isstop=false;
// mt1.interrupt();
// mt2.interrupt();
// mt3.interrupt();
// mt4.interrupt();
//System.out.println(mt1.s);
// String s1;
// s1=(mt1.m+"");
// String s2;
// s2=(mt2.m+"");
// String s3;
// s3=(mt3.m+"");
// String s4;
//// s4=(mt4.m+"");
// jl[0].setText(mt1.jf.getText());
// jl[1].setText(mt2.jf.getText());
// jl[2].setText(mt3.jf.getText());
// jl[3].setText(mt4.jf.getText());
tg.interrupt();
synchronized (mt1) {
synchronized (mt2) {
synchronized (mt3) {
synchronized (mt4) {
// System.out.println();
// ja.setLineWrap(true);
ja.append("\n"+mt1.jf.getText()+""+mt2.jf.getText()+""+mt3.jf.getText()+""+mt4.jf.getText());
// scoll.add(ja);
// ja.setText(mt2.jf.getText());
}
}
}
}
bt2.setEnabled(false);
}

});
bt3=createButton(this, "繼續", 2);
bt3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tg.resume();
bt1.setEnabled(false);
bt2.setEnabled(true);
}
});
}
public static int getnumber(int n){
int m;
m=n;
return m;
}
public class MThread extends Thread {
int i=0;
JTextField jf=new JTextField();
// int m;
public MThread(JTextField jf,ThreadGroup tg){
super(tg,"搖號");
this.jf=jf;
}
public void run(){
while(true){
// int t=0;
synchronized (this) {
i=(i+1)%10;
jf.setText(""+i);
//
}
if(this.interrupted()){
this.suspend();
}
// t++;
}
}
}
public static void main(String[] args){
try{
MDialog dialog=new MDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
}
public static JTextField createTextField(MDialog mg, int i){
JTextField jf=new JTextField();
jf.setBounds(37+i*38,52,35,21);
mg.getContentPane().add(jf);
return jf;
}
public static JLabel createJLabel(MDialog mg,int i){
JLabel jl=new JLabel();
jl.setBounds(37+i*38, 90, 35, 21);
mg.getContentPane().add(jl);
return jl;
}
public static JButton createButton(MDialog mg, String string, int i){
//Icon name;
JButton jb=new JButton(string);
jb.setBounds(300, 52+i*35, 95, 25);
mg.getContentPane().add(jb);
return jb;
}

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章