很簡(jiǎn)單 我給你個(gè)我寫(xiě)的程序,你可以參考一下 不懂的問(wèn)我。
我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、萊山ssl等。為成百上千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的萊山網(wǎng)站制作公司
public class Shoupiao {
public static void main(String[] args) {
chushou c1=new chushou("A地火車(chē)站",10);
c1.start();//調(diào)用Thread中的run方法需要用start();
chushou c2=new chushou("B地火車(chē)站",5);
c2.start();
chushou c3=new chushou("C地火車(chē)站",8);
c3.start();
try {
c1.join(); //執(zhí)行完 c1 c2 c3 在繼續(xù)執(zhí)行 目的讓結(jié)束最后輸出
c2.join();
c3.join();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("結(jié)束");
}
}
//新建一個(gè)類(lèi)
class chushou extends Thread{//用繼承Thread的方法來(lái)實(shí)現(xiàn)多線(xiàn)程
String iname;
public void run() {
for(int i=inumber;i0;i--){
System.out.println(iname+"火車(chē)票賣(mài)出一張"+"剩余"+(i-1)+"張");
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
int inumber;
public String getIname() {
return iname;
}
public void setIname(String iname) {
this.iname = iname;
}
public int getInumber() {
return inumber;
}
public void setInumber(int inumber) {
this.inumber = inumber;
}
public chushou(String iname, int inumber) {
this.iname = iname;
this.inumber = inumber;
}
}
這個(gè)不難,我正好有個(gè)類(lèi)似的模板,稍微修改下文字,調(diào)整下就可以用了。
你說(shuō)的這個(gè)程序應(yīng)該是不難的,只不過(guò)N久沒(méi)有寫(xiě)過(guò)javaswing 了
還有就是java沒(méi)有多繼承的,只有多實(shí)現(xiàn)接口的
繼承的關(guān)鍵字是:extends
接口的關(guān)鍵字是:interface
package yaoshun.Thread;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
/*
* 多線(xiàn)程同步輸出
*/
public class MultiThread extends JFrame {
private int ticket; // 計(jì)數(shù)變量
private JButton jButton;
private JLabel jLabel;
private JLabel jLabel1;
private JLabel jLabel2;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JTextArea jTextArea1;
private JTextArea jTextArea2;
private JTextArea jTextArea3;
private JTextArea jTextArea4;
private JTextArea jTextArea5;
private Thread thread1;
private Thread thread2;
private Thread thread3;
private Thread thread4;
private Thread thread5;
// 構(gòu)造器
public MultiThread() {
setName("火車(chē)票銷(xiāo)售情況");
setLayout(null); // 使用絕對(duì)布局
setSize(680, 720);
setResizable(false); // 設(shè)置窗體大小不變
setLocationRelativeTo(null);// 設(shè)置窗體居中
jLabel = new JLabel("火車(chē)站售票大廳");
jLabel.setBounds(280, 20, 140, 30);
jLabel.setFont(new Font(getName(), Font.BOLD, 16)); // 設(shè)置字體大小
jLabel.setForeground(Color.BLUE); // 設(shè)置字體顏色
jLabel1 = new JLabel("售票窗1");
jLabel1.setBounds(72, 50, 80, 50);
jLabel1.setForeground(Color.red);
jLabel2 = new JLabel("售票窗2");
jLabel2.setBounds(192, 50, 80, 50);
jLabel2.setForeground(Color.red);
jLabel3 = new JLabel("售票窗3");
jLabel3.setBounds(312, 50, 80, 50);
jLabel3.setForeground(Color.red);
;
jLabel4 = new JLabel("售票窗4");
jLabel4.setBounds(432, 50, 80, 50);
jLabel4.setForeground(Color.red);
jLabel5 = new JLabel("售票窗5");
jLabel5.setBounds(552, 50, 80, 50);
jLabel5.setForeground(Color.red);
jTextArea1 = new JTextArea();
jTextArea1.setBounds(45, 100, 100, 500);
jTextArea1.setEditable(false); // 文本框不可編輯
jTextArea2 = new JTextArea();
jTextArea2.setBounds(165, 100, 100, 500);
jTextArea2.setEditable(false); // 文本框不可編輯
jTextArea3 = new JTextArea();
jTextArea3.setBounds(285, 100, 100, 500);
jTextArea3.setEditable(false); // 文本框不可編輯
jTextArea4 = new JTextArea();
jTextArea4.setBounds(405, 100, 100, 500);
jTextArea4.setEditable(false); // 文本框不可編輯
jTextArea5 = new JTextArea();
jTextArea5.setBounds(525, 100, 100, 500);
jTextArea5.setEditable(false); // 文本框不可編輯
jButton = new JButton("開(kāi)始售票");
jButton.setBounds(270, 625, 140, 40);
jButton.setFont(new Font(getName(), Font.BOLD, 16));// 設(shè)置字體
jButton.setForeground(Color.blue);
thread1 = new Thread(new MyThread1());
thread2 = new Thread(new MyThread2());
thread3 = new Thread(new MyThread3());
thread4 = new Thread(new MyThread4());
thread5 = new Thread(new MyThread5());
// 為按鈕添加監(jiān)聽(tīng)器
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
}
});
add(jLabel);
add(jLabel1);
add(jLabel2);
add(jLabel3);
add(jLabel4);
add(jLabel5);
add(jTextArea1);
add(jTextArea2);
add(jTextArea3);
add(jTextArea4);
add(jTextArea5);
add(jButton);
setVisible(true); // 設(shè)置窗體顯示
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 設(shè)置窗體關(guān)閉
}
class MyThread1 extends Thread {
// 重寫(xiě)run方法
public synchronized void run() {
while (ticket 100) {
++ticket;
jTextArea1.append("第" + ticket + "張票已售出" + "\n");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自動(dòng)生成的 catch 塊
e.printStackTrace();
}
}
}
}
class MyThread2 extends Thread {
// 重寫(xiě)run方法
public synchronized void run() {
while (ticket 100) {
++ticket;
jTextArea2.append("第" + ticket + "張票已售出" + "\n");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自動(dòng)生成的 catch 塊
e.printStackTrace();
}
}
}
}
class MyThread3 extends Thread {
// 重寫(xiě)run方法
public synchronized void run() {
while (ticket 100) {
++ticket;
jTextArea3.append("第" + ticket + "張票已售出" + "\n");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自動(dòng)生成的 catch 塊
e.printStackTrace();
}
}
}
}
class MyThread4 extends Thread {
// 重寫(xiě)run方法
public synchronized void run() {
while (ticket 100) {
++ticket;
jTextArea4.append("第" + ticket + "張票已售出" + "\n");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自動(dòng)生成的 catch 塊
e.printStackTrace();
}
}
}
}
class MyThread5 extends Thread {
// 重寫(xiě)run方法
public synchronized void run() {
while (ticket 100) {
++ticket;
jTextArea5.append("第" + ticket + "張票已售出" + "\n");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO 自動(dòng)生成的 catch 塊
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
MultiThread multiThread = new MultiThread();
}
}
自己改一下 包名吧,這個(gè)題目是很多學(xué)校在多線(xiàn)程都用來(lái)的基礎(chǔ)練習(xí)題。
文章名稱(chēng):車(chē)站售票系統(tǒng)java代碼 車(chē)站售票系統(tǒng)java代碼查詢(xún)
文章地址:http://muchs.cn/article30/doccdso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、搜索引擎優(yōu)化、網(wǎng)站排名、品牌網(wǎng)站制作、靜態(tài)網(wǎng)站、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容