最近想做一個無線數據監控服務端,用java做的,面板Jframe設置了一個Jpanel,我稱這個為Jpanel 1 ,在Jpanel 1中我定義了一個多選JCheckbox,有四種監控方式可選:Bluetooth,Wifi,GPRS,Zigbee。還定義了一個JButton,分為開始ON,結束OFF,然後在JPanel 1中定義了JPanel 2,JPanel 2 中我定義了JScrollPane,scrollAndSetCursor用於實現滾動以及光標相關效果,程序運行的大致過程是這樣的,我先選擇監控方法,(方法是多選的,我想能夠同步實現這幾個方法),然後點擊ON按鈕就開始運行服務器程序了, 重點來了 , 我現在的問題是 ,停止按鈕OFF不知道怎麼設置方法能夠讓程序停止當前的線程,我想實現的是點擊OFF後,當前的監控方式運行全部停止,(就跟剛開始巡行這個程序一樣,可以重新開始選擇監控方式,在重新點擊ON又可以重新運行),現在就是不知道怎麼在jbArray[1](ON按鈕的監控事件)中定義,感覺自己寫的jbArray[0](ON按鈕)也有問題。(我其實是想實現這四種方法可以同步運行,互不干擾,多線程運行)希望各位哥哥姐姐幫幫忙,幫我改一改jbArray[0](按鈕的監聽事件)以及實現jbArray[1](OFF結束當前監控方式線程事件),謝謝各位了,代碼稍微有點長,我全貼上來了,辛苦各位了
package SystemTray;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.Rectangle;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.io.StreamConnection;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import BluetoothChatServer.*;
import GPRSChatServer.*;
import WifiChatServer.*;
import ZigbeeChatServer.*;
public class SystemTrayDemo extends JFrame {
protected JTextArea ta = new JTextArea(20,42);
protected Timer timer = new Timer();
protected boolean jbarry = false;
protected TrayIcon trayIcon = null;
protected SystemTrayDemo(){
this.setTitle("Data monitoring server");
TrayInit();
WinInit();
//取得當前屏幕的寬度和高度
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
//設置窗體大小
this.setSize(610, 470);
//設置窗體初始顯示的位置
this.setLocation((width-610)/2, (height-470)/2);
this.setResizable(false);//設置窗體不可調整大小
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置窗體關閉方式,關閉窗體時同時結束程序
final JPanel jp1 = new JPanel();
jp1.setLayout(null);//不設置的話是無法設置jp2的大小。等同於窗口的大小。
JPanel jp2=new JPanel();
jp2.setBounds(110,20,480,390);
//jp2.setBounds(new Rectangle(110,15,450,350));//設置jp2的左邊距、上邊距、長度、高度,在jp1沒設置setLayout(null);是無效的
//jp2.setBackground(Color.gray);//設置框體的背景色
//jp2.setBorder(BorderFactory.createEtchedBorder(Color.black, Color.BLUE)); //設置框體的邊框
final JScrollPane jsp = new JScrollPane(ta);
jp2.add(jsp,BorderLayout.CENTER);
jp1.add(jp2);
/*ta.setText("Start.....\n");
TimerTask task = new TimerTask(){
public void run(){
ta.append("Time: " + (System.currentTimeMillis() / 1000) + "\n");
scrollAndSetCursor();
}
};
time.schedule(task, 0, 1000);*/
//super();
//enableEvents(AWTEvent.WINDOW_EVENT_MASK);
//創建標簽數組
JLabel[] jlArray = {new JLabel("Switch"),new JLabel("Mode")};
//創建復選框數組
final JCheckBox[] jcbArray = {new JCheckBox("Bluetooth",true),new JCheckBox("Wifi"),new JCheckBox("GPRS"),
new JCheckBox("Zigbee")};
//創建按鈕數組
final JButton[] jbArray = {new JButton("ON"),new JButton("OFF")};
//設置布局管理器
for(int i=0;i<4;i++){
//設置復選按鈕的大小位置
jcbArray[i].setBounds(10,185+i*30,80,30);
//將復選按鈕添加到JPanel中
jp1.add(jcbArray[i]);
//設置標簽與普通按鈕的大小位置
if(i>1){
continue;
} //continue後的語句不在執行。。
jlArray[i].setBounds(30,20+i*140,80,30);
jbArray[i].setBounds(10,50+i*45,80,30);
//將標簽與普通按鈕添加到JPanel中
jp1.add(jlArray[i]);
jp1.add(jbArray[i]);
}
this.add(jp1);
//為普通按鈕注冊動作事件監聽器。
//開始按鈕 **感覺這地方有問題,如何修改?????**
jbArray[0].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
jbArray[0].setEnabled(jbarry);
int result= JOptionPane.showConfirmDialog(null,"Start monitoring program now?","Message",JOptionPane.YES_NO_OPTION);
if(result==0){
//是
StringBuffer temp0 = new StringBuffer();
for(int i = 0;i<4;i++){
if(jcbArray[i].isSelected()){
String tmpstr;
tmpstr = jcbArray[i].getText()+" monitoring method ";
ta.append(tmpstr + "\n");
temp0.append(tmpstr);
}
}
if(temp0.length()==0){
JOptionPane.showMessageDialog(null,"The monitoring method can't be empty!","Message",JOptionPane.WARNING_MESSAGE);
jbArray[0].setEnabled(!jbarry);
}
//藍牙線程
Thread Bluetooth = new Thread(){
public void run() {
if(jcbArray[0].isSelected()){
new BluetoothRemoteServer();
}
}
};
//Bluetooth.setName("Bluetooth");
//String name0 = Bluetooth.getName();
//ta.append(name0+" start\n");
Bluetooth.start();
//Wifi線程
Thread Wifi = new Thread(){
public void run() {
if(jcbArray[1].isSelected()){
new WifiRemoteServer();
}
}
};
//Wifi.setName("Wifi");
// String name1 = Wifi.getName();
// ta.append(name1+" start\n");
Wifi.start();
//GPRS線程
Thread GPRS = new Thread(){
public void run() {
if(jcbArray[2].isSelected()){
new GPRSRemoteServer();
}
}
};
//GPRS.setName("GPRS");
//String name2 = GPRS.getName();
//ta.append(name2+" start\n");
GPRS.start();
//Zigbee線程
Thread Zigbee = new Thread(){
public void run() {
if(jcbArray[3].isSelected()){
new ZigbeeRemoteServer();
}
}
};
//Zigbee.setName("Zigbee");
//String name3 = Zigbee.getName();
//ta.append(name3+" start\n");
Zigbee.start();
}
if(result==1){
//否
jbArray[0].setEnabled(!jbarry);
}
}
});
//結束按鈕 **如何實現點擊jbArray[1]後能夠停止前面jbArray[0]中選擇的監控方式的線程?????**
jbArray[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result= JOptionPane.showConfirmDialog(null,"End monitoring program now?","Message",JOptionPane.YES_NO_OPTION);
if(result==0){
//是
for(int i=0; i<jcbArray.length; i++){
jcbArray[i].setSelected(false);
}
ta.setText(null);
//後續功能完善
jbArray[0].setEnabled(!jbarry);
}
if(result==1){
//否
}
}
});
}
//托盤的功能選項設置
public void TrayInit(){
if(SystemTray.isSupported()){ //檢查當前系統是否支持系統托盤
SystemTray tray = SystemTray.getSystemTray();//獲取表示桌面托盤區的 SystemTray 實例。
URL imageUrl=SystemTrayDemo.class.getResource("java.png");
ImageIcon icon = new ImageIcon(imageUrl);
//Image image = Toolkit.getDefaultToolkit().getImage("D:\\kissjava.gif");
PopupMenu popupMenu = new PopupMenu(); //為托盤添加右鍵菜單
MenuItem exitItem = new MenuItem("Exit");
//退出exitItem按鈕
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
System.exit(0);
}catch(Exception ex) {
ex.printStackTrace();
}
}
});
popupMenu.add(exitItem);
//創建帶指定圖像、工具提示和彈出菜單的 TrayIcon
trayIcon = new TrayIcon(icon.getImage(), "System is running", popupMenu);
trayIcon.addMouseListener(new java.awt.event.MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2){
showIT(true);
}
}
});
try{
tray.add(trayIcon); // 將 TrayIcon 添加到 SystemTray。
} catch (AWTException e) {
System.err.println(e);
}
}else{
JOptionPane.showMessageDialog(null,"The operating system does not support tray","Message",JOptionPane.ERROR_MESSAGE);
}
}
//窗體的菜單功能選項設置
public void WinInit(){
JMenuBar menubar = new JMenuBar();//為窗體添加菜單功能
setJMenuBar(menubar);
//開關option選項
/*前端添加小圖標
* JMenu option = new JMenu("開關");
* Icon startIcon = new ImageIcon("image/bluetooth.png");
JMenuItem start = new JMenuItem("開始",startIcon);
*/
//設置setting選項
JMenu setting = new JMenu("Setting");
JMenu language = new JMenu("Language");
setting.add(language);
JMenuItem language1 = new JMenuItem("中文");
language.add(language1);
JMenuItem language2 = new JMenuItem("English");
language.addSeparator();
language.add(language2);
JMenu update = new JMenu("Update");
setting.addSeparator();
setting.add(update);
JMenuItem update1 = new JMenuItem("Version update");
update.add(update1);
update1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"please send the mail to [email protected]","Message",JOptionPane.PLAIN_MESSAGE);
}
});
//關於help選項
JMenu help = new JMenu("Help");
JMenuItem about = new JMenuItem("About");
help.add(about);
//後續功能完善
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null," Copyright @2013 popcorn\n All Right Reserved\n author popcorn\n version 1.0","Message",JOptionPane.PLAIN_MESSAGE);
}
});
menubar.add(setting);
menubar.add(help);
}
/** 該方法用於實現滾動以及光標相關效果 */
public void scrollAndSetCursor(){
// ta.requestFocus();
ta.setSelectionStart(ta.getText().length());
}
public void showIT(boolean visable){
if(this.isVisible() != visable)
this.setVisible(visable);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
new SystemTrayDemo().setVisible(true);//設置窗體顯示
}
});
}
}
你這個只要停止方法即可停止線程
比如 new GPRSRemoteServer();這句代碼是在線程中執行的 你改成
全局定義一個變量
GPRSRemoteServer gs = null;
然後線程中執行
gs = new GPRSRemoteServer();
停止按鈕代碼處則是
if(null!=gs){
gs.stop();
}
前提是你在該類GPRSRemoteServer中實現一個停止的方法 比如 stop